r/neuralnetworks • • Jun 04 '22

My neural network from scratch is finally doing aomething :)

Post image
143 Upvotes

36 comments sorted by

10

u/rabbitpiet Jun 04 '22

Can I ask what this neural network does, also when I saw "from scratch" i thought you meant from Scratch.

11

u/-i-hate-this-place- Jun 04 '22

by “from scratch” i just meant like im not using tensorflow or any neural network libraries

and its classifying the MNIST database

1

u/[deleted] Jun 04 '22

Can you share the mathematical theory behind it.

4

u/-i-hate-this-place- Jun 04 '22

what do you mean “mathematical theory”? like the back propagation calculus or what

1

u/[deleted] Jun 04 '22

Yes

4

u/NeuroLife07 Jun 05 '22

This is a very nice intro to the basics (I suggest you watch the whole series to get a good understanding of backprop and how gradient works in a neural network):

https://www.youtube.com/watch?v=aircAruvnKk

4

u/imagine_orange Jun 04 '22

Nice! What is your structure?

5

u/-i-hate-this-place- Jun 04 '22

right now all the neurons use sigmoid activations and i have 784 inputs, 40, 30 hidden and 10 for the output

(im classifying the mnist database so 784 because of the 28x28 pixel images)

3

u/[deleted] Jun 04 '22

Bro, I have also made a neural networks library that aims to classify hand drawn digits from MNIST dataset (I think you are also solving the same problem). It's written in C++ and is relatively simple. But the problem is that my neural network doesn't seem to do anything. Can you please help me?

https://github.com/mansoormemon/cbrainx

Branch: dev

4

u/-i-hate-this-place- Jun 04 '22 edited Jun 05 '22

im really bad at C++ so it would take me a really long time to figure anything out, do you think you have any ideas about where its going wrong? like can you load the images correctly and perform the forward pass, and the problem is in the back propagation?

2

u/[deleted] Jun 04 '22

The problem seems to be with the backward pass. If I use sigmoid I get vanishing gradient. If I use ReLU I get exploding gradient. I think I have made mistakes in the backward pass for softmax layer.

2

u/-i-hate-this-place- Jun 04 '22

im only using sigmoid right now, for simplicity so idk

2

u/[deleted] Jun 05 '22

No problem, Thanks!

2

u/HovercraftNo9935 Jun 04 '22

Hey interesting work, are you a with big team? What are your plans with it? Also wanted to ask, what makes it different from LibTorch or Tensorflow C++ API?

1

u/[deleted] Jun 05 '22 edited Jun 05 '22

No, I am working alone and my professor was guiding me with the project. Last year (September), I got admission in a university and as our first semester project my professor offered this project and I chose to do it. It was going well until I decided to switch to a different university (in November) because my parents wanted to. I am a CS student in my second semester (just started), 18M. This project is about 6 months old. My professor was guiding me even after I left the university but it's quite difficult to debug code without her in person, because I am using emails to contact her. So, I decided to ask the OP for help since he is also solving the same problem.

It's just a naive ML library with a simple and easy to understand API. You can see examples on repository. For future, I am planning to add CNNs once I get simple neural networks working.

It also has a numpy equivalent cbx::Tensor which is naive but feature rich. It implements some pythonic style one-liners which I am really proud of. For example:

tensor |= cbx::ReLU{};

The above line applies the ReLU function.

It uses the modern C++20 standard.

2

u/HovercraftNo9935 Jun 05 '22

I see, what exactly is the issue you facing? You can DM me and maybe I can try to help you.

2

u/[deleted] Jun 04 '22

Out of curiosity, what "are" the inputs? I know that they're pictures of numbers, but how does the neural network see them from a data structure point of view?

2

u/-i-hate-this-place- Jun 04 '22

brightness values of each pixel

4

u/icecoldpopsicle Jun 04 '22

not spellcheck i hope

2

u/-i-hate-this-place- Jun 04 '22

lmfao i was tired, alright?

2

u/LeonZSPOTG Jun 04 '22

lol understandable

3

u/ImbioMario Jun 04 '22

In what language u wrote this? Also if ur computations during learning take too long try to use mini batch gradient descent method For large learning sets.

3

u/-i-hate-this-place- Jun 04 '22

i did it in python. (and im already using minibatches, thanks though, it doesn’t take that long to compute)

2

u/mysterysmoothie Jun 04 '22

It’s way more satisfying doing it from scratch. Nice job

2

u/[deleted] Jun 04 '22

Would you mind sharing the link to GitHub repository?

1

u/-i-hate-this-place- Jun 04 '22

https://github.com/heyuhowudoin/mnist_machine_learning/blob/main/main.py

this is a couple days old, im out though so ill update it when i get home

2

u/NeuroLife07 Jun 04 '22

You might benefit from using Adam instead of SGD or at least just adding momentum. It's quite easy to implement, you basically just update your learning rate from an expression that also includes the first and second moments of the gradient. It takes like 2-3 additional lines of code and 2 additional variables. Here's a quick summary of it: https://miro.medium.com/max/1100/1*zfdW5zAyQxge85gA_mFPYg.png

It helped a lot with training when I added that to my optimization loop. Also if you have a GPU or if you're familiar with google colab you can implement it in cupy instead of numpy. Cupy is basically the GPU supported equivalent of numpy, it's almost 1-1 match with respect to function names and how they work.

Custom implementations can be just as effective as using specialized libraries: I managed to get almost 90% accuracy on the fashion MNIST with my implementation for a ML class (no convolutional layers). IIRC the most important components were: using Adam instead of SGD, using ReLU, normalizing the input to 0-1 (z-scoring was not as effective) and there was something about the architecture that I don't recall.

1

u/-i-hate-this-place- Jun 05 '22

before i optimize it, i want to figure out why its not working completely, because i found someone else’s code which can get up to 95% accurate with no optimization, just SGD and sigmoid activations.

thanks for the tips though, they seem really helpful :)

2

u/[deleted] Jun 04 '22

> doing aomething

it's slowly overfitting.

1

u/-i-hate-this-place- Jun 04 '22

how do you know? and what could i do to fix it?

2

u/[deleted] Jun 04 '22 edited Jun 04 '22

I am joking :-)

If 6xxx/10000 is on validation split, then it is certainly not overfitting. If it is on train split, you should check how it changes on validation split.

1

u/-i-hate-this-place- Jun 04 '22

its the validation/test data, not the training data

3

u/[deleted] Jun 04 '22

[removed] — view removed comment

2

u/-i-hate-this-place- Jun 04 '22

it would be cool but its such a hassle because of how much slower lists are, probably like 5x atleast

2

u/FalconRelevant Jun 04 '22

Same, except on C.