{"id":379636,"date":"2024-06-25T03:17:08","date_gmt":"2024-06-25T03:17:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=379636"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=379636","title":{"rendered":"<span>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c Backpropagation \u043d\u0430 Python<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f78\/4d7\/ba8\/f784d7ba8f128212225e92db1d56e2c7.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f78\/4d7\/ba8\/f784d7ba8f128212225e92db1d56e2c7.png\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c <strong>backpropagation<\/strong>, \u0438\u043b\u0438 <strong>\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438,<\/strong> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u044b\u0445 \u043f\u0435\u0440\u0446\u0435\u043f\u0442\u0440\u043e\u043d\u043e\u0432 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439. \u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d \u041f\u043e\u043b\u0435\u043c \u0412\u0435\u0440\u0431\u043e\u0441\u043e\u043c \u0432 1974 \u0433\u043e\u0434\u0443, \u0430 \u043f\u043e\u0437\u0436\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0414\u044d\u0432\u0438\u0434\u043e\u043c \u0420\u0443\u043c\u0435\u043b\u044c\u0445\u0430\u0440\u0442\u043e\u043c, \u0414\u0436\u0435\u0444\u0444\u0440\u0438 \u0425\u0438\u043d\u0442\u043e\u043d\u043e\u043c \u0438 \u0420\u043e\u043d\u0430\u043b\u044c\u0434\u043e\u043c \u0423\u0438\u043b\u044c\u044f\u043c\u0441\u043e\u043c \u0432 1986 \u0433\u043e\u0434\u0443.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430 \u041f\u0438\u0442\u043e\u043d\u0435.<\/p>\n<h4>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/h4>\n<p>Backpropagation \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0444\u0430\u0437: <em>forward propagation<\/em> (\u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435) \u0438 <em>backward propagation<\/em> (\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438).<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0435 \u0441\u043b\u043e\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430. \u041a\u0430\u0436\u0434\u044b\u0439 \u043d\u0435\u0439\u0440\u043e\u043d \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u0430\u043a\u043e\u0439 \u043a\u0430\u043a \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430 \u0438\u043b\u0438 ReLU.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u2014 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u0432\u044b\u0445\u043e\u0434\u044b \u0441\u0435\u0442\u0438), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (\u043c\u0435\u0442\u043a\u0430\u043c\u0438) \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0435\u0441\u0430 \u0441\u0435\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0438 \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430, \u0432\u0435\u0441\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443. \u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u044b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0432\u0435\u0441\u0430\u0445 \u043f\u043e\u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u0438\u0442\u043e\u0433\u043e\u0432\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Backpropagation \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u043c \u0441\u0435\u0442\u044f\u043c \u043e\u0431\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044f \u0441\u0432\u043e\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439.<\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 Python  <\/h3>\n<h4>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u043d\u044b, \u0441\u043b\u043e\u0438 \u0438 \u0441\u0435\u0442\u044c. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u043e\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043d\u0435\u0439\u0440\u043e\u043d\u044b, \u0430 \u0441\u0435\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Neuron<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0435\u0433\u043e \u0432\u0435\u0441\u0430 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np  class Neuron:     def __init__(self, input_size):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]  # \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u0432\u0445\u043e\u0434\u043e\u0432 \u0438 \u0432\u0435\u0441\u043e\u0432         return self.sigmoid(z)          def sigmoid(self, x):         return 1 \/ (1 + np.exp(-x))          def sigmoid_derivative(self, x):         return x * (1 - x)<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Layer<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u0441\u043b\u043e\u0435:<\/p>\n<pre><code class=\"python\">class Layer:     def __init__(self, num_neurons, input_size):         self.neurons = [Neuron(input_size) for _ in range(num_neurons)]          def forward(self, inputs):         return np.array([neuron.activate(inputs) for neuron in self.neurons])<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Network<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0435\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X          def backward(self, X, y, output, learning_rate):         # \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 (\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043d\u0438\u0436\u0435)         pass<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043b\u043e\u0438 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u043e\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u043b\u043e\u0439 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043d\u0435\u043c:<\/p>\n<pre><code class=\"python\"># \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0442\u0438 input_size = 3  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0445\u043e\u0434\u043e\u0432 hidden_size = 5  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u0441\u043a\u0440\u044b\u0442\u043e\u043c \u0441\u043b\u043e\u0435 output_size = 1  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u044b\u0445\u043e\u0434\u043e\u0432  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432 layer1 = Layer(hidden_size, input_size) layer2 = Layer(output_size, hidden_size)  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 network = Network([layer1, layer2])  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 X = np.array([0.5, 0.1, 0.4]) output = network.forward(X) print(\"Output:\", output)<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0434\u0432\u0430 \u0441\u043b\u043e\u044f \u0441\u0435\u0442\u0438: \u0441\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0432\u044b\u0445\u043e\u0434\u043e\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u044c.<\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <\/h4>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u043d\u0435\u0439\u0440\u043e\u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u0441\u0430, \u0437\u0430\u0442\u0435\u043c \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0441\u0435\u0442\u0438.<\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430, tanh \u0438 ReLU.<\/p>\n<ol>\n<li>\n<p><strong>\u0421\u0438\u0433\u043c\u043e\u0438\u0434\u0430 <\/strong><\/p>\n<p>\u0421\u0438\u0433\u043c\u043e\u0438\u0434\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043e\u0442 0 \u0434\u043e 1. \u0418\u0445 \u044e\u0437\u0430\u044e\u0442 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u043b\u043e\u044f\u0445 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">def sigmoid(x):     return 1 \/ (1 + np.exp(-x))  def sigmoid_derivative(x):     return x * (1 - x)<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Tanh<\/strong><\/p>\n<p>\u0413\u0438\u043f\u0435\u0440\u0431\u043e\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0430\u043d\u0433\u0435\u043d\u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043e\u0442 -1 \u0434\u043e 1. \u0415\u0451 \u044e\u0437\u0430\u044e\u0442 \u0432 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043b\u043e\u044f\u0445, \u0442.\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043b\u0443\u0447\u0448\u0435\u0435 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u043e\u0439:<\/p>\n<pre><code class=\"python\">def tanh(x):     return np.tanh(x)  def tanh_derivative(x):     return 1 - np.tanh(x)**2<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>ReLU<\/strong><\/p>\n<p>ReLU \u2014 \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438. \u041e\u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043d\u043e\u043b\u044c, \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"python\">def relu(x):     return np.maximum(0, x)  def relu_derivative(x):     return np.where(x > 0, 1, 0)<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u0435\u0441\u043e\u0432, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u043e\u0435 \u0441\u0435\u0442\u0438.<\/p>\n<pre><code class=\"python\">import numpy as np  class Neuron:     def __init__(self, input_size, activation='sigmoid'):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)         self.activation_function = self._get_activation_function(activation)         self.activation_derivative = self._get_activation_derivative(activation)          def _get_activation_function(self, activation):         if activation == 'sigmoid':             return lambda x: 1 \/ (1 + np.exp(-x))         elif activation == 'tanh':             return np.tanh         elif activation == 'relu':             return lambda x: np.maximum(0, x)          def _get_activation_derivative(self, activation):         if activation == 'sigmoid':             return lambda x: x * (1 - x)         elif activation == 'tanh':             return lambda x: 1 - np.tanh(x)**2         elif activation == 'relu':             return lambda x: np.where(x > 0, 1, 0)          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]         return self.activation_function(z)<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Layer<\/code> \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0442\u0430\u043a\u0438\u043c \u0436\u0435, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<pre><code class=\"python\">class Layer:     def __init__(self, num_neurons, input_size, activation='sigmoid'):         self.neurons = [Neuron(input_size, activation) for _ in range(num_neurons)]          def forward(self, inputs):         return np.array([neuron.activate(inputs) for neuron in self.neurons])<\/code><\/pre>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 <code>Network<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>forward<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0435 \u0441\u043b\u043e\u0438 \u0441\u0435\u0442\u0438:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\"># \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0442\u0438 input_size = 3 hidden_size = 5 output_size = 1  # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432 layer1 = Layer(hidden_size, input_size, activation='relu') layer2 = Layer(output_size, hidden_size, activation='sigmoid')  # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 network = Network([layer1, layer2])  # \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 X = np.array([0.5, 0.1, 0.4]) output = network.forward(X) print(\"Output:\", output)<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u043b\u043e\u044f\u043c\u0438 \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u044c.<\/p>\n<h4>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/h4>\n<p>\u041e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u044d\u0442\u0430\u043f\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><strong>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0438 \u0441\u0435\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432.<\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0448\u0438\u0431\u043a\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0438 \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0442\u0435\u0440\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, MSE.<\/p>\n<pre><code class=\"python\">def mean_squared_error(y_true, y_pred):     return np.mean((y_true - y_pred) ** 2)<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430:<\/p>\n<pre><code class=\"python\">def sigmoid_derivative(x):     return x * (1 - x)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u0432 \u0441\u0435\u0442\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0438 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442. \u0412 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 (\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043a \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443) \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0435\u0441\u0430 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432.<\/p>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430. \u0412\u0435\u0441\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443, \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0443\u044e \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0443 \u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">learning_rate = 0.1  def update_weights(weights, gradients):     return weights - learning_rate * gradients<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Network<\/code>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043c\u0435\u0442\u043e\u0434 <code>backward<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X          def backward(self, X, y, learning_rate):         output = self.forward(X)         error = y - output                  for i in reversed(range(len(self.layers))):             layer = self.layers[i]             if i == len(self.layers) - 1:                 layer.error = error                 layer.delta = layer.error * sigmoid_derivative(output)             else:                 next_layer = self.layers[i + 1]                 layer.error = np.dot(next_layer.delta, np.array([neuron.weights for neuron in next_layer.neurons]))                 layer.delta = layer.error * sigmoid_derivative(layer.output)                  for i in range(len(self.layers)):             layer = self.layers[i]             inputs = X if i == 0 else self.layers[i - 1].output             for neuron in layer.neurons:                 for j in range(len(neuron.weights) - 1):                     neuron.weights[j] += learning_rate * layer.delta[j] * inputs[j]                 neuron.weights[-1] += learning_rate * layer.delta[-1]  class Neuron:     def __init__(self, input_size, activation='sigmoid'):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)         self.activation_function = self._get_activation_function(activation)         self.activation_derivative = self._get_activation_derivative(activation)          def _get_activation_function(self, activation):         if activation == 'sigmoid':             return lambda x: 1 \/ (1 + np.exp(-x))         elif activation == 'tanh':             return np.tanh         elif activation == 'relu':             return lambda x: np.maximum(0, x)          def _get_activation_derivative(self, activation):         if activation == 'sigmoid':             return lambda x: x * (1 - x)         elif activation == 'tanh':             return lambda x: 1 - np.tanh(x)**2         elif activation == 'relu':             return lambda x: np.where(x > 0, 1, 0)          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]         self.output = self.activation_function(z)         return self.output  class Layer:     def __init__(self, num_neurons, input_size, activation='sigmoid'):         self.neurons = [Neuron(input_size, activation) for _ in range(num_neurons)]          def forward(self, inputs):         self.output = np.array([neuron.activate(inputs) for neuron in self.neurons])         return self.output<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>backward<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<ol>\n<li>\n<p><strong>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0432\u0435\u0441\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432.<\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u0430\u043a, \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import numpy as np  # \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0438\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 def sigmoid(x):     return 1 \/ (1 + np.exp(-x))  def sigmoid_derivative(x):     return x * (1 - x)  def tanh(x):     return np.tanh(x)  def tanh_derivative(x):     return 1 - np.tanh(x)**2  def relu(x):     return np.maximum(0, x)  def relu_derivative(x):     return np.where(x > 0, 1, 0)  # \u043a\u043b\u0430\u0441\u0441 Neuron class Neuron:     def __init__(self, input_size, activation='sigmoid'):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)         self.activation_function = self._get_activation_function(activation)         self.activation_derivative = self._get_activation_derivative(activation)         self.output = None          def _get_activation_function(self, activation):         if activation == 'sigmoid':             return sigmoid         elif activation == 'tanh':             return tanh         elif activation == 'relu':             return relu          def _get_activation_derivative(self, activation):         if activation == 'sigmoid':             return sigmoid_derivative         elif activation == 'tanh':             return tanh_derivative         elif activation == 'relu':             return relu_derivative          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]         self.output = self.activation_function(z)         return self.output  # \u043a\u043b\u0430\u0441\u0441 Layer class Layer:     def __init__(self, num_neurons, input_size, activation='sigmoid'):         self.neurons = [Neuron(input_size, activation) for _ in range(num_neurons)]         self.output = None         self.error = None         self.delta = None          def forward(self, inputs):         self.output = np.array([neuron.activate(inputs) for neuron in self.neurons])         return self.output  # \u043a\u043b\u0430\u0441\u0441 Network class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X          def backward(self, X, y, learning_rate):         # \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439         output = self.forward(X)                  # \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435         error = y - output         self.layers[-1].error = error         self.layers[-1].delta = error * self.layers[-1].neurons[0].activation_derivative(output)                  # \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0438         for i in reversed(range(len(self.layers) - 1)):             layer = self.layers[i]             next_layer = self.layers[i + 1]             layer.error = np.dot(next_layer.delta, np.array([neuron.weights[:-1] for neuron in next_layer.neurons]))             layer.delta = layer.error * np.array([neuron.activation_derivative(neuron.output) for neuron in layer.neurons])                  # \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432         for i in range(len(self.layers)):             layer = self.layers[i]             inputs = X if i == 0 else self.layers[i - 1].output             for j, neuron in enumerate(layer.neurons):                 for k in range(len(neuron.weights) - 1):                     neuron.weights[k] += learning_rate * layer.delta[j] * inputs[k]                 neuron.weights[-1] += learning_rate * layer.delta[j]  # \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f          def train(self, X, y, learning_rate, epochs):         for epoch in range(epochs):             for xi, yi in zip(X, y):                 self.backward(xi, yi, learning_rate)  # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 input_size = 3 hidden_size = 5 output_size = 1  layer1 = Layer(hidden_size, input_size, activation='relu') layer2 = Layer(output_size, hidden_size, activation='sigmoid')  network = Network([layer1, layer2])  # \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 X = np.array([[0.5, 0.1, 0.4], [0.9, 0.7, 0.3], [0.2, 0.8, 0.6]]) y = np.array([[1], [0], [1]])  # \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f learning_rate = 0.1 epochs = 10000  # \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 network.train(X, y, learning_rate, epochs)  # \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 for xi in X:     output = network.forward(xi)     print(\"Input:\", xi, \"Output:\", output)<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<hr\/>\n<p>\u0414\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f backpropagation \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><strong>\u0412\u044b\u0431\u043e\u0440 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438:<\/strong> \u043a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432:<\/strong> \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043f\u043e\u0445 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043e\u0447\u0435\u043d\u044c \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445:<\/strong> \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0433\u0443\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u044b OTUS \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443\u0442 \u043f\u0440\u043e \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043d\u043e\u043c\u0430\u043b\u0438\u0439 \u0438 \u043f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 ML \u043c\u043e\u0436\u043d\u043e \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u0432\u044b\u0431\u0440\u043e\u0441\u043e\u0432. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043d\u0430 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 <a href=\"https:\/\/otus.pw\/6NF7\/\">\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/816667\/\"> https:\/\/habr.com\/ru\/articles\/816667\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c <strong>backpropagation<\/strong>, \u0438\u043b\u0438 <strong>\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438,<\/strong> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0434\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u044b\u0445 \u043f\u0435\u0440\u0446\u0435\u043f\u0442\u0440\u043e\u043d\u043e\u0432 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439. \u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d \u041f\u043e\u043b\u0435\u043c \u0412\u0435\u0440\u0431\u043e\u0441\u043e\u043c \u0432 1974 \u0433\u043e\u0434\u0443, \u0430 \u043f\u043e\u0437\u0436\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0414\u044d\u0432\u0438\u0434\u043e\u043c \u0420\u0443\u043c\u0435\u043b\u044c\u0445\u0430\u0440\u0442\u043e\u043c, \u0414\u0436\u0435\u0444\u0444\u0440\u0438 \u0425\u0438\u043d\u0442\u043e\u043d\u043e\u043c \u0438 \u0420\u043e\u043d\u0430\u043b\u044c\u0434\u043e\u043c \u0423\u0438\u043b\u044c\u044f\u043c\u0441\u043e\u043c \u0432 1986 \u0433\u043e\u0434\u0443.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430 \u041f\u0438\u0442\u043e\u043d\u0435.<\/p>\n<h4>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/h4>\n<p>Backpropagation \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0444\u0430\u0437: <em>forward propagation<\/em> (\u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435) \u0438 <em>backward propagation<\/em> (\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438).<\/p>\n<ol>\n<li>\n<p><strong>\u041f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0435 \u0441\u043b\u043e\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044f \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430. \u041a\u0430\u0436\u0434\u044b\u0439 \u043d\u0435\u0439\u0440\u043e\u043d \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u0430\u043a\u043e\u0439 \u043a\u0430\u043a \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430 \u0438\u043b\u0438 ReLU.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u2014 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u0432\u044b\u0445\u043e\u0434\u044b \u0441\u0435\u0442\u0438), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (\u043c\u0435\u0442\u043a\u0430\u043c\u0438) \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0435\u0441\u0430 \u0441\u0435\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0438 \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430, \u0432\u0435\u0441\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443. \u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u044b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0432\u0435\u0441\u0430\u0445 \u043f\u043e\u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u0438\u0442\u043e\u0433\u043e\u0432\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Backpropagation \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u043c \u0441\u0435\u0442\u044f\u043c \u043e\u0431\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044f \u0441\u0432\u043e\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439.<\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 Python  <\/h3>\n<h4>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u043d\u044b, \u0441\u043b\u043e\u0438 \u0438 \u0441\u0435\u0442\u044c. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u043e\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b \u043d\u0435\u0439\u0440\u043e\u043d\u044b, \u0430 \u0441\u0435\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Neuron<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0435\u0433\u043e \u0432\u0435\u0441\u0430 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np  class Neuron:     def __init__(self, input_size):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]  # \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u0432\u0445\u043e\u0434\u043e\u0432 \u0438 \u0432\u0435\u0441\u043e\u0432         return self.sigmoid(z)          def sigmoid(self, x):         return 1 \/ (1 + np.exp(-x))          def sigmoid_derivative(self, x):         return x * (1 - x)<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Layer<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u0441\u043b\u043e\u0435:<\/p>\n<pre><code class=\"python\">class Layer:     def __init__(self, num_neurons, input_size):         self.neurons = [Neuron(input_size) for _ in range(num_neurons)]          def forward(self, inputs):         return np.array([neuron.activate(inputs) for neuron in self.neurons])<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Network<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0435\u0432 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X          def backward(self, X, y, output, learning_rate):         # \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 (\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043d\u0438\u0436\u0435)         pass<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043b\u043e\u0438 \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u043e\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u043b\u043e\u0439 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043d\u0435\u043c:<\/p>\n<pre><code class=\"python\"># \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0442\u0438 input_size = 3  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0445\u043e\u0434\u043e\u0432 hidden_size = 5  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u0441\u043a\u0440\u044b\u0442\u043e\u043c \u0441\u043b\u043e\u0435 output_size = 1  # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u044b\u0445\u043e\u0434\u043e\u0432  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432 layer1 = Layer(hidden_size, input_size) layer2 = Layer(output_size, hidden_size)  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 network = Network([layer1, layer2])  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 X = np.array([0.5, 0.1, 0.4]) output = network.forward(X) print(\"Output:\", output)<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0434\u0432\u0430 \u0441\u043b\u043e\u044f \u0441\u0435\u0442\u0438: \u0441\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0432\u044b\u0445\u043e\u0434\u043e\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u044c.<\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 <\/h4>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u043d\u0435\u0439\u0440\u043e\u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u0441\u0430, \u0437\u0430\u0442\u0435\u043c \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0441\u0435\u0442\u0438.<\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430, tanh \u0438 ReLU.<\/p>\n<ol>\n<li>\n<p><strong>\u0421\u0438\u0433\u043c\u043e\u0438\u0434\u0430 <\/strong><\/p>\n<p>\u0421\u0438\u0433\u043c\u043e\u0438\u0434\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043e\u0442 0 \u0434\u043e 1. \u0418\u0445 \u044e\u0437\u0430\u044e\u0442 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0441\u043b\u043e\u044f\u0445 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">def sigmoid(x):     return 1 \/ (1 + np.exp(-x))  def sigmoid_derivative(x):     return x * (1 - x)<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Tanh<\/strong><\/p>\n<p>\u0413\u0438\u043f\u0435\u0440\u0431\u043e\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0430\u043d\u0433\u0435\u043d\u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043e\u0442 -1 \u0434\u043e 1. \u0415\u0451 \u044e\u0437\u0430\u044e\u0442 \u0432 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043b\u043e\u044f\u0445, \u0442.\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043b\u0443\u0447\u0448\u0435\u0435 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u043e\u0439:<\/p>\n<pre><code class=\"python\">def tanh(x):     return np.tanh(x)  def tanh_derivative(x):     return 1 - np.tanh(x)**2<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>ReLU<\/strong><\/p>\n<p>ReLU \u2014 \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438. \u041e\u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043d\u043e\u043b\u044c, \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"python\">def relu(x):     return np.maximum(0, x)  def relu_derivative(x):     return np.where(x > 0, 1, 0)<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u0435\u0441\u043e\u0432, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u043e\u0435 \u0441\u0435\u0442\u0438.<\/p>\n<pre><code class=\"python\">import numpy as np  class Neuron:     def __init__(self, input_size, activation='sigmoid'):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)         self.activation_function = self._get_activation_function(activation)         self.activation_derivative = self._get_activation_derivative(activation)          def _get_activation_function(self, activation):         if activation == 'sigmoid':             return lambda x: 1 \/ (1 + np.exp(-x))         elif activation == 'tanh':             return np.tanh         elif activation == 'relu':             return lambda x: np.maximum(0, x)          def _get_activation_derivative(self, activation):         if activation == 'sigmoid':             return lambda x: x * (1 - x)         elif activation == 'tanh':             return lambda x: 1 - np.tanh(x)**2         elif activation == 'relu':             return lambda x: np.where(x > 0, 1, 0)          def activate(self, inputs):         z = np.dot(inputs, self.weights[:-1]) + self.weights[-1]         return self.activation_function(z)<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 <code>Layer<\/code> \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0442\u0430\u043a\u0438\u043c \u0436\u0435, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<pre><code class=\"python\">class Layer:     def __init__(self, num_neurons, input_size, activation='sigmoid'):         self.neurons = [Neuron(input_size, activation) for _ in range(num_neurons)]          def forward(self, inputs):         return np.array([neuron.activate(inputs) for neuron in self.neurons])<\/code><\/pre>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 <code>Network<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>forward<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0435 \u0441\u043b\u043e\u0438 \u0441\u0435\u0442\u0438:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\"># \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0435\u0442\u0438 input_size = 3 hidden_size = 5 output_size = 1  # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432 layer1 = Layer(hidden_size, input_size, activation='relu') layer2 = Layer(output_size, hidden_size, activation='sigmoid')  # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u0438 network = Network([layer1, layer2])  # \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 X = np.array([0.5, 0.1, 0.4]) output = network.forward(X) print(\"Output:\", output)<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u043b\u043e\u044f\u043c\u0438 \u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u044c.<\/p>\n<h4>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/h4>\n<p>\u041e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u044d\u0442\u0430\u043f\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><strong>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0438 \u0441\u0435\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432.<\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0448\u0438\u0431\u043a\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u043c \u0441\u043b\u043e\u0435 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0438 \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0442\u0435\u0440\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, MSE.<\/p>\n<pre><code class=\"python\">def mean_squared_error(y_true, y_pred):     return np.mean((y_true - y_pred) ** 2)<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u0430:<\/p>\n<pre><code class=\"python\">def sigmoid_derivative(x):     return x * (1 - x)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u0430 \u0432 \u0441\u0435\u0442\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0438 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442. \u0412 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 (\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043a \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443) \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0435\u0441\u0430 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432.<\/p>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430. \u0412\u0435\u0441\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443, \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0443\u044e \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0443 \u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">learning_rate = 0.1  def update_weights(weights, gradients):     return weights - learning_rate * gradients<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Network<\/code>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043c\u0435\u0442\u043e\u0434 <code>backward<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432:<\/p>\n<pre><code class=\"python\">class Network:     def __init__(self, layers):         self.layers = layers          def forward(self, X):         for layer in self.layers:             X = layer.forward(X)         return X          def backward(self, X, y, learning_rate):         output = self.forward(X)         error = y - output                  for i in reversed(range(len(self.layers))):             layer = self.layers[i]             if i == len(self.layers) - 1:                 layer.error = error                 layer.delta = layer.error * sigmoid_derivative(output)             else:                 next_layer = self.layers[i + 1]                 layer.error = np.dot(next_layer.delta, np.array([neuron.weights for neuron in next_layer.neurons]))                 layer.delta = layer.error * sigmoid_derivative(layer.output)                  for i in range(len(self.layers)):             layer = self.layers[i]             inputs = X if i == 0 else self.layers[i - 1].output             for neuron in layer.neurons:                 for j in range(len(neuron.weights) - 1):                     neuron.weights[j] += learning_rate * layer.delta[j] * inputs[j]                 neuron.weights[-1] += learning_rate * layer.delta[-1]  class Neuron:     def __init__(self, input_size, activation='sigmoid'):         self.weights = np.random.randn(input_size + 1) * 0.1  # +1 \u0434\u043b\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (bias)         self.activation_function = self._get_activation_function(activation)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-379636","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379636","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=379636"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379636\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}