A generic package to inspect and extract activations from deep learning models built in pytorch. The code allows you to:
- Extract the architecture of the model in a human-readable JSON file
- Extract activations from some or all of the intermediate modules (layers) of the model
pip
support coming soon
- Copy the
pytorch_inspector
directory into the root of your code - Import
add_opts
andcheck_opts
frompytorch_inspector.opts
add_opts
takes anArgumentParser
(argparse
lib) and adds the options necessary for the inspector. Add this when you are defining yourArgumentParser
but before your parse the actual input (beforeparse_args()
call)check_opts
checks if the conditions for the inspector are met - Add this after yourparse_args()
call
- Import
Mode
frompytorch_inspector.opts
andload_model_config
/save_model_config
frompytorch_inspector.structure
- You can use
opt.mode
to see if the user requested for model structure extraction (= Mode.extract_structure
) or activations extraction (= Mode.extract_activations
) - if the mode is model structure extraction, you can call
save_model_config
to save the model architecture in a JSON file - if the mode is model activation extraction, you can call
load_model_config
to load the model configuration, and initialize anActivationsExtractor
(pytorch_inspector.extractor.ActivationsExtractor
) - After your forward pass and before you exit the code, perform a final call to
ActivationsExtractor.save_activations()
to save the activations
- You can use
We have already implemented the extractor for some existing code bases:
- OpenNMT-py:
clients/OpenNMT-py/translate.py
- Support for shards comes out of the box. The extractor saves activations in memory before dumping them to disk - if your inputset is large and you cannot fit all the activations in memory, you can use sharding to periodically dump everything to disk after
N
instances - Currently, the extractor only supports
batch_sizes
= 1, but support for larger batches is coming soon
Coming soon