### Install TorchLens and Dependencies Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md This snippet provides the bash commands to install TorchLens and its required dependency, Graphviz. Graphviz is necessary for generating network visualizations. ```Bash sudo apt install graphviz pip install torchlens ``` -------------------------------- ### Retrieve Model Metadata Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Get metadata for a PyTorch model without saving any activations. This function is equivalent to calling `log_forward_pass` with `layers_to_save=None` and is useful for understanding the model's structure before deciding which layers to inspect. ```Python tl.get_model_metadata(model) ``` -------------------------------- ### Log Forward Pass with TorchLens Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Demonstrates how to use `torchlens.log_forward_pass` to capture intermediate activations and metadata from a PyTorch model (AlexNet) during a forward pass. It saves all layers and uses an unrolled visualization option. ```Python import torch import torchvision import torchlens as tl alexnet = torchvision.models.alexnet() x = torch.rand(1, 3, 224, 224) model_history = tl.log_forward_pass(alexnet, x, layers_to_save='all', vis_opt='unrolled') print(model_history) ``` -------------------------------- ### Log Forward Pass and Visualize PyTorch Model Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md This snippet demonstrates how to use TorchLens to log the forward pass of a PyTorch model and generate a visualization. It shows the extraction of activations from intermediate layers, specifically the second pass of the first linear layer in a simple recurrent model. ```Python class SimpleRecurrent(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(in_features=5, out_features=5) def forward(self, x): for r in range(4): x = self.fc(x) x = x + 1 x = x * 2 return x simple_recurrent = SimpleRecurrent() model_history = tl.log_forward_pass(simple_recurrent, x, layers_to_save='all', vis_opt='rolled') print(model_history['linear_1_1:2'].tensor_contents) # second pass of first linear layer ``` -------------------------------- ### Access Function Call Stack Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Retrieve the function call stack information for a specific layer's output. This provides insights into the execution context and the exact code path that generated the activation. ```Python print(model_history['conv2d_3'].func_call_stack[8]) ``` -------------------------------- ### Visualize Model Graph Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Generate a visualization of the PyTorch model's computational graph. This function does not save any activations and is purely for architectural understanding. ```Python tl.show_model_graph(model) ``` -------------------------------- ### Validate Model Activations Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Perform a validation procedure to ensure the correctness of saved model activations. This involves multiple forward passes and checks against ground-truth outputs and random data. ```Python tl.validate_model_activations(model, activations) ``` -------------------------------- ### Access Layer Information from ModelHistory Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Shows how to retrieve specific layer information, such as activations and parameters, from a `ModelHistory` object. It illustrates accessing a layer by its name ('conv2d_3_7'), module name ('features.6'), or ordinal position. ```Python print(model_history['conv2d_3_7']) # pulling out layer by its name # The following commented lines pull out the same layer: # model_history['conv2d_3'] you can omit the second number (since strictly speaking it's redundant) # model_history['conv2d_3_7:1'] colon indicates the pass of a layer (here just one) # model_history['features.6'] can grab a layer by the module for which it is an output # model_history[7] the 7th layer overall # model_history[-17] the 17th-to-last layer ``` -------------------------------- ### Log Forward Pass with Specific Layers Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Log the forward pass of a PyTorch model, selectively saving activations for specified layers. This allows for memory optimization by avoiding the storage of all intermediate activations. Layers can be specified by name, index, or substring. ```Python model_history = tl.log_forward_pass(alexnet, x, vis_opt='unrolled', layers_to_save=['conv2d_3_7', 'features', -5, 'linear']) print(model_history.layer_labels) ``` -------------------------------- ### Extract Tensor Activations from Layer Source: https://github.com/johnmarktaylor91/torchlens/blob/main/README.md Access the raw tensor contents of a specific layer's output from a model's forward pass history. This is useful for analyzing intermediate results within a neural network. ```Python print(model_history['conv2d_3_7'].tensor_contents) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.