Create a simple Neural Network from scratch using Origin C

When learning artificial intelligence, I’m always amazed by those who provide all kinds of supporting packages for Python and R and whatever programming languages people are using. However, in order to make sure I really understand how this machine thinking works, I always want to implement a neural network without using packages, totally from zero. That’s why I choose Origin C because seems like no one has done this on this platform yet and there will be definitely no 3rd party packages that I can use. Here in this blog, I will explain step by step how I did (Thanks to one of Milo Spencer-Harper’s blogs).

First, we need to understand what is Neural Network. The human brain is made of hundreds of billions of neurons. Once the synapse gets enough impulses, it will turn the current neuron on and pass the impulse to the next neuron. We call this process “think”.

We can simulate this process programmablly, not by building a brain but by simulating the high-level rules. Let’s use a 2D data table, and in order to simplify the task, we only simulate a neural network with 4 inputs and a single output.

So first, we need to train the neurons for solving unknown cases. In the following table, the first seven cases will be used as training set. Have you noticed the pattern?

 Input Output Sample 1 0 0 0 1 0 Sample 2 1 1 1 1 1 Sample 3 1 0 1 0 0 Sample 4 0 1 1 0 1 Sample 5 0 0 1 1 0 Sample 6 0 1 0 1 1 Sample 7 1 0 0 1 0 New situation 0 1 0 0 ?

Yes, the output will always follow the second input, so “?” shall be 1.

Training Process

So how can we train the network to return a good perdition? The idea is to give each input a weight, either positive or negative. Thus, the input that has the larger weight will have more impact on the output. So first we can give each neuron a random weighting value and let the network adjust them during the process:

1. Take a training sample, calculate the output based on a special function by the current weights.
2. Calculate the error, which is the difference between the sample output and the result from step1.
3. Adjust the weight slightly according to the error value.
4. Repeat step 1~3 for … ten thousand times.

Then the weighting values will finally become an optimal solution for the training sets. Now if we use it for an unknown case, it will provide a close-enough prediction. We call this process “Back Propagation“.

The function for calculating network output

You may think what will be the function that we use to calculate the output? First, let’s get the weighted sum of all neurons.

Normalize the result so that it will fall between 0 and 1, by Sigmoid function.
$\dpi{150}&space;\large&space;\frac{1}{1+e^{-x}}$

Sigmoid function is an S-shaped curve

Embed the first equation into the second, we will have:
$\dpi{150}&space;\large&space;output\;&space;of\;&space;neuron=\frac{1}{1+e^{\left&space;(&space;\sum&space;weight_{i}\cdot&space;input_{i}&space;\right&space;)}}$
Again, in order to simply the task, we didn’t take lowest excitation threshold into consider.

So how to modify the weighting value during the process? We can use Error Weighted Derivative method (A.K.A Delta Rule).
$\dpi{150}&space;\large&space;Adjust\;&space;weights\;&space;by=error\cdot&space;input\cdot&space;Sigmoid^{-1}\left&space;(&space;output&space;\right&space;)$

Why choose this equation? First, we want the adjustment to be proportional to the error. Second, when there is no input (input = 0), the current weight will not be modified. At last, we multiply it by the slope of the sigmoid curve. In order to understand the slope part, consider the following:

1. The same function (Sigmoid) is been used to calculate the output
2. If the calculated output is a large positive (or negative) value, it means that the neural network is taking this (or the opposite) decision
3. For the curve we can see that the larger the output value be, the smaller the slope is.
4. If the neural network thinks the current weight is correct (error ≈ 0), it will not be adjusted too much. By multiplying the slope will be able to implement this.

Take the derivative of sigmoid equation, we can get
$\dpi{150}&space;\large&space;Sigmoid^{-1}\left&space;(&space;output&space;\right&space;)=output\cdot&space;\left&space;(&space;1-output&space;\right&space;)$
Put it into the adjustment equation, then we have
$\dpi{150}&space;\large&space;Adjust\;&space;weights\;&space;by=error\cdot&space;input\cdot&space;output\cdot&space;\left&space;(&space;1-output&space;\right&space;)$
Of course, there are other equations you can find that have better performance, while the advantage of this equation is, it’s simple enough to understand.

Epilogue

Compile the OriginC code above and call the main function in Script Window as following (you can change the input vector to other 4-dig combinations):

you should be able to see results like:

You did it !!!!!! You’ve built a simple neural network by plain Origin C !!!!

You can see the network trained itself, considered a new case {0, 1, 0, 0} and gives its prediction 0.999998. It’s almost 1 !!!!!

Related Posts

View all posts by lkb0221 →

3 Comments on “Create a simple Neural Network from scratch using Origin C”

1. Very well explained and detailed guide.Thank you for sharing this.
Any thoughts on how to implement LSTM neurons on C?

1. Chris Drozdowski says:

Hi Anthony,

Thank you fo the feedback.

Regarding “Any thoughts on how to implement LSTM neurons on C?”- Do you mean Origin C specifically or C in general? If you mean in C in general, you would have to research that yourself. If you mean in Origin C specifically, you can email us at tech@originlab.com. We would prefer that you provide as many citations as possible, so be prepared.

2. Cheng says:

awesome!!!