Home > Article > Web Front-end > Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript
Neural networks attempt to simulate the relationship between neurons in the brain to process information. Its computational model usually requires a large number of nodes connected to each other. Each neuron processes weighted input values from other neighboring neurons through a special output function.
The intensity of information transmission between neurons is defined by the so-called weighted value. The algorithm will continuously adjust the weighted value to realize its own learning process.
Neural network is divided into multiple layers, as shown in the figure above, with input layer, hidden layer and output layer.
The calculation of neural network involves a large number of matrix calculations. There are many open source software for linear algebra. The famous numpy under Python is very famous. There are also several Javascripts:
http://www.php.cn/
Inputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Inputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Output | |
---|---|---|---|
0 | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | 0 | |
Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | |
0 | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | |
Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | 0 |
Because the input is three parameters and the output is one, our neural network input layer has three nodes and the output is one.
// Sigmod function function nonlin(x, deriv) { if (deriv) { return numeric.mul(x, numeric.sub(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, x)); } return numeric.p(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.add(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.exp(numeric.neg(x)))); } function train_neural(X, y, iteration) { // initialize weights var syn0 = numeric.sub(numeric.mul(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.random([Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript])), Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript); //Training loop var i = 0; for (; i < iteration; i++) { var l0 = X; var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript = nonlin(numeric.dot(l0, syn0)); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error = numeric.sub(y, lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta = numeric.mul(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error, nonlin(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, true)); syn0 = numeric.add(syn0, numeric.dot(numeric.transpose(l0), lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta)); } } } //Initial input/ouput values var X = [ [0, 0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript] ]; var y = [ [0], [0], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript] ]; train_neural(X, y, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000);Briefly introduce the training code and process X Input data
Here we find that the weight of the first node is larger, which is consistent with our data. By observing the data, we can also find that the output value is strongly correlated with the input value of the first column. If the number of iterations is increased, this value will be larger. Three-layer neural network
Inputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Output | 0 | |
---|---|---|---|
Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | 0 | 0 | |
Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | ##Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | 0 |
Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript | |
0 | Now we have a new set of data. Through observation, we found that the third column has nothing to do with the result. When the first column and the second column are the same, the result is 0, otherwise it is Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript. This is a non-linear relationship. In order to effectively learn, we add a layer, and the network becomes like this. |
// Sigmod function function nonlin(x, deriv) { if (deriv) { return numeric.mul(x, numeric.sub(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, x)); } return numeric.p(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.add(Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.exp(numeric.neg(x)))); } function train_neural(X, y, iteration) { // initialize weights var syn0 = [ [-0.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript65Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript90Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript7Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript7966, -0.7Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript9Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript6Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, -0.60Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript7970Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [0.60Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript89Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.9Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript65Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript5, -0.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript7Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript5Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript6Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript8Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript6Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript5Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [0.75Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript778Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.789Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, -0.8Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript99Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript58, -0.9Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript890Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript] ]; var synDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript = [ [-0.660Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript9Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript6], [0.756Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript850Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [-0.80Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript06Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [-0.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript5778Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript75] ]; //Training loop var i = 0; for (; i < Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000; i++) { var l0 = X; var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript = nonlin(numeric.dot(l0, syn0)); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript = nonlin(numeric.dot(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, synDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript)); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error = numeric.sub(y, lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta = numeric.mul(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error, nonlin(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, true)); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error = numeric.dot(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta, numeric.transpose(synDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript)); var lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta = numeric.mul(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_error, nonlin(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, true)); synDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript = numeric.add(synDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, numeric.dot(numeric.transpose(lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript), lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta)); syn0 = numeric.add(syn0, numeric.dot(numeric.transpose(l0), lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript_delta)); } } //Initial input/output values var X = [ [0, 0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript] ]; var y = [ [0], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript], [0] ]; train_neural(X, y, Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000);
The training process is not much different from the previous two layers, there is just one more layer. By adding this layer, complex nonlinear relationships in the data can be effectively learned.
After Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000 iterations, the output value is: [0.0Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.95, 0.9Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.05]
syn0:The above is JavaScript Implementation of a simple neural network algorithm with detailed graphics and text. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!