{"id":378387,"date":"2024-06-09T15:01:47","date_gmt":"2024-06-09T15:01:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=378387"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=378387","title":{"rendered":"<span>\u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 Keras \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432<\/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\/7c3\/85d\/8f9\/7c385d8f9c071c4ef3adad1df44e1090.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c3\/85d\/8f9\/7c385d8f9c071c4ef3adad1df44e1090.png\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>Keras \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043e\u0449\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u043b\u043e\u0435\u0432 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <strong>\u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0441\u043b\u043e\u0438.<\/strong><\/p>\n<p>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u043e\u0434 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0435\u043a.<\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u0442 <code>tf.keras.layers.Layer<\/code>. \u0412 <code>__init__<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u044f, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043b\u043e\u044f:<\/p>\n<pre><code class=\"python\">import tensorflow as tf  class CustomLayer(tf.keras.layers.Layer):     def __init__(self, units=32, activation=None, **kwargs):         super(CustomLayer, self).__init__(**kwargs)         self.units = units         self.activation = tf.keras.activations.get(activation)<\/code><\/pre>\n<p>\u0422\u0443\u0442 <code>units<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432, \u0430 <code>activation<\/code> \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438. <code>super(CustomLayer, self).__init__(**kwargs)<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 <code>Layer<\/code>.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>build<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f Keras \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043b\u043e\u044f. \u0415\u0433\u043e \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0441\u043b\u043e\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">    def build(self, input_shape):         self.kernel = self.add_weight(shape=(input_shape[-1], self.units),                                       initializer='glorot_uniform',                                       trainable=True)         self.bias = self.add_weight(shape=(self.units,),                                     initializer='zeros',                                     trainable=True)         super(CustomLayer, self).build(input_shape)<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0432\u0435\u0441\u0430 <code>kernel<\/code> \u0438 <code>bias<\/code>. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>add_weight<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>call<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u044f. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">    def call(self, inputs):         output = tf.matmul(inputs, self.kernel) + self.bias         if self.activation is not None:             output = self.activation(output)         return output<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0435\u0441\u0430 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u043e\u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043c\u043e\u0434\u0435\u043b\u044f\u0445 Keras \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043b\u043e\u0439:<\/p>\n<pre><code class=\"python\">model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomLayer(units=64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<ul>\n<li>\n<p><code>add_weight<\/code>: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0441\u0430 \u0432 \u0441\u043b\u043e\u0439.<\/p>\n<\/li>\n<li>\n<p><code>compute_output_shape<\/code>: \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u043e\u0440\u043c\u0443 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u043e\u0440\u043c\u044b \u0432\u0445\u043e\u0434\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><code>get_config<\/code>: \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u044f \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u0447\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438: Dense, Convolutional \u0438 \u0435\u0449\u0435 \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043b\u043e\u0435\u0432<\/h4>\n<p><strong>Dense \u0441\u043b\u043e\u0439<\/strong> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e: \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0432\u0435\u0441\u043e\u0432 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import tensorflow as tf  class CustomDenseLayer(tf.keras.layers.Layer):     def __init__(self, units=32, activation=None):         super(CustomDenseLayer, self).__init__()         self.units = units         self.activation = tf.keras.activations.get(activation)      def build(self, input_shape):         self.w = self.add_weight(shape=(input_shape[-1], self.units),                                  initializer='glorot_uniform',                                  trainable=True)         self.b = self.add_weight(shape=(self.units,),                                  initializer='zeros',                                  trainable=True)      def call(self, inputs):         z = tf.matmul(inputs, self.w) + self.b         if self.activation is not None:             return self.activation(z)         return z  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomDenseLayer(units=64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Convolutional \u0441\u043b\u043e\u0438<\/strong> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442 \u0441\u0432\u0435\u0440\u0442\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a \u0432\u0445\u043e\u0434\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"python\">class CustomConvLayer(tf.keras.layers.Layer):     def __init__(self, filters, kernel_size, strides=(1, 1), padding='valid', activation=None):         super(CustomConvLayer, self).__init__()         self.filters = filters         self.kernel_size = kernel_size         self.strides = strides         self.padding = padding         self.activation = tf.keras.activations.get(activation)      def build(self, input_shape):         self.kernel = self.add_weight(shape=(*self.kernel_size, input_shape[-1], self.filters),                                       initializer='glorot_uniform',                                       trainable=True)      def call(self, inputs):         conv = tf.nn.conv2d(inputs, self.kernel, strides=self.strides, padding=self.padding.upper())         if self.activation is not None:             return self.activation(conv)         return conv  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(28, 28, 1)),     CustomConvLayer(filters=32, kernel_size=(3, 3), activation='relu'),     tf.keras.layers.Flatten(),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Recurrent \u0441\u043b\u043e\u0438<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u2014 \u044d\u0442\u043e LSTM:<\/p>\n<pre><code class=\"python\">class CustomLSTMLayer(tf.keras.layers.Layer):     def __init__(self, units):         super(CustomLSTMLayer, self).__init__()         self.units = units      def build(self, input_shape):         self.lstm_cell = tf.keras.layers.LSTMCell(self.units)      def call(self, inputs, states):         return self.lstm_cell(inputs, states)  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(None, 8)),     tf.keras.layers.RNN(CustomLSTMLayer(units=64)),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Dropout \u0441\u043b\u043e\u0439<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u044f \u043f\u0435\u0440\u0435\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u0437\u0430\u043d\u0443\u043b\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438:<\/p>\n<pre><code class=\"python\">class CustomDropoutLayer(tf.keras.layers.Layer):     def __init__(self, rate):         super(CustomDropoutLayer, self).__init__()         self.rate = rate      def call(self, inputs, training=None):         return tf.nn.dropout(inputs, rate=self.rate) if training else inputs  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     tf.keras.layers.Dense(64, activation='relu'),     CustomDropoutLayer(rate=0.5),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>BatchNormalization \u0441\u043b\u043e\u0439<\/strong> \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f, \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0438:<\/p>\n<pre><code class=\"python\">class CustomBatchNormalizationLayer(tf.keras.layers.Layer):     def __init__(self):         super(CustomBatchNormalizationLayer, self).__init__()      def build(self, input_shape):         self.gamma = self.add_weight(shape=(input_shape[-1],),                                      initializer='ones',                                      trainable=True)         self.beta = self.add_weight(shape=(input_shape[-1],),                                     initializer='zeros',                                     trainable=True)         self.moving_mean = self.add_weight(shape=(input_shape[-1],),                                            initializer='zeros',                                            trainable=False)         self.moving_variance = self.add_weight(shape=(input_shape[-1],),                                                initializer='ones',                                                trainable=False)      def call(self, inputs, training=None):         if training:             mean, variance = tf.nn.moments(inputs, axes=[0])             self.moving_mean.assign(self.moving_mean * 0.9 + mean * 0.1)             self.moving_variance.assign(self.moving_variance * 0.9 + variance * 0.1)         else:             mean, variance = self.moving_mean, self.moving_variance          return tf.nn.batch_normalization(inputs, mean, variance, self.beta, self.gamma, variance_epsilon=1e-3)  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomBatchNormalizationLayer(),     tf.keras.layers.Dense(64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<hr\/>\n<p><strong><em>\u0411\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043a\u0435\u0439\u0441\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u0433\u0438 \u0438\u0437 OTUS \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u043e\u0432. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e<\/em><\/strong><a href=\"https:\/\/otus.pw\/NPlK\/\"><strong><em> \u0441 \u043f\u043e\u043b\u043d\u044b\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u043c \u043a\u0443\u0440\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/em><\/strong><\/a><strong><em>.<\/em><\/strong><\/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\/818791\/\"> https:\/\/habr.com\/ru\/articles\/818791\/<\/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>Keras \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043e\u0449\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u043b\u043e\u0435\u0432 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <strong>\u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0441\u043b\u043e\u0438.<\/strong><\/p>\n<p>\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u043e\u0434 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0435\u043a.<\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u0442 <code>tf.keras.layers.Layer<\/code>. \u0412 <code>__init__<\/code> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u044f, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043b\u043e\u044f:<\/p>\n<pre><code class=\"python\">import tensorflow as tf  class CustomLayer(tf.keras.layers.Layer):     def __init__(self, units=32, activation=None, **kwargs):         super(CustomLayer, self).__init__(**kwargs)         self.units = units         self.activation = tf.keras.activations.get(activation)<\/code><\/pre>\n<p>\u0422\u0443\u0442 <code>units<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432, \u0430 <code>activation<\/code> \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438. <code>super(CustomLayer, self).__init__(**kwargs)<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 <code>Layer<\/code>.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>build<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f Keras \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043b\u043e\u044f. \u0415\u0433\u043e \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0441\u043b\u043e\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">    def build(self, input_shape):         self.kernel = self.add_weight(shape=(input_shape[-1], self.units),                                       initializer='glorot_uniform',                                       trainable=True)         self.bias = self.add_weight(shape=(self.units,),                                     initializer='zeros',                                     trainable=True)         super(CustomLayer, self).build(input_shape)<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0432\u0435\u0441\u0430 <code>kernel<\/code> \u0438 <code>bias<\/code>. \u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>add_weight<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>call<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u044f. \u041e\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">    def call(self, inputs):         output = tf.matmul(inputs, self.kernel) + self.bias         if self.activation is not None:             output = self.activation(output)         return output<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0435\u0441\u0430 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438, \u043e\u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043c\u043e\u0434\u0435\u043b\u044f\u0445 Keras \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043b\u043e\u0439:<\/p>\n<pre><code class=\"python\">model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomLayer(units=64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<ul>\n<li>\n<p><code>add_weight<\/code>: \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0441\u0430 \u0432 \u0441\u043b\u043e\u0439.<\/p>\n<\/li>\n<li>\n<p><code>compute_output_shape<\/code>: \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u043e\u0440\u043c\u0443 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u043e\u0440\u043c\u044b \u0432\u0445\u043e\u0434\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><code>get_config<\/code>: \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u044f \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u0447\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438: Dense, Convolutional \u0438 \u0435\u0449\u0435 \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043b\u043e\u0435\u0432<\/h4>\n<p><strong>Dense \u0441\u043b\u043e\u0439<\/strong> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e: \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0432\u0435\u0441\u043e\u0432 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import tensorflow as tf  class CustomDenseLayer(tf.keras.layers.Layer):     def __init__(self, units=32, activation=None):         super(CustomDenseLayer, self).__init__()         self.units = units         self.activation = tf.keras.activations.get(activation)      def build(self, input_shape):         self.w = self.add_weight(shape=(input_shape[-1], self.units),                                  initializer='glorot_uniform',                                  trainable=True)         self.b = self.add_weight(shape=(self.units,),                                  initializer='zeros',                                  trainable=True)      def call(self, inputs):         z = tf.matmul(inputs, self.w) + self.b         if self.activation is not None:             return self.activation(z)         return z  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomDenseLayer(units=64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Convolutional \u0441\u043b\u043e\u0438<\/strong> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442 \u0441\u0432\u0435\u0440\u0442\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a \u0432\u0445\u043e\u0434\u043d\u044b\u043c \u0434\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"python\">class CustomConvLayer(tf.keras.layers.Layer):     def __init__(self, filters, kernel_size, strides=(1, 1), padding='valid', activation=None):         super(CustomConvLayer, self).__init__()         self.filters = filters         self.kernel_size = kernel_size         self.strides = strides         self.padding = padding         self.activation = tf.keras.activations.get(activation)      def build(self, input_shape):         self.kernel = self.add_weight(shape=(*self.kernel_size, input_shape[-1], self.filters),                                       initializer='glorot_uniform',                                       trainable=True)      def call(self, inputs):         conv = tf.nn.conv2d(inputs, self.kernel, strides=self.strides, padding=self.padding.upper())         if self.activation is not None:             return self.activation(conv)         return conv  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(28, 28, 1)),     CustomConvLayer(filters=32, kernel_size=(3, 3), activation='relu'),     tf.keras.layers.Flatten(),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Recurrent \u0441\u043b\u043e\u0438<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 \u2014 \u044d\u0442\u043e LSTM:<\/p>\n<pre><code class=\"python\">class CustomLSTMLayer(tf.keras.layers.Layer):     def __init__(self, units):         super(CustomLSTMLayer, self).__init__()         self.units = units      def build(self, input_shape):         self.lstm_cell = tf.keras.layers.LSTMCell(self.units)      def call(self, inputs, states):         return self.lstm_cell(inputs, states)  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(None, 8)),     tf.keras.layers.RNN(CustomLSTMLayer(units=64)),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>Dropout \u0441\u043b\u043e\u0439<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u044f \u043f\u0435\u0440\u0435\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u0437\u0430\u043d\u0443\u043b\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438:<\/p>\n<pre><code class=\"python\">class CustomDropoutLayer(tf.keras.layers.Layer):     def __init__(self, rate):         super(CustomDropoutLayer, self).__init__()         self.rate = rate      def call(self, inputs, training=None):         return tf.nn.dropout(inputs, rate=self.rate) if training else inputs  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     tf.keras.layers.Dense(64, activation='relu'),     CustomDropoutLayer(rate=0.5),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<p><strong>BatchNormalization \u0441\u043b\u043e\u0439<\/strong> \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f, \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0438:<\/p>\n<pre><code class=\"python\">class CustomBatchNormalizationLayer(tf.keras.layers.Layer):     def __init__(self):         super(CustomBatchNormalizationLayer, self).__init__()      def build(self, input_shape):         self.gamma = self.add_weight(shape=(input_shape[-1],),                                      initializer='ones',                                      trainable=True)         self.beta = self.add_weight(shape=(input_shape[-1],),                                     initializer='zeros',                                     trainable=True)         self.moving_mean = self.add_weight(shape=(input_shape[-1],),                                            initializer='zeros',                                            trainable=False)         self.moving_variance = self.add_weight(shape=(input_shape[-1],),                                                initializer='ones',                                                trainable=False)      def call(self, inputs, training=None):         if training:             mean, variance = tf.nn.moments(inputs, axes=[0])             self.moving_mean.assign(self.moving_mean * 0.9 + mean * 0.1)             self.moving_variance.assign(self.moving_variance * 0.9 + variance * 0.1)         else:             mean, variance = self.moving_mean, self.moving_variance          return tf.nn.batch_normalization(inputs, mean, variance, self.beta, self.gamma, variance_epsilon=1e-3)  # example model = tf.keras.Sequential([     tf.keras.layers.Input(shape=(8,)),     CustomBatchNormalizationLayer(),     tf.keras.layers.Dense(64, activation='relu'),     tf.keras.layers.Dense(10, activation='softmax') ])<\/code><\/pre>\n<hr\/>\n<p><strong><em>\u0411\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043a\u0435\u0439\u0441\u043e\u0432 \u043a\u043e\u043b\u043b\u0435\u0433\u0438 \u0438\u0437 OTUS \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u043e\u0432. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e<\/em><\/strong><a href=\"https:\/\/otus.pw\/NPlK\/\"><strong><em> \u0441 \u043f\u043e\u043b\u043d\u044b\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u043c \u043a\u0443\u0440\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/em><\/strong><\/a><strong><em>.<\/em><\/strong><\/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\/818791\/\"> https:\/\/habr.com\/ru\/articles\/818791\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-378387","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378387","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=378387"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378387\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}