{"id":322681,"date":"2021-05-06T15:00:08","date_gmt":"2021-05-06T15:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=322681"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=322681","title":{"rendered":"\u041d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0441 \u043d\u0443\u043b\u044f \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438. \u0427\u0430\u0441\u0442\u044c 2. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041a\u0430\u043a \u044f \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432\u043e \u0432\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0438 \u043a <a href=\"https:\/\/habr.com\/ru\/post\/556076\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a>, \u044f frontend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u0438 \u043c\u043e\u0439 \u0440\u043e\u0434\u043d\u043e\u0439 \u044f\u0437\u044b\u043a &#8212; JavaScript, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043d\u0435\u043c. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0432 \u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435. \u0417\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043b\u043e\u0435\u0432 layers, \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 neurons, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0435\u0439\u0440\u043e\u043d, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u201c\u0432\u0445\u043e\u0434\u043e\u0432\u201d &#8212; \u0441\u0432\u044f\u0437\u0435\u0439 \u0441 \u043d\u0435\u0439\u0440\u043e\u043d\u0430\u043c\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f inputs. \u0422\u0430\u043a\u0436\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0435\u0449\u0438 \u043e\u0431\u0449\u0438\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0439 \u0441\u0435\u0442\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u0430\u044f \u0438 learning rate, \u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u043d\u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0438\u0437 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0443\u0441\u043b\u043e\u0432\u0438\u043c\u0441\u044f, \u0447\u0442\u043e \u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0430 _layer \u043d\u0430 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u043d \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442, \u0430 \u0443 \u0441\u043b\u043e\u044f \u0431\u0443\u0434\u0435\u0442 _network &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0439\u0434\u0435\u043c \u043e\u0442 \u0447\u0430\u0441\u0442\u043d\u043e\u0433\u043e \u043a \u043e\u0431\u0449\u0435\u043c\u0443 \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0432\u0445\u043e\u0434\u0430 \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u0430.<\/p>\n<pre><code class=\"javascript\">class Input {  constructor(neuron, weight) {    this.neuron = neuron;    this.weight = weight;  } }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0432\u0441\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0432\u0435\u0441 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043d\u0435\u0439\u0440\u043e\u043d \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f. \u0418\u0434\u0435\u043c \u0434\u0430\u043b\u0435\u0435. \u041e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430.  <\/p>\n<pre><code class=\"javascript\">class Neuron {  constructor(layer, previousLayer) {    this._layer = layer;    this.inputs = previousLayer      ? previousLayer.neurons.map((neuron) =&gt; new Input(neuron, Math.random() - 0.5))      : [0];  }   get $isFirstLayerNeuron() {    return !(this.inputs[0] instanceof Input)  }   get inputSum() {    return this.inputs.reduce((sum, input) =&gt; {      return sum + input.neuron.value * input.weight;    }, 0);  }   get value() {    return this.$isFirstLayerNeuron      ? this.inputs[0]      : this._layer._network.activationFunction(this.inputSum);  }   set input(val) {    if (!this.$isFirstLayerNeuron) {      return;    }     this.inputs[0] = val;  }   set error(error) {    if (this.$isFirstLayerNeuron) {      return;    }     const wDelta = error * this._layer._network.derivativeFunction(this.inputSum);     this.inputs.forEach((input) =&gt; {      input.weight -= input.neuron.value * wDelta * this._layer._network.learningRate;      input.neuron.error = input.weight * wDelta;    });  } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0447\u0442\u043e \u0442\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430: \u0441\u043b\u043e\u0439, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044d\u0442\u043e\u0442 \u043d\u0435\u0439\u0440\u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438, \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043b\u043e\u0439.<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0432\u044f\u0436\u0435\u0442 \u043d\u0435\u0439\u0440\u043e\u043d\u044b \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0432\u0435\u0441, \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0432\u0441\u0435 \u0432\u0445\u043e\u0434\u044b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 inputs. \u0415\u0441\u043b\u0438 \u0436\u0435 \u044d\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 \u0441\u0435\u0442\u0438, \u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 inputs \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u043d\u0430 \u0432\u0445\u043e\u0434.<\/p>\n<p><strong>$isFirstLayerNeuron<\/strong> &#8212; \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0444\u043b\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u0439\u0440\u043e\u043d \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u043e\u044f\u0445, \u043a\u0440\u043e\u043c\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e, \u0442\u0430\u043a \u0447\u0442\u043e \u043e\u043d \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<p><strong>inputSum <\/strong>&#8212; readonly \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u0443\u043c\u043c\u0443 \u0432\u0441\u0435\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f) \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u043a \u043d\u0438\u043c \u0432\u0435\u0441\u0430\u043c\u0438.<\/p>\n<p><strong>value <\/strong>&#8212; \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u0430. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043b\u043e\u0435\u0432, \u043a\u0440\u043e\u043c\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442 inputSum.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u0434\u0432\u0430 \u0441\u0435\u0442\u0442\u0435\u0440\u0430:<\/p>\n<p><strong>input <\/strong>&#8212; \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>\u0418 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 &#8212; \u0441\u0435\u0442\u0442\u0435\u0440 <strong>error<\/strong>. \u0412 \u043d\u0435\u043c \u043c\u044b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0434\u0435\u043b\u044c\u0442\u0443 \u0432\u0435\u0441\u043e\u0432, \u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c error \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0441\u0435\u0442\u0438, \u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 \u0432\u0435\u0441\u043e\u0432 \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043b\u0430\u0441\u0441 \u0441\u043b\u043e\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0449\u0435.<\/p>\n<pre><code class=\"javascript\">class Layer {  constructor(neuronsCount, previousLayer, network) {    this._network = network;    this.neurons = [];    for (let i = 0; i &lt; neuronsCount; i++) {      this.neurons.push(new Neuron(this, previousLayer));    }  }   get $isFirstLayer() {    return this.neurons[0].$isFirstLayerNeuron;  }   set input(val) {    if (!this.$isFirstLayer) {      return;    }     if (!Array.isArray(val)) {      return;    }     if (val.length !== this.neurons.length) {      return;    }     val.forEach((v, i) =&gt; this.neurons[i].input = v);  } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432, \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043d\u0438\u043c \u043c\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 neurons \u0441\u043b\u043e\u044f, \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0435 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <strong>$isFirstLayer<\/strong> &#8212; \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0444\u043b\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043b\u0438 \u044d\u0442\u043e \u0441\u043b\u043e\u0439, \u0438 \u0441\u0435\u0442\u0442\u0435\u0440 input, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u043d\u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435 \u0438 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0443\u0436\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439.<\/p>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438  <\/p>\n<pre><code class=\"javascript\">class Network {  static  sigmoid(x) {    return 1 \/ (1 + Math.exp(-x));  }   static sigmoidDerivative(x) {    return Network.sigmoid(x) * (1 - Network.sigmoid(x));  }   constructor(inputSize, outputSize, hiddenLayersCount = 1, learningRate = 0.5) {    this.activationFunction = Network.sigmoid;    this.derivativeFunction = Network.sigmoidDerivative;    this.learningRate = learningRate;     this.layers = [new Layer(inputSize, null, this)];     for (let i = 0; i &lt; hiddenLayersCount; i++) {      const layerSize = Math.min(inputSize * 2 - 1, Math.ceil((inputSize * 2 \/ 3) + outputSize));      this.layers.push(new Layer(layerSize, this.layers[this.layers.length - 1], this));    }     this.layers.push(new Layer(outputSize, this.layers[this.layers.length - 1], this));  }   set input(val) {    this.layers[0].input = val;  }   get prediction() {    return this.layers[this.layers.length - 1].neurons.map((neuron) =&gt; neuron.value);  }   trainOnce(dataSet) {    if (!Array.isArray(dataSet)) {      return;    }     dataSet.forEach((dataCase) =&gt; {      const [input, expected] = dataCase;       this.input = input;      this.prediction.forEach((r, i) =&gt; {        this.layers[this.layers.length - 1].neurons[i].error = r - expected[i];      });    });  }   train(dataSet, epochs = 100000) {    return new Promise(resolve =&gt; {      for (let i = 0; i &lt; epochs; i++) {        this.trainOnce(dataSet);      }      resolve();    });  } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0435\u0432, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u0438 learning rate.<\/p>\n<p>\u0421\u0435\u0442\u0442\u0435\u0440 <strong>input <\/strong>&#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0430\u0445\u0430\u0440, \u043f\u043e \u0441\u0443\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0442\u0442\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <strong>prediction <\/strong>&#8212; \u0442\u043e, \u0440\u0430\u0434\u0438 \u0447\u0435\u0433\u043e \u0432\u0441\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u043b\u043e\u0441\u044c. \u0412\u044b\u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>trainOnce <\/strong>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <strong>dataset <\/strong>&#8212; \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u0433\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0438\u0434\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0432\u0442\u043e\u0440\u044b\u043c &#8212; \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u043d\u0438\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0418\u0437\u0432\u0438\u043d\u044f\u044e\u0441\u044c, \u0435\u0441\u043b\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0412 \u043d\u0435\u043c \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u043c \u0435\u0435, \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u044f \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 \u0432\u0435\u0441\u043e\u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>train <\/strong>&#8212; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0435\u0442\u0438, \u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u044d\u043f\u043e\u0445. \u041e\u0444\u043e\u0440\u043c\u043b\u0435\u043d \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u043c\u0438\u0441\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u043d\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e .then, \u0445\u043e\u0442\u044c \u0432\u0435\u0441\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0438 \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 main thread.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0432 \u0434\u0435\u043b\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u0441\u0435\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e XOR.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0441\u0435\u0442\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u043c\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 \u043e\u0434\u043d\u0438\u043c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c.<\/p>\n<pre><code class=\"javascript\">const network = new Network(2, 1);<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0434\u0438\u043c \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"javascript\">const data = [  [[0, 0], [0]],  [[0, 1], [1]],  [[1, 0], [1]],  [[1, 1], [0]], ]; <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.  <\/p>\n<pre><code class=\"javascript\">network.train(data).then(() =&gt; {  const testData = [    [0, 0],    [0, 1],    [1, 0],    [1, 1],  ];   testData.forEach((input, index) =&gt; {    network.input = input;    console.log(`${input[0]} XOR ${input[1]} = ${network.prediction}`)  }); });<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/de5\/216\/f24\/de5216f24a9e69dced23815b1d85b99a.jpg\" width=\"425\" height=\"113\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e \u0436\u0435, \u043d\u0430\u0448\u0430 \u0441\u0435\u0442\u044c \u0434\u0430\u0435\u0442 \u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u043b\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0435 \u043f\u0440\u044f\u043c\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0438 \u043d\u0430\u0439\u0434\u0435\u043c \u0435\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435.  <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/556180\/\"> https:\/\/habr.com\/ru\/post\/556180\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041a\u0430\u043a \u044f \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0432\u043e \u0432\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0438 \u043a <a href=\"https:\/\/habr.com\/ru\/post\/556076\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438<\/a>, \u044f frontend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u0438 \u043c\u043e\u0439 \u0440\u043e\u0434\u043d\u043e\u0439 \u044f\u0437\u044b\u043a &#8212; JavaScript, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043d\u0435\u043c. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0432 \u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435. \u0417\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043b\u043e\u0435\u0432 layers, \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 neurons, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0435\u0439\u0440\u043e\u043d, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u201c\u0432\u0445\u043e\u0434\u043e\u0432\u201d &#8212; \u0441\u0432\u044f\u0437\u0435\u0439 \u0441 \u043d\u0435\u0439\u0440\u043e\u043d\u0430\u043c\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f inputs. \u0422\u0430\u043a\u0436\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0435\u0449\u0438 \u043e\u0431\u0449\u0438\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0439 \u0441\u0435\u0442\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u0430\u044f \u0438 learning rate, \u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u043d\u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0438\u0437 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0443\u0441\u043b\u043e\u0432\u0438\u043c\u0441\u044f, \u0447\u0442\u043e \u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0430 _layer \u043d\u0430 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u043d \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442, \u0430 \u0443 \u0441\u043b\u043e\u044f \u0431\u0443\u0434\u0435\u0442 _network &#8212; \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0439\u0434\u0435\u043c \u043e\u0442 \u0447\u0430\u0441\u0442\u043d\u043e\u0433\u043e \u043a \u043e\u0431\u0449\u0435\u043c\u0443 \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0432\u0445\u043e\u0434\u0430 \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u0430.<\/p>\n<pre><code class=\"javascript\">class Input {  constructor(neuron, weight) {    this.neuron = neuron;    this.weight = weight;  } }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0432\u0441\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0432\u0435\u0441 \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043d\u0435\u0439\u0440\u043e\u043d \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f. \u0418\u0434\u0435\u043c \u0434\u0430\u043b\u0435\u0435. \u041e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430.  <\/p>\n<pre><code class=\"javascript\">class Neuron {  constructor(layer, previousLayer) {    this._layer = layer;    this.inputs = previousLayer      ? previousLayer.neurons.map((neuron) =&gt; new Input(neuron, Math.random() - 0.5))      : [0];  }   get $isFirstLayerNeuron() {    return !(this.inputs[0] instanceof Input)  }   get inputSum() {    return this.inputs.reduce((sum, input) =&gt; {      return sum + input.neuron.value * input.weight;    }, 0);  }   get value() {    return this.$isFirstLayerNeuron      ? this.inputs[0]      : this._layer._network.activationFunction(this.inputSum);  }   set input(val) {    if (!this.$isFirstLayerNeuron) {      return;    }     this.inputs[0] = val;  }   set error(error) {    if (this.$isFirstLayerNeuron) {      return;    }     const wDelta = error * this._layer._network.derivativeFunction(this.inputSum);     this.inputs.forEach((input) =&gt; {      input.weight -= input.neuron.value * wDelta * this._layer._network.learningRate;      input.neuron.error = input.weight * wDelta;    });  } }<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0447\u0442\u043e \u0442\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430: \u0441\u043b\u043e\u0439, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044d\u0442\u043e\u0442 \u043d\u0435\u0439\u0440\u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438, \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043b\u043e\u0439.<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0432\u044f\u0436\u0435\u0442 \u043d\u0435\u0439\u0440\u043e\u043d\u044b \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0432\u0435\u0441, \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0432\u0441\u0435 \u0432\u0445\u043e\u0434\u044b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 inputs. \u0415\u0441\u043b\u0438 \u0436\u0435 \u044d\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 \u0441\u0435\u0442\u0438, \u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 inputs \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u043d\u0430 \u0432\u0445\u043e\u0434.<\/p>\n<p><strong>$isFirstLayerNeuron<\/strong> &#8212; \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0444\u043b\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u0439\u0440\u043e\u043d \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u043e\u044f\u0445, \u043a\u0440\u043e\u043c\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e, \u0442\u0430\u043a \u0447\u0442\u043e \u043e\u043d \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<p><strong>inputSum <\/strong>&#8212; readonly \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u0443\u043c\u043c\u0443 \u0432\u0441\u0435\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f) \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u043a \u043d\u0438\u043c \u0432\u0435\u0441\u0430\u043c\u0438.<\/p>\n<p><strong>value <\/strong>&#8212; \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u0430. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043b\u043e\u0435\u0432, \u043a\u0440\u043e\u043c\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442 inputSum.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u0434\u0432\u0430 \u0441\u0435\u0442\u0442\u0435\u0440\u0430:<\/p>\n<p><strong>input <\/strong>&#8212; \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>\u0418 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 &#8212; \u0441\u0435\u0442\u0442\u0435\u0440 <strong>error<\/strong>. \u0412 \u043d\u0435\u043c \u043c\u044b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0434\u0435\u043b\u044c\u0442\u0443 \u0432\u0435\u0441\u043e\u0432, \u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430 \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c error \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0441\u0435\u0442\u0438, \u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 \u0432\u0435\u0441\u043e\u0432 \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043b\u0430\u0441\u0441 \u0441\u043b\u043e\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438. \u0422\u0443\u0442 \u0432\u0441\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0449\u0435.<\/p>\n<pre><code class=\"javascript\">class Layer {  constructor(neuronsCount, previousLayer, network) {    this._network = network;    this.neurons = [];    for (let i = 0; i &lt; neuronsCount; i++) {      this.neurons.push(new Neuron(this, previousLayer));    }  }   get $isFirstLayer() {    return this.neurons[0].$isFirstLayerNeuron;  }   set input(val) {    if (!this.$isFirstLayer) {      return;    }     if (!Array.isArray(val)) {      return;    }     if (val.length !== this.neurons.length) {      return;    }     val.forEach((v, i) =&gt; this.neurons[i].input = v);  } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432, \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043d\u0438\u043c \u043c\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 neurons \u0441\u043b\u043e\u044f, \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0435 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0438 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0430\u043c\u0443 \u0441\u0435\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <strong>$isFirstLayer<\/strong> &#8212; \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0444\u043b\u0430\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043b\u0438 \u044d\u0442\u043e \u0441\u043b\u043e\u0439, \u0438 \u0441\u0435\u0442\u0442\u0435\u0440 input, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u043d\u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435 \u0438 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0443\u0436\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439.<\/p>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438  <\/p>\n<pre><code class=\"javascript\">class Network {  static  sigmoid(x) {    return 1 \/ (1 + Math.exp(-x));  }   static sigmoidDerivative(x) {    return Network.sigmoid(x) * (1 - Network.sigmoid(x));  }   constructor(inputSize, outputSize, hiddenLayersCount = 1, learningRate = 0.5) {    this.activationFunction = Network.sigmoid;    this.derivativeFunction = Network.sigmoidDerivative;    this.learningRate = learningRate;     this.layers = [new Layer(inputSize, null, this)];     for (let i = 0; i &lt; hiddenLayersCount; i++) {      const layerSize = Math.min(inputSize * 2 - 1, Math.ceil((inputSize * 2 \/ 3) + outputSize));      this.layers.push(new Layer(layerSize, this.layers[this.layers.length - 1], this));    }     this.layers.push(new Layer(outputSize, this.layers[this.layers.length - 1], this));  }   set input(val) {    this.layers[0].input = val;  }   get prediction() {    return this.layers[this.layers.length - 1].neurons.map((neuron) =&gt; neuron.value);  }   trainOnce(dataSet) {    if (!Array.isArray(dataSet)) {      return;    }     dataSet.forEach((dataCase) =&gt; {      const [input, expected] = dataCase;       this.input = input;      this.prediction.forEach((r, i) =&gt; {        this.layers[this.layers.length - 1].neurons[i].error = r - expected[i];      });    });  }   train(dataSet, epochs = 100000) {    return new Promise(resolve =&gt; {      for (let i = 0; i &lt; epochs; i++) {        this.trainOnce(dataSet);      }      resolve();    });  } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0435\u0432, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u0438 learning rate.<\/p>\n<p>\u0421\u0435\u0442\u0442\u0435\u0440 <strong>input <\/strong>&#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0430\u0445\u0430\u0440, \u043f\u043e \u0441\u0443\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u0442\u0442\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <strong>prediction <\/strong>&#8212; \u0442\u043e, \u0440\u0430\u0434\u0438 \u0447\u0435\u0433\u043e \u0432\u0441\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u043b\u043e\u0441\u044c. \u0412\u044b\u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>trainOnce <\/strong>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <strong>dataset <\/strong>&#8212; \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u0433\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0438\u0434\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0432\u0442\u043e\u0440\u044b\u043c &#8212; \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u043d\u0438\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0418\u0437\u0432\u0438\u043d\u044f\u044e\u0441\u044c, \u0435\u0441\u043b\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435, \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0412 \u043d\u0435\u043c \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u043c \u0435\u0435, \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u044f \u043f\u0435\u0440\u0435\u0441\u0447\u0435\u0442 \u0432\u0435\u0441\u043e\u0432 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0435\u0439.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong>train <\/strong>&#8212; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0435\u0442\u0438, \u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u044d\u043f\u043e\u0445. \u041e\u0444\u043e\u0440\u043c\u043b\u0435\u043d \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u043c\u0438\u0441\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u043d\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e .then, \u0445\u043e\u0442\u044c \u0432\u0435\u0441\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0438 \u0442\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432 main thread.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0432 \u0434\u0435\u043b\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 &#8212; \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u0441\u0435\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e XOR.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0441\u0435\u0442\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u043c\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 \u043e\u0434\u043d\u0438\u043c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c.<\/p>\n<pre><code class=\"javascript\">const network = new Network(2, 1);<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0434\u0438\u043c \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"javascript\">const data = [  [[0, 0], [0]],  [[0, 1], [1]],  [[1, 0], [1]],  [[1, 1], [0]], ]; <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.  <\/p>\n<pre><code class=\"javascript\">network.train(data).then(() =&gt; {  const testData = [    [0, 0],    [0, 1],    [1, 0],    [1, 1],  ];   testData.forEach((input, index) =&gt; {    network.input = input;    console.log(`${input[0]} XOR ${input[1]} = ${network.prediction}`)  }); });<\/code><\/pre>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e \u0436\u0435, \u043d\u0430\u0448\u0430 \u0441\u0435\u0442\u044c \u0434\u0430\u0435\u0442 \u0432\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u043b\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0435 \u043f\u0440\u044f\u043c\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0438 \u043d\u0430\u0439\u0434\u0435\u043c \u0435\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435.  <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/556180\/\"> https:\/\/habr.com\/ru\/post\/556180\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-322681","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322681"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322681\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}