bioRxivNeurosciencePreprint
TorchLens: A Python package for extracting and visualizing hidden activations of PyTorch models
Abstract
Deep neural network models (DNNs) are essential to modern AI and provide powerful models of information processing in biological neural networks. Researchers in both neuroscience and engineering are pursuing a better understanding of the internal representations and operations that undergird the successes and failures of DNNs. Neuroscientists additionally evaluate DNNs as models of brain computation by comparing their internal representations to those found in brains. It is therefore essential to have a method to easily and exhaustively extract and characterize the results of the internal operations of any DNN. Many models are implemented in PyTorch, the leading framework for building DNN models. Here we introduce TorchLens, a new open-source Python package for extracting and characterizing hidden-layer activations in PyTorch models. Uniquely among existing approaches to this problem, TorchLens has the following features: (1) it exhaustively extracts the results of all intermediate operations, not just those associated with PyTorch module objects, yielding a full record of every step in the models computational graph, (2) it provides an intuitive visualization of the models complete computational graph along with metadata about each computational step in a models forward pass for further analysis, (3) it contains a built-in validation procedure to algorithmically verify the accuracy of all saved hidden-layer activations, and (4) the approach it uses can be automatically applied to any PyTorch model with no modifications, including models with conditional (if-then) logic in their forward pass, recurrent models, branching models where layer outputs are fed into multiple subsequent layers in parallel, and models with internally generated tensors (e.g., injections of noise). Furthermore, using TorchLens requires minimal additional code, making it easy to incorporate into existing pipelines for model development and analysis, and useful as a pedagogical aid when teaching deep learning concepts. We hope this contribution will help researchers in AI and neuroscience understand the internal representations of DNNs.
- Citations
- 2
Reproducibility ledger
Nobody has reported trying to reproduce this paper yet. Did you replicate an experiment or run the code? Your report saves the next lab a week.
Sign in to file a reproducibility report.
Related papers
- THINGSvision: a Python toolbox for streamlining the extraction of activations from deep neural networks
Lukas Muttenthaler, Martin N. Hebart·Frontiers in NeuroinformaticsPeer-reviewed·Sep 22, 2021
- DNNBrain: a unifying toolbox for mapping deep neural networks and brains
Xiayu S. Chen, Ming Quan Zhou, … Jia Liu B·Frontiers in Computational NeurosciencePeer-reviewed·Nov 30, 2020
- Deep Neural Networks In Computational Neuroscience
Tim Christian Kietzmann, Patrick McClure, Nikolaus Kriegeskorte·Oxford Research Encyclopedia of NeurosciencePeer-reviewed·Jan 25, 2019
- NiftyTorch: A Deep Learning framework for NeuroImaging
Adithya Subramanian, Haoyu Lan, … Farshid Sepehrband·bioRxiv·Feb 26, 2021
- Compact deep neural network models of visual cortex
Benjamin R. Cowley, Patricia L. Stan, … Matthew A. Smith·NaturePeer-reviewed·Feb 25, 2026
- openretina: Collaborative Retina Modelling Across Datasets and Species
Federico D’Agostino, Thomas Zenkel, … Matthias Bethge·bioRxiv·Mar 7, 2025