Home >Web Front-end >JS Tutorial >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

黄舟
黄舟Original
2017-03-07 14:42:552918browse

Introduction to Neural Networksgraphic 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.

Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript

Neural network is divided into multiple layers, as shown in the figure above, with input layer, hidden layer and output layer.

JS Linear Algebra Packagegraphic and text explanation of simple neural network algorithm implemented in JavaScript>

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/

  • ##http://www.php.cn/

  • http://www.php.cn/

I used numericjs and the effect is pretty good. It is recommended that everyone try it.

Two-layer neural networkgraphic and text explanation of simple neural network algorithm implemented in JavaScript>We have some simple input and output data to train the neural network. Each row here represents a piece of data. The input has three parameters and the output is one.

Inputs 0Inputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptInputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptOutput00Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript0##Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript0##First we implement the simplest neural network , there is no hidden layer, the input is directly connected to the output.
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. Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript

// 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
  • y Output data
  • nonlin, S function
  • l0, the first layer of the network, which is equal to the input data
  • lDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, network The second layer, here is the output layer
  • syn0, the weight of the first layer network
  • The iterative process of training is to first give an initial The weight, use this weight to calculate an output value, subtract this value from the target result to get a difference value, and then use this difference value to correct the weight.
Network output after Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000 iterations: [0.0Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.0Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript, 0.979, 0.97Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript]

Syn0 weight value after Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript000 iterations: [7.Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript66, -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 JavaScriptDetailed 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 JavaScriptDetailed graphic and text explanation of simple neural network algorithm implemented in JavaScript5]

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

graphic and text explanation of simple neural network algorithm implemented in JavaScript>##Inputs 0Inputs Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript0Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript##Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript0Detailed 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 JavaScript0
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
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] Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript

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

Detailed graphic and text explanation of simple neural network algorithm implemented in JavaScript



#

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn