{"id":368156,"date":"2024-05-21T03:36:42","date_gmt":"2024-05-21T03:36:42","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=368156"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T03:36:42","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=368156","title":{"rendered":"<span>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0435\u0432 \u0432 \u041d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445 (\u0447\u0430\u0441\u0442\u044c 1)<\/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<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <strong>Sequential<\/strong> \u0438 \u0441\u043b\u043e\u0451\u0432 <strong>Dense<\/strong> \u0438 <strong>Input<\/strong> \u0447\u0435\u0440\u0435\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Numpy \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u043e\u0439 NN.<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430\u0446\u0435\u043b\u0435\u043d\u0430 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u0435\u0432 \u0441 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u043c \u0442\u0435\u043e\u0440\u0438\u0438, \u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0442\u0435\u043e\u0440\u0438\u0435\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439.  <\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a:<\/p>\n<pre><code class=\"python\">import numpy as np<\/code><\/pre>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Dense \u0441\u043b\u043e\u044f<\/h3>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/ad3\/227\/ff2\/ad3227ff22cb97130ee7cc110e602ff1.jpeg\" alt=\"Dense-\u0441\u043b\u043e\u0439\" title=\"Dense-\u0441\u043b\u043e\u0439\" width=\"1500\" height=\"712\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad3\/227\/ff2\/ad3227ff22cb97130ee7cc110e602ff1.jpeg\" data-blurred=\"true\"\/><\/p>\n<div><figcaption>Dense-\u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"python\">class DenseLayer():     def __init__(self,  units=1, activation='relu', weights=np.array([]), b=np.array([])):         self.units = units         self.fl_init = True         self.activation = activation         self.weights = weights         self.b_new = b         self.w, self.b = np.array([]), np.array([])<\/code><\/pre>\n<p>\u041f\u043e\u044f\u0441\u043d\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<ul>\n<li>\n<p><em>units<\/em> &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 <\/p>\n<\/li>\n<li>\n<p><em>activation &#8212; <\/em>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p><em>weights<\/em> \u0438 <em>b_new<\/em> &#8212; \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438<\/p>\n<\/li>\n<li>\n<p><em>w<\/em>,<em> b<\/em> &#8212; \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p><em>fl_init <\/em>&#8212;<em> <\/em>\u0444\u043b\u0430\u0433, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043b\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c call, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 \u0432 <a href=\"https:\/\/pylot.me\/article\/g8N-functors-in-python\/?ysclid=lsgmpjzheg673075891#\" rel=\"noopener noreferrer nofollow\">\u0444\u0443\u043d\u043a\u0442\u043e\u0440<\/a>:<\/p>\n<pre><code class=\"python\">    def __call__(self, x):         if (self.fl_init == True) and (self.weights.shape[0] == 0):             self.w = np.random.normal(loc=0.0, scale=1.0, size=(x.shape[-1], self.units))\/np.sqrt(2.0\/x.shape[-1]) + 0.001             self.b = np.ones(shape=(self.units, ), dtype=np.float32)             self.fl_init = False             # print(self.w.shape, self.weights)          elif self.weights.shape[0] != 0:             self.weights = self.weights.reshape((x.shape[-1], self.units))             self.w = self.weights             self.fl_init = False              self.b_new = self.b_new.reshape((self.units, ))             self.b = self.b_new             self.fl_init = False          y = x.dot(self.w) + self.b          if self.activation == 'relu':             return np.maximum(np.zeros(shape=y.shape), y), self.w, self.b, 1, self.units, self.activation         if self.activation == 'Leaky_relu':             return np.maximum(0.01*y, y), self.w, self.b, 1, self.units, self.activation         if self.activation == 'softmax':             return np.exp(y)\/np.sum(np.exp(y), axis=0), self.w, self.b, 1, self.units, self.activation         if self.activation == 'sigmoid':             return 1 \/ (1 + np.exp(-y)), self.w, self.b, 1, self.units, self.activation         if self.activation == 'tanh':             return (np.exp(2*y) - 1)\/(np.exp(2*y) + 1), self.w, self.b, 1, self.units, self.activation         if self.activation == 'linear':             return y, self.w, self.b, 1, self.units, self.activation<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u0430\u043a\u043e\u0439:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0447\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0435\u0449\u0435 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u043d\u0430\u043c \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u043b\u0438 \u0443\u0436\u0435 \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c (\u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 = 0, \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f = 1), \u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0451\u043c \u043a\u0430\u043a \u0435\u0434\u0438\u043d\u0438\u0446\u044b -> \u0424\u043b\u0430\u0433 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 False.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b, \u0442\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u043d\u0430 \u043d\u0438\u0445.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c<strong> y<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0435\u0451 \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 (\u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u0430 &#8212; \u0442\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0447\u0435\u0440\u0435\u0437 &#8216;<em>relu<\/em>&#8216;)<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 Input<\/strong>:<\/p>\n<pre><code class=\"python\">class Input():     def __init__(self, shape=None):         self.shape = shape      def __call__(self, x):         if self.shape is not None:             if x.shape != self.shape:                 return x.reshape(shape=self.shape), 0             else:                 return x, 0         return x, 0<\/code><\/pre>\n<p><strong>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043c\u043e\u0434\u0443\u043b\u044f<\/strong> <strong>Sequential:<\/strong><\/p>\n<pre><code class=\"python\">class Sequential():     def __init__(self, layers):         self.layers = layers # \u0441\u043b\u043e\u0438 \u0432 NN<\/code><\/pre>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043c\u0435\u0442\u043e\u0434\u0430 <strong>fit:<\/strong><\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <em>predict<\/em>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0432\u044b\u0445\u043e\u0434\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0441\u043b\u043e\u0439 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432. \u041e\u043d\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u0430 <strong><em>\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0430<\/em><\/strong><em>(BP).<\/em><\/p>\n<pre><code class=\"python\">        def predict(x):             activations = []             predict_for_layers = []             weights = []             b_coef = []             layer_2 = []             units = []             predict = self.layers[0](x)             layer_2.append(predict[1])             predict_for_layers.append(predict[0])             for i in range(1, len(self.layers)):                 predict = self.layers[i](predict[0])                 activations.append(predict[-1])                 predict_for_layers.append(predict[0])                 weights.append(predict[1])                 b_coef.append(predict[2])                 layer_2.append(predict[3])                 units.append(predict[4])             #print(len(units))              return predict_for_layers, activations, weights, b_coef, layer_2, units<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\">        def sigmoid_gradient(output):             return output * (1 - output)          def tanh_gradient(out):             return 1\/((np.exp(out) + np.exp(-out)\/2)**2)          def relu_gradient(x):             return (x > 0) * 1          def leaky_relu_gradient(x):             return (x > 0) * 1 + (x &lt;= 0) * 0.01          def linear_gradient(x):             return 1<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u0430\u043c\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <strong>Backpropagation<\/strong><em>:<\/em><\/p>\n<pre><code class=\"python\">list_back = self.layers[::-1] for elem in range(x_input.shape[0]):   x, y = x_input[elem].reshape(1, -1), y_input[elem]   for epoch in range(epochs):     predict_layers = predict(x) # 1 - y, 2 - w, 3 - b, 4 - \u0441\u043b\u043e\u0439, 5 - \u043a\u043e\u043b. \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432     predict_for_layers, activations, weights, b_coef, layers = predict_layers[0][::-1], predict_layers[1][::-1], predict_layers[2][::-1], predict_layers[3][::-1], predict_layers[4]     units = predict_layers[5]     layer_error = predict_for_layers[0] - y     if len(layer_error.shape) == 1:       layer_error = layer_error.reshape(1, -1)     for ind in range(len(list_back) - 1):       delta_weights = 0       if activations[ind] == 'linear':         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])       if activations[ind] == 'Leaky_relu':         delta_weights = layer_error * leaky_relu_gradient(predict_for_layers[ind])       if activations[ind] == 'relu':         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])       if activations[ind] == 'sigmoid':         delta_weights = layer_error * sigmoid_gradient(predict_for_layers[ind])       if activations[ind] == 'tanh':         delta_weights = layer_error * tanh_gradient(predict_for_layers[ind])        b_coef[ind] -= alpha * (np.full(b_coef[ind].shape, layer_error.sum()))       layer_error = delta_weights.dot(np.transpose(weights[ind]))       weights[ind] -= alpha * (np.transpose(predict_for_layers[ind + 1]).dot(delta_weights))      weights_inp = weights[::-1]     b_inp = b_coef[::-1]     activations_inp = activations[::-1]      for indx in range(1, len(self.layers)):       if layers[indx] == 1:         self.layers[indx] = DenseLayer(units=units[indx - 1], weights=weights_inp[indx - 1], b=b_inp[indx - 1], activation=activations_inp[indx - 1]) <\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0443 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b:<\/p>\n<ul>\n<li>\n<p>\u0412 \u0446\u0438\u043a\u043b\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u044d\u043f\u043e\u0445:<\/p>\n<ol>\n<li>\n<p>\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 &#8212; label + input.<\/p>\n<\/li>\n<li>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>predict<\/em> \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0445\u043e\u0434 (\u0441\u043f\u0438\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0441\u043b\u043e\u044f).<\/p>\n<\/li>\n<li>\n<p>\u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043b\u043e\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>delta_weights<\/strong> &#8212; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u0430\u044f, \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u0430\u044f \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0430\u043c &#8212; (\u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439, \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0445 \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e. \u0415\u0441\u043b\u0438 \u043d\u0430\u043a\u043b\u043e\u043d \u043a\u0430\u0441\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u0438\u043d\u0438\u0438(\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439) \u0431\u044b\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0442\u043e \u0432 \u0441\u0435\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435, \u043b\u0438\u0431\u043e \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435) &#8212; \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c <strong><em>layer_error<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0441\u0430 \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/afa\/5f0\/bf0\/afa5f0bf0745ae63c566d7e6199c0bdb.png\" alt=\"\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432 NN\" title=\"\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432 NN\" width=\"260\" height=\"136\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/afa\/5f0\/bf0\/afa5f0bf0745ae63c566d7e6199c0bdb.png\"\/><\/p>\n<div><figcaption><em>\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432 NN<\/em><\/figcaption><\/div>\n<\/figure>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u043b\u043e\u044f\u043c \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0438\u0445 \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <em>predict <\/em>\u0431\u0435\u0437 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0445\u043e\u0434\u043e\u0432:<\/p>\n<pre><code class=\"python\">    def predict(self, x):         predict = self.layers[0](x)         for i in range(1, len(self.layers)):             predict = self.layers[i](predict[0])          return predict<\/code><\/pre>\n<p><strong>\u041a\u043e\u0434 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e<\/strong>:<\/p>\n<pre><code class=\"python\">class Sequential():     def __init__(self, layers):         self.layers = layers      def fit(self, x_input, y_input, epochs=50, alpha=0.01):          def predict(x):             activations = []             predict_for_layers = []             weights = []             b_coef = []             layer_2 = []             units = []             predict = self.layers[0](x)             layer_2.append(predict[1])             predict_for_layers.append(predict[0])             for i in range(1, len(self.layers)):                 predict = self.layers[i](predict[0])                 activations.append(predict[-1])                 predict_for_layers.append(predict[0])                 weights.append(predict[1])                 b_coef.append(predict[2])                 layer_2.append(predict[3])                 units.append(predict[4])              return predict_for_layers, activations, weights, b_coef, layer_2, units                     def sigmoid_gradient(output):             return output * (1 - output)          def tanh_gradient(out):             return 1\/((np.exp(out) + np.exp(-out)\/2)**2)          def relu_gradient(x):             return (x > 0) * 1          def leaky_relu_gradient(x):             return (x > 0) * 1 + (x &lt;= 0) * 0.01          def linear_gradient(x):             return 1                     list_back = self.layers[::-1]         for epoch in range(epochs):             for elem in range(x_input.shape[0]):                 x, y = x_input[elem].reshape(1, -1), y_input[elem]                 predict_layers = predict(x)  # 1 - y, 2 - w, 3 - b, 4 - \u0441\u043b\u043e\u0439, 5 - \u043a\u043e\u043b. \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432                 predict_for_layers, activations, weights, b_coef, layers = predict_layers[0][::-1], predict_layers[1][::-1], predict_layers[2][::-1], predict_layers[3][::-1], predict_layers[4]                 units = predict_layers[5]                 layer_error = predict_for_layers[0] - y                 if len(layer_error.shape) == 1:                     layer_error = layer_error.reshape(1, -1)                 for ind in range(len(list_back) - 1):                     delta_weights = 0                     if activations[ind] == 'linear':                         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])                     if activations[ind] == 'Leaky_relu':                         delta_weights = layer_error * leaky_relu_gradient(predict_for_layers[ind])                     if activations[ind] == 'relu':                         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])                     if activations[ind] == 'sigmoid':                         delta_weights = layer_error * sigmoid_gradient(predict_for_layers[ind])                     if activations[ind] == 'tanh':                         delta_weights = layer_error * tanh_gradient(predict_for_layers[ind])                      b_coef[ind] -= alpha * (np.full(b_coef[ind].shape, layer_error.sum()))                     layer_error = delta_weights.dot(np.transpose(weights[ind]))                     weights[ind] -= alpha * (np.transpose(predict_for_layers[ind + 1]).dot(delta_weights))                  weights_inp = weights[::-1]                 b_inp = b_coef[::-1]                 activations_inp = activations[::-1]                  for indx in range(1, len(self.layers)):                     if layers[indx] == 1:                         self.layers[indx] = DenseLayer(units=units[indx - 1], weights=weights_inp[indx - 1],                                                        b=b_inp[indx - 1], activation=activations_inp[indx - 1])      # \u041f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439     def predict(self, x):         predict = self.layers[0](x)         for i in range(1, len(self.layers)):             #print(predict[0].shape)             predict = self.layers[i](predict[0])          return predict <\/code><\/pre>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">model = Sequential([     Input(),     DenseLayer(units=3, activation='relu'),     DenseLayer(units=1, activation='relu') ]) x = np.array([[3., 2.], [2., 2.], [3., 3.], [4., 4.]]) y = [5, 4, 6, 8] #print(x.shape) model.fit(x, y, epochs=40) x_test = np.array([[6., 5.], [5., 5.]])  print(model.predict(x_test)[0])<\/code><\/pre>\n<p>\u0412\u044b\u0445\u043e\u0434:<\/p>\n<p>[[11.26779558]<br \/> [ 9.52089243]]<\/p>\n<h3>\u0412\u044b\u0432\u043e\u0434\u044b<\/h3>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043c\u043e\u0433\u043b\u0430 \u0432\u044b\u0434\u0430\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0430 \u043f\u0440\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438. <\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043d\u0435\u0434\u043e\u0447\u0451\u0442\u043e\u0432, \u0442\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0447\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c &#8212; \u043f\u0438\u0448\u0438\u0442\u0435.<\/p>\n<\/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\/793584\/\"> https:\/\/habr.com\/ru\/articles\/793584\/<\/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<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <strong>Sequential<\/strong> \u0438 \u0441\u043b\u043e\u0451\u0432 <strong>Dense<\/strong> \u0438 <strong>Input<\/strong> \u0447\u0435\u0440\u0435\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Numpy \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0439\u043d\u043e\u0439 NN.<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430\u0446\u0435\u043b\u0435\u043d\u0430 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u0435\u0432 \u0441 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u043c \u0442\u0435\u043e\u0440\u0438\u0438, \u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0442\u0435\u043e\u0440\u0438\u0435\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439.  <\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a:<\/p>\n<pre><code class=\"python\">import numpy as np<\/code><\/pre>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Dense \u0441\u043b\u043e\u044f<\/h3>\n<figure class=\"full-width\">\n<div><figcaption>Dense-\u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"python\">class DenseLayer():     def __init__(self,  units=1, activation='relu', weights=np.array([]), b=np.array([])):         self.units = units         self.fl_init = True         self.activation = activation         self.weights = weights         self.b_new = b         self.w, self.b = np.array([]), np.array([])<\/code><\/pre>\n<p>\u041f\u043e\u044f\u0441\u043d\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<ul>\n<li>\n<p><em>units<\/em> &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 <\/p>\n<\/li>\n<li>\n<p><em>activation &#8212; <\/em>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<li>\n<p><em>weights<\/em> \u0438 <em>b_new<\/em> &#8212; \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438<\/p>\n<\/li>\n<li>\n<p><em>w<\/em>,<em> b<\/em> &#8212; \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p><em>fl_init <\/em>&#8212;<em> <\/em>\u0444\u043b\u0430\u0433, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043b\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c call, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 \u0432 <a href=\"https:\/\/pylot.me\/article\/g8N-functors-in-python\/?ysclid=lsgmpjzheg673075891#\" rel=\"noopener noreferrer nofollow\">\u0444\u0443\u043d\u043a\u0442\u043e\u0440<\/a>:<\/p>\n<pre><code class=\"python\">    def __call__(self, x):         if (self.fl_init == True) and (self.weights.shape[0] == 0):             self.w = np.random.normal(loc=0.0, scale=1.0, size=(x.shape[-1], self.units))\/np.sqrt(2.0\/x.shape[-1]) + 0.001             self.b = np.ones(shape=(self.units, ), dtype=np.float32)             self.fl_init = False             # print(self.w.shape, self.weights)          elif self.weights.shape[0] != 0:             self.weights = self.weights.reshape((x.shape[-1], self.units))             self.w = self.weights             self.fl_init = False              self.b_new = self.b_new.reshape((self.units, ))             self.b = self.b_new             self.fl_init = False          y = x.dot(self.w) + self.b          if self.activation == 'relu':             return np.maximum(np.zeros(shape=y.shape), y), self.w, self.b, 1, self.units, self.activation         if self.activation == 'Leaky_relu':             return np.maximum(0.01*y, y), self.w, self.b, 1, self.units, self.activation         if self.activation == 'softmax':             return np.exp(y)\/np.sum(np.exp(y), axis=0), self.w, self.b, 1, self.units, self.activation         if self.activation == 'sigmoid':             return 1 \/ (1 + np.exp(-y)), self.w, self.b, 1, self.units, self.activation         if self.activation == 'tanh':             return (np.exp(2*y) - 1)\/(np.exp(2*y) + 1), self.w, self.b, 1, self.units, self.activation         if self.activation == 'linear':             return y, self.w, self.b, 1, self.units, self.activation<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u0430\u043a\u043e\u0439:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0447\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0435\u0449\u0435 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438 \u043d\u0430\u043c \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u043b\u0438 \u0443\u0436\u0435 \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c (\u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 = 0, \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f = 1), \u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0451\u043c \u043a\u0430\u043a \u0435\u0434\u0438\u043d\u0438\u0446\u044b -> \u0424\u043b\u0430\u0433 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 False.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b, \u0442\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u043d\u0430 \u043d\u0438\u0445.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c<strong> y<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0435\u0451 \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 (\u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u0430 &#8212; \u0442\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0447\u0435\u0440\u0435\u0437 &#8216;<em>relu<\/em>&#8216;)<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 Input<\/strong>:<\/p>\n<pre><code class=\"python\">class Input():     def __init__(self, shape=None):         self.shape = shape      def __call__(self, x):         if self.shape is not None:             if x.shape != self.shape:                 return x.reshape(shape=self.shape), 0             else:                 return x, 0         return x, 0<\/code><\/pre>\n<p><strong>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043c\u043e\u0434\u0443\u043b\u044f<\/strong> <strong>Sequential:<\/strong><\/p>\n<pre><code class=\"python\">class Sequential():     def __init__(self, layers):         self.layers = layers # \u0441\u043b\u043e\u0438 \u0432 NN<\/code><\/pre>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043c\u0435\u0442\u043e\u0434\u0430 <strong>fit:<\/strong><\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <em>predict<\/em>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0432\u044b\u0445\u043e\u0434\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u0432\u0435\u0441\u0430 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0441\u043b\u043e\u0439 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432. \u041e\u043d\u0430 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u0430 <strong><em>\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0430<\/em><\/strong><em>(BP).<\/em><\/p>\n<pre><code class=\"python\">        def predict(x):             activations = []             predict_for_layers = []             weights = []             b_coef = []             layer_2 = []             units = []             predict = self.layers[0](x)             layer_2.append(predict[1])             predict_for_layers.append(predict[0])             for i in range(1, len(self.layers)):                 predict = self.layers[i](predict[0])                 activations.append(predict[-1])                 predict_for_layers.append(predict[0])                 weights.append(predict[1])                 b_coef.append(predict[2])                 layer_2.append(predict[3])                 units.append(predict[4])             #print(len(units))              return predict_for_layers, activations, weights, b_coef, layer_2, units<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\">        def sigmoid_gradient(output):             return output * (1 - output)          def tanh_gradient(out):             return 1\/((np.exp(out) + np.exp(-out)\/2)**2)          def relu_gradient(x):             return (x > 0) * 1          def leaky_relu_gradient(x):             return (x > 0) * 1 + (x &lt;= 0) * 0.01          def linear_gradient(x):             return 1<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u0430\u043c\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <strong>Backpropagation<\/strong><em>:<\/em><\/p>\n<pre><code class=\"python\">list_back = self.layers[::-1] for elem in range(x_input.shape[0]):   x, y = x_input[elem].reshape(1, -1), y_input[elem]   for epoch in range(epochs):     predict_layers = predict(x) # 1 - y, 2 - w, 3 - b, 4 - \u0441\u043b\u043e\u0439, 5 - \u043a\u043e\u043b. \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432     predict_for_layers, activations, weights, b_coef, layers = predict_layers[0][::-1], predict_layers[1][::-1], predict_layers[2][::-1], predict_layers[3][::-1], predict_layers[4]     units = predict_layers[5]     layer_error = predict_for_layers[0] - y     if len(layer_error.shape) == 1:       layer_error = layer_error.reshape(1, -1)     for ind in range(len(list_back) - 1):       delta_weights = 0       if activations[ind] == 'linear':         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])       if activations[ind] == 'Leaky_relu':         delta_weights = layer_error * leaky_relu_gradient(predict_for_layers[ind])       if activations[ind] == 'relu':         delta_weights = layer_error * relu_gradient(predict_for_layers[ind])       if activations[ind] == 'sigmoid':         delta_weights = layer_error * sigmoid_gradient(predict_for_layers[ind])       if activations[ind] == 'tanh':         delta_weights = layer_error * tanh_gradient(predict_for_layers[ind])        b_coef[ind] -= alpha * (np.full(b_coef[ind].shape, layer_error.sum()))       layer_error = delta_weights.dot(np.transpose(weights[ind]))       weights[ind] -= alpha * (np.transpose(predict_for_layers[ind + 1]).dot(delta_weights))      weights_inp = weights[::-1]     b_inp = b_coef[::-1]     activations_inp = activations[::-1]      for indx in range(1, len(self.layers)):       if layers[indx] == 1:         self.layers[indx] = DenseLayer(units=units[indx - 1], weights=weights_inp[indx - 1], b=b_inp[indx - 1], activation=activations_inp[indx - 1]) <\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0443 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b:<\/p>\n<ul>\n<li>\n<p>\u0412 \u0446\u0438\u043a\u043b\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u044d\u043f\u043e\u0445:<\/p>\n<ol>\n<li>\n<p>\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 &#8212; label + input.<\/p>\n<\/li>\n<li>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <em>predict<\/em> \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0445\u043e\u0434 (\u0441\u043f\u0438\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0441\u043b\u043e\u044f).<\/p>\n<\/li>\n<li>\n<p>\u0421\u0447\u0438\u0442\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043b\u043e\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>delta_weights<\/strong> &#8212; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u0430\u044f, \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u0430\u044f \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0430\u043c &#8212; (\u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439, \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0445 \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e. \u0415\u0441\u043b\u0438 \u043d\u0430\u043a\u043b\u043e\u043d \u043a\u0430\u0441\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u0438\u043d\u0438\u0438(\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439) \u0431\u044b\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0442\u043e \u0432 \u0441\u0435\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435, \u043b\u0438\u0431\u043e \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435) &#8212; \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c <strong><em>layer_error<\/em><\/strong>.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0441\u0430 \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443:<\/p>\n<figure class=\"\">\n<div><figcaption><em>\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432 NN<\/em><\/figcaption><\/div>\n<\/figure>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043f\u043e \u0441\u043b\u043e\u044f\u043c \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0438\u0445 \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <em>predict <\/em>\u0431\u0435\u0437 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0445\u043e\u0434\u043e\u0432:<\/p>\n<pre><code class=\"python\">    def predict(self, x):         predict = self.layers[0](x)         for i in range(1, len(self.layers)):             predict = self.layers[i](predict[0])          return predict<\/code><\/pre>\n<p><strong>\u041a\u043e\u0434 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e<\/strong>:<\/p>\n<pre><code class=\"python\">class Sequential():     def __init__(self, layers):         self.layers = layers      def fit(self, x_input, y_input, epochs=50, alpha=0.01):          def predict(x):             activations = []             predict_for_layers = []             weights = []             b_coef = []             layer_2 = []             units = []             predict = self.layers[0](x)             layer_2.append(predict[1])             predict_for_layers.append(predict[0])             for i in range(1, len(self.layers)):                 predict = self.layers[i](predict[0])                 activations.append(predict[-1])                 predict_for_layers.append(predict[0])                 weights.append(predict[1])                 b_coef.append(predict[2])                 layer_2.append(predict[3])                 units.append(predict[4])              return predict_for_layers, activations, weights, b_coef, layer_2, units                     def sigmoid_gradient(output):             return output * (1 - output)          def tanh_gradient(out):             return 1\/((np.exp(out) + np.exp(-out)\/2)**2)          def relu_gradient(x):             return (x > 0) * 1          def leaky_relu_gradient(x):             return (x > 0) * 1 + (x &lt;= 0) * 0.01          def linear_gradient(x):             return 1                     list_back = self.layers[::-1]         for epoch in range(epochs):             for elem in range(x_input.shape[0]):                 x, y = x_input[elem].reshape(1, -1), y_input[elem]                 predict_layers = predict(x)  # 1 - y, 2 - w, 3 - b, 4 - \u0441\u043b\u043e\u0439, 5 - \u043a\u043e\u043b. \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432                 predict_for_layers, activations, weights, b_coef, layers = predict_layers[0][::-1], predict_layers[1][::-1], predict_layers[2][::-1], predict_layers[3][::-1], predict_layers[4]                 units = predict_layers[5]                 layer_error = predict_for_layers[0] - y                 if len(layer_error.shape) == 1:                     layer_error = layer_error.reshape(1, -1)                 for ind in range(len(list_back) - 1):                     delta_weights = 0                     if activations[ind] == 'linear':                         delta_weights = layer_error *<\/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-368156","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368156","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=368156"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368156\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}