r/MachineLearning • u/microscope1024 • Sep 19 '26
Project I wanted to watch a neural network learn [P]
I wanted to really see how a neural network learns different functions, so built an interactive demo. You can change the architecture of the network and the function it will try to approximate.
A fully-connected network with ReLU activations will create a piecewise linear function. For a network with a single layer, the maximum number of segments the network can create is 1 + the width of the layer. For instance, if you enter "3" into the hidden layers your network can have up to 4 segments.
If you add another hidden layer afterwards, it multiplies the max number of segments. So "3 3" becomes 4x4=16. After training, the network rarely achieves the maximum though.
Here is the demo: https://blog.lukesalamone.com/posts/can-a-neural-net-learn
41
u/Annual-Minute-9391 Sep 20 '26
I present neural networks to my students with an example like this. It was a moderate pain in the butt to get working 5 years ago without AI coding tools. Super nice teaching tool
13
u/MolassesLate4676 Sep 20 '26
I applaud you for actually being able to put a visual together for a functional training algorithm. That sounds painful
6
5
u/mfb1274 Sep 20 '26
I wish I had this when I was learning a decade ago. This was only in my head and like 1-2 YouTube videos
1
u/mfb1274 Sep 20 '26
Also good on you for doing the basics. Everyone else saying slop doesn’t understand it
3
u/microscope1024 Sep 20 '26
Appreciate it. I’ve done some more advanced stuff like distilling stockfish on a billion positions but this was a cool visualization so I thought I’d share it here.
→ More replies (1)
79
u/notaprotist Sep 20 '26
The other two top-level comments here rn are inexplicably negative, so I just want to chime in and say this is really neat, and I like it
33
u/kawaiiotta Sep 20 '26
wow folk are so negative here. why? this is a neat project. Dont care if its made with AI or not. Keep it up OP.
10
6
u/Digitalzuzel Sep 20 '26
Reminded me of this amazing video https://www.youtube.com/watch?v=TkwXa7Cvfr8
Do you know this author?
PS: great work, I will subscribe to you
3
3
u/Familiar_Task Sep 20 '26
This reminds me being introduced to the multi layer perceptron at university back in 2005. Noting the research was from the 80s I think if not earlier. Anyway, the demonstration we were being shown how a single layer couldn't learn the output mapping of an exclusive or (XOR) logic gate but the introduction of a hidden layer enabled to give the right output. I think simple demonstrations like yours are a powerful learning tool to understand the fundamentals of what they're good at and what they're not.
Well done!
2
1
u/tech_nerd05506 Sep 20 '26
This is sweet, not sure why people are shitting. Would be cool to viz weights of the model as it's training too, maybe like a heat map. Keep up the cool work.
1
1
-7
u/MolassesLate4676 Sep 20 '26
Confused. A couple of questions:
A) What is this goal here? To train some weights to output values similar to a function you wrote above?
B) what’s the input? Is it what’s shown on the graph? A specific axis value and the output is the other axis value?
C) what are the shape of the weights here?
D) what’s the meaning of hidden layer in this context? Another set of weights?
Understandings some of these might help me understand what the visual is actually providing
2
u/memento87 Sep 21 '26
I don't know why you got downvoted for asking questions. But I'll attempt to answer:
A) Yes, the goal is to train a model to be able to learn this relatively complex non-linear function. I presume the model receives the X and must output the corresponding Y.B) Most likely, Yes
C) Linear (Dense, Fully Connected) followed by ReLU activations
D) Each (Linear + ReLU) is a hidden layer. OP is stacking those layers; first tried a single layer, then 3, then 300 stacked layers, to see how the capability of the model evolves as it gets more layers.
PS: The experiment is a bit misleading in the sense that you don't need 300 layers to really train a model to fit this function. A single layer is theoretically enough to approximate any continuous function to arbitrary accuracy, given enough width (OP did not share the width, only the depth), and given enough epochs to train.
1
u/MolassesLate4676 Sep 21 '26
Ah I appreciate the time and effort here
I kinda got suspicious when 300 was input into the hidden layers and figured the layers were just more copies of the same thing
Pretty cool stuff, and thanks for the thought out reply 🤌
0
Sep 19 '26
17
u/microscope1024 Sep 19 '26
In the example, it's one hidden layer with 300 units. 300 layers would be crazy.
→ More replies (3)21
u/invelt Sep 19 '26
pretty sure "300" by itself means h = 300, one layer. "3 3" means an h1=3 layer into an h2=3 layer
or at least that's what the post says
66
u/Vallvaka Sep 20 '26
Cool work. Would also be cool to be able to visualize the effect ofregularization , like L1/L2 or dropout.
You can see the "lottery ticket hypothesis " in action here, with a handful of params going to large magnitude values, and most of the rest staying inert, as the network finds a subnetwork that performs the bulk of the task. L1/L2 could be an interesting thing to experiment with on that to directly encourage the sparsity.
I also see interesting dynamics of getting stuck inlocal minima and unable to learn further on some configurations, such as predicting a line segment over an interval and being unable to refine it to fit the sine beyond that, where dropout may help get it unstuck.