Crossposted from my Substack. Try the demo in your browser: it trains live, so you can watch the dots cluster and the arrows move. Set the hidden layer to Linear to match this post.
TL;DR: As I mentioned at the end of my last post, my favorite way to think about a neuron in layer i + 1 is as a feature detector. Its incoming weights form an n-dimensional vector, where n is the number of neurons in layer i, and that vector defines a direction in layer i’s activation space. The neuron fires most strongly when the activations of layer i point in that direction (measured by the dot product). This really gives the intuition that a neuron is a singular feature detector.
Well, that’s a lot of words that don’t really mean much if you can’t visualize it yourself. I very much like to be able to understand concepts like these in my head, so I made a demo to do so.
This linear MLP is trained on the same task as last time: classifying handwritten digits from 0-9. However, the difference between this and the last post’s network is I have now included a 3-neuron hidden layer in between the 64 neuron input layer and the 10 neuron output layer (with no activation function this collapses to one layer but this is for visualization purposes). This forces the network to do many interesting things:
The network is forced to compress the information of 64 input features (one per pixel in the image) into a 3-dimensional activation space that will later feed 10 output features (one per digit 0-9)
Because this hidden layer has 3 neurons, the neuron activations of any input image X can be drawn as a point/vector in 3-dimensional space, where the dimensions of the space correspond to the activations h1, h2 and h3 in that hidden layer
We can visualize every neuron in the output layer as a feature detector vector in 3-dimensional space where provides an intuitive sense on what that output neuron is looking for
(Cool thought: because this network has no activation function, everything does condense down to one layer. But what would that layer look like? It’s not exactly the same as the 64 → 10 network from last post. It would still be a single layer going straight from the 64 pixels to the 10 outputs, except it wouldn’t be as powerful as a normal one. Because everything still had to pass through 3 neurons, the 64 weights of each output neuron would just be a mix of the three hidden neuron images, with the mixing amounts being that neuron’s vector in the 3D graph above. That means all 10 output neurons are stuck looking inside the same 3D slice of the 64-dimensional pixel space, and the network is completely blind to anything about an image that falls outside of that slice).
To understand how powerful this intuition can be, we have to break apart what’s going on here piece by piece.
So let’s say you feed an input image X through the network. That image itself comes in the form of a 64-dimensional vector where each dimension is the intensity of one of the 64 pixels in the image. This 64-dimensional space contains every possible image you could make from pixel intensities in an 8x8 grid. Here is a point in that 64-dimensional space:
Our input image vector X then passes through the first layer of the network, where it undergoes a linear transformation that turns the 64-dimensional vector into a 3-dimensional vector. Yes, this means that we have lost almost all of the original information of our input X.
So here we have it. Our input image has been transformed and now lives temporarily as a 3-dimensional vector in the activation space of our hidden layer. That is what you can see here:
Now there’s a lot going on here. We see many different colored dots arranged in 3D space, as well as 10 different colored vectors that seem to be as orthogonal to each other as 10 vectors in 3D space could ever get (this comes in later). Let’s break everything down:
The dots
Each dot above represents a distinct input image X from our training dataset (hollow dots come from the test dataset). Like I said above, all the information from the 64D input X has been condensed down to 3 numbers, which we can plot in 3D space. As you can see, the first layer of our network has clearly learned some way to represent images of the same number in clusters that take up a different region of the 3D space than clusters representing other numbers (I mean, it’s pretty crazy when you see it in real space like this).
The vectors
This is the really cool part. Each vector above is actually a representation of what each of the 10 output neurons in the next layer has learned to look for in this previous layer! That means unlike our dots, which depend on a specific input X, these vectors are determined by learned parameters (weights).
How do you calculate these vectors? Well, take any given neuron in the layer that comes after the hidden layer we are currently looking at. Each of these output neurons connects to our hidden layer by 3 connections (weights), meaning 3 values that the network has learned to look for. Now, consider the formula for that output neuron with respect to the three activations of our hidden layer neurons:
Now that makes sense on paper. But how can you really imagine this? Well, consider our 3 learned weight parameters as a 3-dimensional vector: <w1, w2, w3>. Here’s the intuition: because you have one weight per hidden layer neuron, you can graph that vector in the 3D activation space of the hidden layer. Even crazier: you can calculate that neuron’s output as the dot product of that weight vector and the hidden activations of your X. As an example, let’s say you have an input image that turned into the vector <a1, a2, a3> in the hidden layer activation space. The dot product of <a1, a2, a3> and our weight vector <w1, w2, w3> is a1 * w1 + a2 * w2 + a3 * w3, which is the exact same formula as earlier (without the bias).
But what does that mean?
The simplified explanation of a dot product is that it is a tool that measures how much 2 vectors align in space (scaled by their lengths). That means that every neuron is really just a feature: a direction in the activation space before it, which fires in proportion to how aligned the incoming activation vector is with that direction. That’s crazy. The weights it has learned are more than just how much each previous neuron should affect this neuron; the weights themselves are a vector that represents a specific feature. This is how networks abstract increasingly hierarchical, higher-level features as layers get deeper: they are just specific directions in previous activation spaces, searching for activation vectors that align with them.
What has the network learned?
So, going back to the picture, it’s important to separate what the network has learned from what is simply data from our dataset. Each colored dot is a specific input datapoint; what the network has learned is where to place it. Each vector is where an output neuron in the network has learned to look. During training you can actually watch these two things evolve, as the network learns to separate the images into separate clusters, and the output neurons learn where to look to detect those clusters. In case you’re wondering, here’s what the graph looks like on an untrained network.
With completely randomized parameters, our network has no sense of separating the input images into clusters, and our random weight vectors have no idea where to look. When training, you can watch the dots slowly organize themselves into clusters and the vectors move to point to where those clusters are. Can you believe that? That’s what it’s like to watch a neural network train itself??? Thats so crazy.
Another interesting side thought: if you tried to graph this out for the first layer, you would have a 64-dimensional graph with a 64D vector representing the total information of each input image X, and three 64D vectors representing what each of our hidden layer neurons is “looking for” in that space.
Is this superposition?
I will go much further into my thoughts on superposition in a future write-up. Simply put, though, this is a concrete example of the core idea behind superposition in neural networks. With fewer hidden layer neurons than output features, the network is forced to encode 10 different features in R3, where only 3 directions can ever be truly orthogonal. So instead, the 10 feature vectors fan out in all directions to stay as far apart from each other as they can. Even in the best possible arrangement, 10 vectors in 3D can only be about 65 degrees from their nearest neighbors, well short of the 90 degrees of true orthogonality, and our network’s vectors don’t even spread out that perfectly (look at how close 1 and 8 are). This means the features have to overlap: an image that lines up with one output neuron’s vector will always partially line up with its neighbors too. One caveat is that superposition in real networks also relies on features being sparse and on nonlinearities to filter out that overlap, which our linear network doesn’t have, so think of this as the geometric half of the story.
Interpreting the hidden layer neurons
This is where it gets good. Because the network has to squeeze too many directions into 3 neurons, it is quite difficult to interpret what our 3 hidden layer neurons are actually looking for in an input image X. If I had to come up with a plausible scenario, I would say the neurons would look for specific features in the input image, like how curved/straight the number is, how much pixel space it takes up in total, and maybe how vertical/horizontal its straight edges are. Later neurons could then parse some combination of these features to determine which number it actually is. Well, we are in luck: since each of our hidden layer neurons connects to the 64 input pixels with 64 weights, we can visualize what each neuron is actually looking for by turning those 64 weights into an 8x8 image, shown below:
Yeah, so this is why interpretability is hard. Now that I think about it, h1 actually looks very much like a 2, so it makes sense that it fires highly for 2 and for curved numbers. But everything else seems pretty uninterpretable. I mean, there definitely seems to be some difference between straight-edged numbers and curvy numbers, but not much more than that from what I can see.
Wrapping up
Obviously there are a few caveats that leave a lot more to talk about in future posts. For example, how do activation functions and bias terms play into this? In terms of activation functions, without them our network can’t actually build hierarchical features on top of each other, which means that the whole simplification of neurons in a next layer building new features in previous activation spaces doesn’t work unless you add nonlinearity. In terms of leaving out the bias, we are thinking of output neurons as vectors in the previous activation space, when more accurately they can be represented as hyperplanes defined by the equation w1x1 + w2x2 + w3x3 + b = 0. This changes the intuition slightly: adding the bias term lets us incorporate distance from the origin into our output neuron intuition. The output neuron’s activation is now proportional to how far our X is from the hyperplane, positive on one side and negative on the other. However, thinking in terms of the hyperplane is still more or less the same intuition as before. For example, the weight vector is simply the normal to this plane and contains all of the same information except for the bias term.
Anyways, thanks for reading. I love finding new ways to think about networks, and this one is for sure my most transformative demo yet. An upcoming post will be about a cool new idea I just had: what if you thought of weight vectors as all the weights coming out of a neuron instead of the ones coming in? This means that each neuron contributes its own vector, which lives in the activation space of the next layer, and these vectors add up to form the full activation vector of that next layer across all of its neurons.
Crossposted from my Substack. Try the demo in your browser: it trains live, so you can watch the dots cluster and the arrows move. Set the hidden layer to Linear to match this post.
TL;DR: As I mentioned at the end of my last post, my favorite way to think about a neuron in layer i + 1 is as a feature detector. Its incoming weights form an n-dimensional vector, where n is the number of neurons in layer i, and that vector defines a direction in layer i’s activation space. The neuron fires most strongly when the activations of layer i point in that direction (measured by the dot product). This really gives the intuition that a neuron is a singular feature detector.
Well, that’s a lot of words that don’t really mean much if you can’t visualize it yourself. I very much like to be able to understand concepts like these in my head, so I made a demo to do so.
This linear MLP is trained on the same task as last time: classifying handwritten digits from 0-9. However, the difference between this and the last post’s network is I have now included a 3-neuron hidden layer in between the 64 neuron input layer and the 10 neuron output layer (with no activation function this collapses to one layer but this is for visualization purposes). This forces the network to do many interesting things:
(Cool thought: because this network has no activation function, everything does condense down to one layer. But what would that layer look like? It’s not exactly the same as the 64 → 10 network from last post. It would still be a single layer going straight from the 64 pixels to the 10 outputs, except it wouldn’t be as powerful as a normal one. Because everything still had to pass through 3 neurons, the 64 weights of each output neuron would just be a mix of the three hidden neuron images, with the mixing amounts being that neuron’s vector in the 3D graph above. That means all 10 output neurons are stuck looking inside the same 3D slice of the 64-dimensional pixel space, and the network is completely blind to anything about an image that falls outside of that slice).
To understand how powerful this intuition can be, we have to break apart what’s going on here piece by piece.
So let’s say you feed an input image X through the network. That image itself comes in the form of a 64-dimensional vector where each dimension is the intensity of one of the 64 pixels in the image. This 64-dimensional space contains every possible image you could make from pixel intensities in an 8x8 grid. Here is a point in that 64-dimensional space:
Our input image vector X then passes through the first layer of the network, where it undergoes a linear transformation that turns the 64-dimensional vector into a 3-dimensional vector. Yes, this means that we have lost almost all of the original information of our input X.
So here we have it. Our input image has been transformed and now lives temporarily as a 3-dimensional vector in the activation space of our hidden layer. That is what you can see here:
Now there’s a lot going on here. We see many different colored dots arranged in 3D space, as well as 10 different colored vectors that seem to be as orthogonal to each other as 10 vectors in 3D space could ever get (this comes in later). Let’s break everything down:
The dots
Each dot above represents a distinct input image X from our training dataset (hollow dots come from the test dataset). Like I said above, all the information from the 64D input X has been condensed down to 3 numbers, which we can plot in 3D space. As you can see, the first layer of our network has clearly learned some way to represent images of the same number in clusters that take up a different region of the 3D space than clusters representing other numbers (I mean, it’s pretty crazy when you see it in real space like this).
The vectors
This is the really cool part. Each vector above is actually a representation of what each of the 10 output neurons in the next layer has learned to look for in this previous layer! That means unlike our dots, which depend on a specific input X, these vectors are determined by learned parameters (weights).
How do you calculate these vectors? Well, take any given neuron in the layer that comes after the hidden layer we are currently looking at. Each of these output neurons connects to our hidden layer by 3 connections (weights), meaning 3 values that the network has learned to look for. Now, consider the formula for that output neuron with respect to the three activations of our hidden layer neurons:
output neuron activation = a1 * w1 + a2 * w2 + a3 * w3 + b
Now that makes sense on paper. But how can you really imagine this? Well, consider our 3 learned weight parameters as a 3-dimensional vector: <w1, w2, w3>. Here’s the intuition: because you have one weight per hidden layer neuron, you can graph that vector in the 3D activation space of the hidden layer. Even crazier: you can calculate that neuron’s output as the dot product of that weight vector and the hidden activations of your X. As an example, let’s say you have an input image that turned into the vector <a1, a2, a3> in the hidden layer activation space. The dot product of <a1, a2, a3> and our weight vector <w1, w2, w3> is a1 * w1 + a2 * w2 + a3 * w3, which is the exact same formula as earlier (without the bias).
But what does that mean?
The simplified explanation of a dot product is that it is a tool that measures how much 2 vectors align in space (scaled by their lengths). That means that every neuron is really just a feature: a direction in the activation space before it, which fires in proportion to how aligned the incoming activation vector is with that direction. That’s crazy. The weights it has learned are more than just how much each previous neuron should affect this neuron; the weights themselves are a vector that represents a specific feature. This is how networks abstract increasingly hierarchical, higher-level features as layers get deeper: they are just specific directions in previous activation spaces, searching for activation vectors that align with them.
What has the network learned?
So, going back to the picture, it’s important to separate what the network has learned from what is simply data from our dataset. Each colored dot is a specific input datapoint; what the network has learned is where to place it. Each vector is where an output neuron in the network has learned to look. During training you can actually watch these two things evolve, as the network learns to separate the images into separate clusters, and the output neurons learn where to look to detect those clusters. In case you’re wondering, here’s what the graph looks like on an untrained network.
With completely randomized parameters, our network has no sense of separating the input images into clusters, and our random weight vectors have no idea where to look. When training, you can watch the dots slowly organize themselves into clusters and the vectors move to point to where those clusters are. Can you believe that? That’s what it’s like to watch a neural network train itself??? Thats so crazy.
Another interesting side thought: if you tried to graph this out for the first layer, you would have a 64-dimensional graph with a 64D vector representing the total information of each input image X, and three 64D vectors representing what each of our hidden layer neurons is “looking for” in that space.
Is this superposition?
I will go much further into my thoughts on superposition in a future write-up. Simply put, though, this is a concrete example of the core idea behind superposition in neural networks. With fewer hidden layer neurons than output features, the network is forced to encode 10 different features in R3, where only 3 directions can ever be truly orthogonal. So instead, the 10 feature vectors fan out in all directions to stay as far apart from each other as they can. Even in the best possible arrangement, 10 vectors in 3D can only be about 65 degrees from their nearest neighbors, well short of the 90 degrees of true orthogonality, and our network’s vectors don’t even spread out that perfectly (look at how close 1 and 8 are). This means the features have to overlap: an image that lines up with one output neuron’s vector will always partially line up with its neighbors too. One caveat is that superposition in real networks also relies on features being sparse and on nonlinearities to filter out that overlap, which our linear network doesn’t have, so think of this as the geometric half of the story.
Interpreting the hidden layer neurons
This is where it gets good. Because the network has to squeeze too many directions into 3 neurons, it is quite difficult to interpret what our 3 hidden layer neurons are actually looking for in an input image X. If I had to come up with a plausible scenario, I would say the neurons would look for specific features in the input image, like how curved/straight the number is, how much pixel space it takes up in total, and maybe how vertical/horizontal its straight edges are. Later neurons could then parse some combination of these features to determine which number it actually is. Well, we are in luck: since each of our hidden layer neurons connects to the 64 input pixels with 64 weights, we can visualize what each neuron is actually looking for by turning those 64 weights into an 8x8 image, shown below:
Yeah, so this is why interpretability is hard. Now that I think about it, h1 actually looks very much like a 2, so it makes sense that it fires highly for 2 and for curved numbers. But everything else seems pretty uninterpretable. I mean, there definitely seems to be some difference between straight-edged numbers and curvy numbers, but not much more than that from what I can see.
Wrapping up
Obviously there are a few caveats that leave a lot more to talk about in future posts. For example, how do activation functions and bias terms play into this? In terms of activation functions, without them our network can’t actually build hierarchical features on top of each other, which means that the whole simplification of neurons in a next layer building new features in previous activation spaces doesn’t work unless you add nonlinearity. In terms of leaving out the bias, we are thinking of output neurons as vectors in the previous activation space, when more accurately they can be represented as hyperplanes defined by the equation w1x1 + w2x2 + w3x3 + b = 0. This changes the intuition slightly: adding the bias term lets us incorporate distance from the origin into our output neuron intuition. The output neuron’s activation is now proportional to how far our X is from the hyperplane, positive on one side and negative on the other. However, thinking in terms of the hyperplane is still more or less the same intuition as before. For example, the weight vector is simply the normal to this plane and contains all of the same information except for the bias term.
Anyways, thanks for reading. I love finding new ways to think about networks, and this one is for sure my most transformative demo yet. An upcoming post will be about a cool new idea I just had: what if you thought of weight vectors as all the weights coming out of a neuron instead of the ones coming in? This means that each neuron contributes its own vector, which lives in the activation space of the next layer, and these vectors add up to form the full activation vector of that next layer across all of its neurons.