NEW PAPER đź§µ: Deep neural networks are complicated, but looking inside them ought to be simple. In this paper we introduce TorchLens, a package for extracting all activations and metadata from any #PyTorch model, and visualizing its structure, in as little as one line of code. 1/
The core function is log_forward_pass. Just pass in any PyTorch model (as-is, no changes needed) and input, and this one line of code gives you a data structure with activations and metadata of any layer, along with an automatic visualization of the model’s computational graph 3/
What’s new here? Till now, there have been arbitrary roadblocks to fetching hidden activations from PyTorch models: for example, some methods can get activations from “modules” but not from all tensor operations, or fail for models with if-then branching ("dynamic models") 4/
TorchLens works by (temporarily!) mutating all elementary PyTorch functions that return a tensor such that every function call is logged. Since all PyTorch models are built from these "building block" functions, the approach it uses should work for any PyTorch model whatsoever 5/
So far, it's been tested on hundreds of models, spanning different inputs (images/videos/audio/ language/multimodal) & architectures (feedforward, recurrent, transformers, graph neural networks). You can see example visuals in this “model menagerie”:
The visuals are meant to make the salient aspects of the network “pop out”: inputs are green, outputs are red, layers with trainable parameters are in gray, and layers with internally generated tensors (e.g., that generate random noise) are shown with dashed lines. 7/
TorchLens will automatically find and mark the loops in recurrent models for you, and you can visualize them in either rolled or unrolled format. Check out the CORNet models in the model menagerie for some real-life examples:
For models with a hierarchical “Russian nesting dolls” structure that have modules within modules, you can specify how deep you want to visualize. For example, here's an "inception" module from GoogLeNet shown with different levels of nesting. 9/
If the model's operations change based on the input (a stumbling block for some approaches to PyTorch feature extraction), it simply dynamically traces what actually happens in the forward pass for that input, and marks the operations involved in evaluating the "if" statement 10/
TorchLens even works for massive models. For instance, here's the visual for the swin_v2b transformer model, which has nearly 2000 tensor operations, all of which are logged and visualized. 11/
…and here’s the visual for the Keypoint R-CNN detection model with nearly 8000 tensor operations (this one took awhile to render!)
In addition to the activations and visuals, TorchLens gives you exhaustive metadata both about the overall model, and about each layer. You can see a mostly-complete list of available metadata here:
Metadata includes info about the tensors & trainable parameters for each layer, model graph info, info about the function executed in each layer (e.g., the runtime—useful for profiling), & much more. I included everything I could think of, but let me know if I missed anything 14/
Other features include being able to inspect the actual code used to execute each layer, and being able to save the gradients from a backward pass—see the CoLab for how to do this. 15/
One principle of TorchLens is "no silent errors". Since there are infinitely many possible DNNs (and potential edge cases we didn't predict), TorchLens has a built-in validation procedure to algorithmically verify the accuracy of saved activations—useful for new architectures 16/
Since its initial release, TorchLens has greatly benefitted from user feedback. For example, it now has a function to quickly save activations to new model inputs, and to apply a user-provided function (e.g., averaging conv layers over space) to activations before saving them 17/
Many thanks to members of the @KriegeskorteLab, @CogCompNeuro and @VSSMtg attendees, @alfcnz, @grez72, @_jacobprince_, and Colin Conwell (not on Twitter) for their invaluable suggestions. Since TorchLens remains in active development, we welcome your further feedback.
18/
TorchLens should be ready to go—just type the following into your terminal to install it (graphviz is required for making the visuals), and you should be set:
We hope that TorchLens will complement the existing outstanding packages (e.g. ThingsVision, Net2Brain, DeepDive) for DNN feature extraction: these have excellent functionality for loading models and stimuli and analyzing the resulting activations, which TorchLens lacks. 21/
You should also check out our lab’s recently-updated RSA Toolbox, which provides a complete solution for performing RSA on DNN activations and neural/behavioral data.
We envision TorchLens being useful in various ways:
1. For neuroscience, to streamline comparing DNNs and brains, 2. For engineering, to aid in debugging, profiling, and visualizing models, and 3. For teaching, to help translate between concepts and code
23/
Understanding the brain and advancing #AI is hard enough as it is. We hope TorchLens removes all arbitrary barriers to seeing what's going on in your network, lighting up the black box so you can focus on the more interesting problems. Tell us how we can make it better!
24/
• • •
Missing some Tweet in this thread? You can try to
force a refresh
1/ NEW PREPRINT #2: Your visual experience is full of colored shapes. How does your brain encode colors, shapes, and the way they’re combined? biorxiv.org/cgi/content/sh…
2/ In this study we examined 1) do separate brain regions encode these features, or are they encoded by the same regions? And 2) if the latter, how are these features encoded relative to each other: independently or interactively?
3/ We applied fMRI MVPA to responses in early visual cortex and higher-level ventral stream regions sensitive to either shape or color, examining stimuli varying in color, and either orientation or curvature.
1/ NEW PREPRINT: Much work has examined the “binding problem” of how the brain encodes combinations of visual features. But how do convolutional neural networks do it?
2/ In this paper, we looked at how CNNs encode color/shape combinations: are these features encoded independently or does the representation of one feature interact with the other feature?
3/ Specifically, we used a variation of RSA to look at whether encoding of color varies across different shape features.
@General_Jagoff @MattWalshBlog Go to the actual study in PNAS. It was so deeply flawed that the authors had to issue a correction specifically stating that it doesn’t show what people are taking it to show (that black people aren’t more frequent victims of violence).
@General_Jagoff @MattWalshBlog Meanwhile there are plenty of carefully controlled studies showing that black people *are* disproportionate victims of violence... e.g. journals.plos.org/plosone/articl…
1/ Tragically, the famous mathematician John Conway has recently passed away to COVID-19. To commemorate his life, here's a thread about his most famous creation, the Game of Life, which raises many provocative questions about the nature of complex systems.
2/ The Game of Life is a system consisting of an infinite grid of squares, that can be either black (alive) or white (dead). Every time step, the squares evolve according to three simple rules:
3/ 1) Any live cell with two or three live neighbors survives. 2) Any dead cell with three live neighbors becomes a live cell. 3) All other live cells die in the next generation. Similarly, all other dead cells stay dead.
1/ Just finished the fantastic (if pretentiously titled) biography "William James in the Maelstrom of American Modernism" by Robert Richardson. James is one of those people who grabs your attention and gets more interesting the more you learn about them. Some thoughts
2/ First, and most obviously, the guy is phenomenally quotable, on any number of topics... a smattering of examples (some of his better ones are longer than Tweet length and rely heavily on his obsession with italics, unfortunately):
3/ On socializing and the self: "A man has as many social selves as there are distinct groups of persons about whose opinion he cares."
Pointing out hypocrisy in political opponents can be fun/satisfying (feels like a "knockdown argument"--got em!) and has its uses but 1) it often replaces the work of making the case for an alternative vision, promoting laziness (1/4)
2) "inconsistent" isn't the worst thing an ideology can be (e.g. consistent sociopathy/sadism is bad; why should "at least they're consistent" always have a positive connotation?), and (2/4)
3) when overemphasized it can lead to timidness/cynicism about having costly principles at all (South Park effect); can't be called a hypocrite if you never commit to something that sometimes demands personal sacrifices. (3/4)