Create a simple Neural Network from scratch using Origin C

posted in: Data Analysis, Programming | 1

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”.

NeuImg_01

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.

NeuImg_02

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.
imgtemp-cc8w0k-1

Normalize the result so that it will fall between 0 and 1, by Sigmoid function.

Sigmoid function is an S-shaped curve

SigmoidCurve

Embed the first equation into the second, we will have:

Again, in order to simply the task, we didn’t take lowest excitation threshold into consider.

 

Weight Adjustment Function

So how to modify the weighting value during the process? We can use Error Weighted Derivative method (A.K.A Delta Rule).

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

Put it into the adjustment equation, then we have

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.

 

Building your Origin C code

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):

ScriptWindow_1

you should be able to see results like:

ScriptWindow_2

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 !!!!!

One Response

  1. awesome!!!

Leave a Reply