{"id":297060,"date":"2020-01-13T15:00:22","date_gmt":"2020-01-13T15:00:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297060"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297060","title":{"rendered":"Keras Functional API \u0432 TensorFlow"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/483664\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/w1\/zr\/n8\/w1zrn8ydafoxahso_ig7vx1stfg.png\"><\/p>\n<p>  \u0412 Keras \u0435\u0441\u0442\u044c \u0434\u0432\u0430 API \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439 Sequential \u0438 Functional. \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Functional API \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0412 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0430, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c Functional API, \u0441 \u0441\u0430\u0439\u0442\u0430 TensorFlow.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>  Functional API \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e \u0447\u0435\u043c Sequential API, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0435\u0439, \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043e\u0431\u0449\u0438\u043c\u0438 \u0441\u043b\u043e\u044f\u043c\u0438, \u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0432\u0445\u043e\u0434\u0430\u043c\u0438 \u0438\u043b\u0438 \u0432\u044b\u0445\u043e\u0434\u0430\u043c\u0438.<\/p>\n<p>  \u041e\u043d \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0433\u0440\u0430\u0444 (DAG) \u0441\u043b\u043e\u0435\u0432<\/p>\n<p>  Functional API \u2014 \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f <b>\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430 \u0441\u043b\u043e\u0435\u0432<\/b>.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<blockquote><p>(\u0432\u0445\u043e\u0434: 784-\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440)<br \/>   \u21a7<br \/>  [\u041f\u043b\u043e\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 (64 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f relu)]<br \/>   \u21a7<br \/>  [\u041f\u043b\u043e\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 (64 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f relu)]<br \/>   \u21a7<br \/>  [\u041f\u043b\u043e\u0442\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 (10 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f softmax)]<br \/>   \u21a7<br \/>  (\u0432\u044b\u0445\u043e\u0434: \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 10 \u043a\u043b\u0430\u0441\u0441\u043e\u0432)<\/p><\/blockquote>\n<p>  \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0433\u0440\u0430\u0444 \u0438\u0437 3 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<p>  \u0414\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Functional API, \u0432\u0430\u043c \u043d\u0430\u0434\u043e \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430:<\/p>\n<pre><code class=\"python\">from tensorflow import keras  inputs = keras.Input(shape=(784,))<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445: 784-\u043c\u0435\u0440\u043d\u044b\u0445 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f, \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u0414\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u043b\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 `(32, 32, 3)`, \u043c\u044b \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438:<\/p>\n<pre><code class=\"python\">img_inputs = keras.Input(shape=(32, 32, 3))<\/code><\/pre>\n<p>  \u0422\u043e, \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>inputs<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430\u0445 \u0438 \u0442\u0438\u043f\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0432\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"python\">inputs.shape<\/code><\/pre>\n<pre><code class=\"python\">TensorShape([None, 784])<\/code><\/pre>\n<pre><code class=\"python\">inputs.dtype<\/code><\/pre>\n<pre><code class=\"python\">tf.float32<\/code><\/pre>\n<p>  \u0412\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043b\u043e\u0435\u0432, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u0441\u043b\u043e\u0439 \u043d\u0430 \u044d\u0442\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 <code>inputs<\/code>:<\/p>\n<pre><code class=\"python\">from tensorflow.keras import layers  dense = layers.Dense(64, activation='relu') x = dense(inputs)<\/code><\/pre>\n<p>  \u00ab\u0412\u044b\u0437\u043e\u0432 \u0441\u043b\u043e\u044f\u00bb \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u0440\u0438\u0441\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0442\u0440\u0435\u043b\u043a\u0438 \u0438\u0437 \u00ab\u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445\u00bb \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u0441\u043b\u043e\u0439. \u041c\u044b \u00ab\u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c\u00bb \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 <code>dense<\/code> \u0441\u043b\u043e\u0439, \u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <code>x<\/code>.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0435\u0432 \u0432 \u043d\u0430\u0448 \u0433\u0440\u0430\u0444 \u0441\u043b\u043e\u0435\u0432:<\/p>\n<pre><code class=\"python\">x = layers.Dense(64, activation='relu')(x) outputs = layers.Dense(10, activation='softmax')(x)<\/code><\/pre>\n<p>  \u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c <code>Model<\/code> \u0443\u043a\u0430\u0437\u0430\u0432 \u0435\u0433\u043e \u0432\u0445\u043e\u0434\u044b \u0438 \u0432\u044b\u0445\u043e\u0434\u044b \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043b\u043e\u0435\u0432:<\/p>\n<pre><code class=\"python\">model = keras.Model(inputs=inputs, outputs=outputs)<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">inputs = keras.Input(shape=(784,), name='img') x = layers.Dense(64, activation='relu')(inputs) x = layers.Dense(64, activation='relu')(x) outputs = layers.Dense(10, activation='softmax')(x)  model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')<\/code><\/pre>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u0432\u043e\u0434\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">model.summary()<\/code><\/pre>\n<pre><code class=\"python\">Model: \"mnist_model\" _________________________________________________________________ Layer (type)                 Output Shape              Param #    ================================================================= img (InputLayer)             [(None, 784)]             0          _________________________________________________________________ dense_3 (Dense)              (None, 64)                50240      _________________________________________________________________ dense_4 (Dense)              (None, 64)                4160       _________________________________________________________________ dense_5 (Dense)              (None, 10)                650        ================================================================= Total params: 55,050 Trainable params: 55,050 Non-trainable params: 0 _________________________________________________________________<\/code><\/pre>\n<p>  \u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0447\u0435\u0440\u0442\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u0432\u0438\u0434\u0435 \u0433\u0440\u0430\u0444\u0430:<\/p>\n<pre><code class=\"python\">keras.utils.plot_model(model, 'my_first_model.png')<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/oq\/4o\/vl\/oq4ovlxewr3hxczaldchmbeyfua.png\" alt=\"image\"><\/p>\n<p>  \u0418 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u0432\u0445\u043e\u0434\u0430 \u0438 \u0432\u044b\u0445\u043e\u0434\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u043d\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435:<\/p>\n<pre><code class=\"python\">keras.utils.plot_model(model, 'my_first_model_with_shape_info.png', show_shapes=True)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fn\/rm\/hc\/fnrmhcqknnsjdcgmqp9w6dpong8.png\" alt=\"image\"><\/p>\n<p>  \u042d\u0442\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b. \u0412 \u0432\u0435\u0440\u0441\u0438\u0438 \u043a\u043e\u0434\u0430, \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u0435\u043b\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0432\u044b\u0437\u043e\u0432\u0430.<\/p>\n<p>  \u00ab\u0413\u0440\u0430\u0444 \u0441\u043b\u043e\u0435\u0432\u00bb \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0430 Functional API \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u043b\u0438\u0437\u043a\u043e \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u044d\u0442\u043e\u0442 \u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0437.<\/p>\n<h2>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u043e\u0446\u0435\u043d\u043a\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434<\/h2>\n<p>  \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u043e\u0446\u0435\u043d\u043a\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Functional API \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u0438 \u0432 Sequential \u043c\u043e\u0434\u0435\u043b\u044f\u0445.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044e.<\/p>\n<p>  \u0422\u0443\u0442 \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 MNIST, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u044b, \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043c\u043e\u043d\u0438\u0442\u043e\u0440\u044f \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435), \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043c\u044b \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train = x_train.reshape(60000, 784).astype('float32') \/ 255 x_test = x_test.reshape(10000, 784).astype('float32') \/ 255  model.compile(loss='sparse_categorical_crossentropy',               optimizer=keras.optimizers.RMSprop(),               metrics=['accuracy']) history = model.fit(x_train, y_train,                     batch_size=64,                     epochs=5,                     validation_split=0.2) test_scores = model.evaluate(x_test, y_test, verbose=2) print('Test loss:', test_scores[0]) print('Test accuracy:', test_scores[1]) <\/code><\/pre>\n<h2>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Functional API \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u0438 \u0434\u043b\u044f Sequential \u043c\u043e\u0434\u0435\u043b\u0435\u0439.<\/p>\n<p>  \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f Functional \u043c\u043e\u0434\u0435\u043b\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432 <code>model.save(<\/code>) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0441\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b.<\/p>\n<p>  \u041f\u043e\u0437\u0436\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u0443 \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043a\u043e\u0434\u0443 \u0441\u043e\u0437\u0434\u0430\u0432\u0448\u0435\u043c\u0443 \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<p>  \u042d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442:  <\/p>\n<ul>\n<li>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043c\u043e\u0434\u0435\u043b\u0438<\/li>\n<li>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0441\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f)<\/li>\n<li>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 (\u0442\u043e \u0447\u0442\u043e \u0432\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u043b\u0438 \u0432 <code>compile<\/code>)<\/li>\n<li>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440 \u0438 \u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0431\u044b\u043b\u043e (\u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441 \u0442\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u0432\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c)<\/li>\n<\/ul>\n<pre><code class=\"python\"> model.save('path_to_my_model.h5') del model # Recreate the exact same model purely from the file: model = keras.models.load_model('path_to_my_model.h5') <\/code><\/pre>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441\u043b\u043e\u0435\u0432 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439<\/h2>\n<p>  \u0412 Functional API, \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043b\u043e\u0435\u0432. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0433\u0440\u0430\u0444 \u0441\u043b\u043e\u0435\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439.<\/p>\n<p>  \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0441\u0442\u0435\u043a \u0441\u043b\u043e\u0435\u0432 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439:<br \/>  \u043c\u043e\u0434\u0435\u043b\u044c <code>\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 (encoder)<\/code> \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 16-\u043c\u0435\u0440\u043d\u044b\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u0438 \u0441\u043a\u0432\u043e\u0437\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c <code>\u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 (autoencoder) <\/code>\u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"python\">encoder_input = keras.Input(shape=(28, 28, 1), name='img') x = layers.Conv2D(16, 3, activation='relu')(encoder_input) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.MaxPooling2D(3)(x) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.Conv2D(16, 3, activation='relu')(x) encoder_output = layers.GlobalMaxPooling2D()(x)  encoder = keras.Model(encoder_input, encoder_output, name='encoder') encoder.summary()  x = layers.Reshape((4, 4, 1))(encoder_output) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) x = layers.Conv2DTranspose(32, 3, activation='relu')(x) x = layers.UpSampling2D(3)(x) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) decoder_output = layers.Conv2DTranspose(1, 3, activation='relu')(x)  autoencoder = keras.Model(encoder_input, decoder_output, name='autoencoder') autoencoder.summary() <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u043a\u0443\u044e \u0436\u0435 \u043a\u0430\u043a \u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 <code>(28, 28, 1)<\/code>. \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u043a \u0441\u043b\u043e\u044e <code>Conv2D<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0439 <code>Conv2DTranspose<\/code>, \u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u043a \u0441\u043b\u043e\u044e <code>MaxPooling2D<\/code> \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0439 <code>UpSampling2D<\/code>.<\/p>\n<h2>\u041c\u043e\u0434\u0435\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u043b\u043e\u0438<\/h2>\n<p>  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0442\u0430\u043a, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0431\u044b\u043b \u0441\u043b\u043e\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u0435\u0435 \u043d\u0430 <code>Input<\/code> \u0438\u043b\u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0441\u043b\u043e\u044f.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0432\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0435 \u0432\u0435\u0441\u0430. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0432\u0438\u0434\u0438\u043c \u044d\u0442\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438. \u0412\u043e\u0442 \u0434\u0440\u0443\u0433\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u0438 \u043e\u043d\u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430:<\/p>\n<pre><code class=\"python\">encoder_input = keras.Input(shape=(28, 28, 1), name='original_img') x = layers.Conv2D(16, 3, activation='relu')(encoder_input) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.MaxPooling2D(3)(x) x = layers.Conv2D(32, 3, activation='relu')(x) x = layers.Conv2D(16, 3, activation='relu')(x) encoder_output = layers.GlobalMaxPooling2D()(x)  encoder = keras.Model(encoder_input, encoder_output, name='encoder') encoder.summary()  decoder_input = keras.Input(shape=(16,), name='encoded_img') x = layers.Reshape((4, 4, 1))(decoder_input) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) x = layers.Conv2DTranspose(32, 3, activation='relu')(x) x = layers.UpSampling2D(3)(x) x = layers.Conv2DTranspose(16, 3, activation='relu')(x) decoder_output = layers.Conv2DTranspose(1, 3, activation='relu')(x)  decoder = keras.Model(decoder_input, decoder_output, name='decoder') decoder.summary()  autoencoder_input = keras.Input(shape=(28, 28, 1), name='img') encoded_img = encoder(autoencoder_input) decoded_img = decoder(encoded_img) autoencoder = keras.Model(autoencoder_input, decoded_img, name='autoencoder') autoencoder.summary()<\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435, \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u0430: \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043c\u043e\u0434\u0435\u043b\u044c (\u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u043b\u043e\u0439).<\/p>\n<p>  \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <i>ensembling<\/i>.<\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u0432\u043e\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432 \u043e\u0434\u043d\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u0441\u0440\u0435\u0434\u043d\u044f\u0435\u0442 \u0438\u0445 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u044b:<\/p>\n<blockquote><p>def get_model():<br \/>   inputs = keras.Input(shape=(128,))<br \/>   outputs = layers.Dense(1, activation=&#8217;sigmoid&#8217;)(inputs)<br \/>   return keras.Model(inputs, outputs)<\/p>\n<p>  model1 = get_model()<br \/>  model2 = get_model()<br \/>  model3 = get_model()<\/p>\n<p>  inputs = keras.Input(shape=(128,))<br \/>  y1 = model1(inputs)<br \/>  y2 = model2(inputs)<br \/>  y3 = model3(inputs)<br \/>  outputs = layers.average([y1, y2, y3])<br \/>  ensemble_model = keras.Model(inputs=inputs, outputs=outputs)  <\/p><\/blockquote>\n<h2>\u041c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u044f\u043c\u0438 \u0433\u0440\u0430\u0444\u043e\u0432<\/h2>\n<p>  <\/p>\n<h3>\u041c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0432\u0445\u043e\u0434\u0430\u043c\u0438 \u0438 \u0432\u044b\u0445\u043e\u0434\u0430\u043c\u0438<\/h3>\n<p>  Functional API \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0432\u0445\u043e\u0434\u0430\u043c\u0438 \u0438 \u0432\u044b\u0445\u043e\u0434\u0430\u043c\u0438. \u042d\u0442\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0441 Sequential API.<\/p>\n<p>  \u0412\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<p>  \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0434\u043b\u044f \u0440\u0430\u043d\u0436\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0445 \u0437\u0430\u044f\u0432\u043e\u043a \u043f\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u043e\u0442\u0434\u0435\u043b.<\/p>\n<p>  \u0423 \u0432\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 3 \u0432\u0445\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0437\u0430\u044f\u0432\u043a\u0438 (\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435)<\/li>\n<li>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0437\u0430\u044f\u0432\u043a\u0438 (\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435)<\/li>\n<li>\u041b\u044e\u0431\u044b\u0435 \u0442\u0435\u0433\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c (\u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0439\u043d\u044b\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435)<\/li>\n<\/ul>\n<p>  \u0423 \u043c\u043e\u0434\u0435\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 2 \u0432\u044b\u0445\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\u041e\u0446\u0435\u043d\u043a\u0430 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 0 \u0438 1 (\u0441\u043a\u0430\u043b\u044f\u0440\u043d\u044b\u0439 \u0441\u0438\u0433\u043c\u043e\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0445\u043e\u0434)<\/li>\n<li>\u041e\u0442\u0434\u0435\u043b \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0437\u0430\u044f\u0432\u043a\u0443 (softmax \u0432\u044b\u0445\u043e\u0434 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043e\u0442\u0434\u0435\u043b\u043e\u0432)<\/li>\n<\/ul>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Functional API.<\/p>\n<pre><code class=\"python\">num_tags = 12  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0435\u0433\u043e\u0432 \u043f\u0440\u043e\u0431\u043b\u0435\u043c num_words = 10000  # \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u0440\u0435\u0434\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 num_departments = 4  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0434\u0435\u043b\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439  title_input = keras.Input(shape=(None,), name='title')  # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b body_input = keras.Input(shape=(None,), name='body')  # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b tags_input = keras.Input(shape=(num_tags,), name='tags')  # \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 `num_tags`  # \u0412\u043b\u043e\u0436\u0438\u043c \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0432 64-\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 title_features = layers.Embedding(num_words, 64)(title_input) # \u0412\u043b\u043e\u0436\u0438\u043c \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 64-\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 body_features = layers.Embedding(num_words, 64)(body_input)  # \u0421\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0434\u043e \u043e\u0434\u043d\u043e\u0433\u043e 128-\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 title_features = layers.LSTM(128)(title_features) # \u0421\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0434\u043e \u043e\u0434\u043d\u043e\u0433\u043e 32-\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 body_features = layers.LSTM(32)(body_features)  # \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043c \u0432\u0441\u0435 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438 \u0432 \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u0438 x = layers.concatenate([title_features, body_features, tags_input])  # \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044e \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u043f\u043e \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u043c priority_pred = layers.Dense(1, activation='sigmoid', name='priority')(x) # \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043e\u0442\u0434\u0435\u043b\u043e\u0432 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0430\u0445 department_pred = layers.Dense(num_departments, activation='softmax', name='department')(x)  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043a\u0432\u043e\u0437\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0438 \u043e\u0442\u0434\u0435\u043b model = keras.Model(inputs=[title_input, body_input, tags_input],                     outputs=[priority_pred, department_pred])<\/code><\/pre>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u0435\u0440\u0442\u0438\u043c \u0433\u0440\u0430\u0444 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">keras.utils.plot_model(model, 'multi_input_and_output_model.png', show_shapes=True)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/gc\/hk\/uw\/gchkuwc_zgefnaf4tx0ercck8bc.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0432\u044b\u0445\u043e\u0434\u0443.<\/p>\n<p>  \u0412\u044b \u0434\u0430\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c, \u0447\u0442\u043e\u0431\u044b \u0432\u0430\u0440\u044c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u043a\u043b\u0430\u0434 \u0432 \u043e\u0431\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0442\u0435\u0440\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"python\">model.compile(optimizer=keras.optimizers.RMSprop(1e-3),               loss=['binary_crossentropy', 'categorical_crossentropy'],               loss_weights=[1., 0.2])<\/code><\/pre>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0434\u0430\u043b\u0438 \u0438\u043c\u0435\u043d\u0430 \u043d\u0430\u0448\u0438\u043c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0441\u043b\u043e\u044f\u043c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0442\u0430\u043a\u0436\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c:<\/p>\n<pre><code class=\"python\">model.compile(optimizer=keras.optimizers.RMSprop(1e-3),               loss={'priority': 'binary_crossentropy',                     'department': 'categorical_crossentropy'},               loss_weights=[1., 0.2])<\/code><\/pre>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0441\u043f\u0438\u0441\u043a\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 Numpy \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u0435\u0442\u043e\u043a:<\/p>\n<pre><code class=\"python\">import numpy as np  # Dummy input data title_data = np.random.randint(num_words, size=(1280, 10)) body_data = np.random.randint(num_words, size=(1280, 100)) tags_data = np.random.randint(2, size=(1280, num_tags)).astype('float32') # Dummy target data priority_targets = np.random.random(size=(1280, 1)) dept_targets = np.random.randint(2, size=(1280, num_departments))  model.fit({'title': title_data, 'body': body_data, 'tags': tags_data},           {'priority': priority_targets, 'department': dept_targets},           epochs=2,           batch_size=32) <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 fit \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c <code>Dataset<\/code>, \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043b\u0438\u0431\u043e \u043a\u043e\u0440\u0442\u0435\u0436 \u0441\u043f\u0438\u0441\u043a\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a <code>([title_data, body_data, tags_data], [priority_targets, dept_targets])<\/code>, \u043b\u0438\u0431\u043e \u043a\u043e\u0440\u0442\u0435\u0436 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439 <code>({'title': title_data, 'body': body_data, 'tags': tags_data}, {'priority': priority_targets, 'department': dept_targets})<\/code>.<\/p>\n<h3>\u0423\u0447\u0435\u0431\u043d\u0430\u044f resnet \u043c\u043e\u0434\u0435\u043b\u044c<\/h3>\n<p>  \u0412 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a \u043c\u043e\u0434\u0435\u043b\u044f\u043c \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0432\u0445\u043e\u0434\u0430\u043c\u0438 \u0438 \u0432\u044b\u0445\u043e\u0434\u0430\u043c\u0438, Functional API \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u044f\u043c\u0438 \u0441 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c\u044e, \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u043e\u0438 \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0422\u0430\u043a\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Sequential API (\u044d\u0442\u043e \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f).<\/p>\n<p>  \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u2014 residual connections.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0443\u0447\u0435\u0431\u043d\u0443\u044e ResNet \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043b\u044f CIFAR10 \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e.<\/p>\n<pre><code class=\"python\">inputs = keras.Input(shape=(32, 32, 3), name='img') x = layers.Conv2D(32, 3, activation='relu')(inputs) x = layers.Conv2D(64, 3, activation='relu')(x) block_1_output = layers.MaxPooling2D(3)(x)  x = layers.Conv2D(64, 3, activation='relu', padding='same')(block_1_output) x = layers.Conv2D(64, 3, activation='relu', padding='same')(x) block_2_output = layers.add([x, block_1_output])  x = layers.Conv2D(64, 3, activation='relu', padding='same')(block_2_output) x = layers.Conv2D(64, 3, activation='relu', padding='same')(x) block_3_output = layers.add([x, block_2_output])  x = layers.Conv2D(64, 3, activation='relu')(block_3_output) x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(256, activation='relu')(x) x = layers.Dropout(0.5)(x) outputs = layers.Dense(10, activation='softmax')(x)  model = keras.Model(inputs, outputs, name='toy_resnet') model.summary()<\/code><\/pre>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u0435\u0440\u0442\u0438\u043c \u0433\u0440\u0430\u0444 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">keras.utils.plot_model(model, 'mini_resnet.png', show_shapes=True)<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qj\/vi\/bk\/qjvibkpx9zrbp09rcfhj_drtlzc.png\"><\/p>\n<p>  \u0418 \u043e\u0431\u0443\u0447\u0438\u043c \u0435\u0435:<\/p>\n<pre><code class=\"python\">(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data() x_train = x_train.astype('float32') \/ 255. x_test = x_test.astype('float32') \/ 255. y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10)  model.compile(optimizer=keras.optimizers.RMSprop(1e-3),               loss='categorical_crossentropy',               metrics=['acc']) model.fit(x_train, y_train,           batch_size=64,           epochs=1,           validation_split=0.2)<\/code><\/pre>\n<h2>\u0421\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0435\u0432<\/h2>\n<p>  \u0414\u0440\u0443\u0433\u0438\u043c \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Functional API \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 \u043e\u0431\u0449\u0438\u0435 \u0441\u043b\u043e\u0438. \u041e\u0431\u0449\u0438\u0435 \u0441\u043b\u043e\u0438 \u2014 \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0441\u043b\u043e\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u0438: \u043e\u043d\u0438 \u0438\u0437\u0443\u0447\u0430\u044e\u0442 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u0443\u0442\u044f\u043c \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043b\u043e\u0435\u0432.<\/p>\n<p>  \u041e\u0431\u0449\u0438\u0435 \u0441\u043b\u043e\u0438 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u0438\u0437 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432 (\u0441\u043a\u0430\u0436\u0435\u043c, \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0430, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u043e\u0431\u043c\u0435\u043d \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0432\u0445\u043e\u0434\u043e\u0432, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u0430 \u0432\u0441\u0435\u0445 \u0432\u0445\u043e\u0434\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u0449\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0439 \u0432 Functional API, \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u0438\u0442\u0435 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0441\u043b\u043e\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0434\u0435\u0441\u044c \u0441\u043b\u043e\u0439 <code>Embedding<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u043d\u0430 \u0434\u0432\u0443\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0432\u0445\u043e\u0434\u0430\u0445:<\/p>\n<pre><code class=\"python\"># \u0412\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f 1000 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u043b\u043e\u0432 \u0432 128-\u043c\u0435\u0440\u043d\u044b\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 shared_embedding = layers.Embedding(1000, 128)  # \u0426\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b text_input_a = keras.Input(shape=(None,), dtype='int32')  # \u0426\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b text_input_b = keras.Input(shape=(None,), dtype='int32')  # \u041c\u044b \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u043e\u0442 \u0436\u0435 \u0441\u043b\u043e\u0439 \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430 \u043e\u0431\u043e\u0438\u0445 \u0432\u0445\u043e\u0434\u0430\u0445 encoded_input_a = shared_embedding(text_input_a) encoded_input_b = shared_embedding(text_input_b)<\/code><\/pre>\n<h2>\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0432 \u0433\u0440\u0430\u0444\u0435 \u0441\u043b\u043e\u0435\u0432 <\/h2>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0433\u0440\u0430\u0444 \u0441\u043b\u043e\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0432\u044b \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u0443\u0435\u0442\u0435 \u0432 Functional API, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a \u043d\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0435\u0435. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c Functional \u043c\u043e\u0434\u0435\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0432\u0438\u0434\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>  \u042d\u0442\u043e \u0442\u0430\u043a\u0436\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f\u043c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432 (\u00ab\u0443\u0437\u043b\u043e\u0432\u00bb \u0432 \u0433\u0440\u0430\u0444\u0435) \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u0445. \u042d\u0442\u043e \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440!<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u042d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c VGG19 \u0441 \u0432\u0435\u0441\u0430\u043c\u0438 \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u043d\u0430 ImageNet:<\/p>\n<pre><code class=\"python\">from tensorflow.keras.applications import VGG19  vgg19 = VGG19()<\/code><\/pre>\n<p>  \u0418 \u044d\u0442\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0443\u0442\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0444\u0430:<\/p>\n<pre><code class=\"python\">features_list = [layer.output for layer in vgg19.layers]<\/code><\/pre>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u2014 \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0432 3 \u0441\u0442\u0440\u043e\u0447\u043a\u0438<\/p>\n<pre><code class=\"python\">feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list)  img = np.random.random((1, 224, 224, 3)).astype('float32') extracted_features = feat_extraction_model(img)<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 neural style transfer, \u043a\u0430\u043a \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445.<\/p>\n<h2>\u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 API \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432<\/h2>\n<p>  <code>tf.keras<\/code> \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0448\u0438\u0440\u043e\u043a\u0438\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432. \u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<p>  \u0421\u0432\u0435\u0440\u0442\u043e\u0447\u043d\u044b\u0435 \u0441\u043b\u043e\u0438: <code>Conv1D<\/code>, <code>Conv2D<\/code>, <code>Conv3D<\/code>, <code>Conv2DTranspose<\/code>, \u0438 \u0442.\u0434.<br \/>  \u0421\u043b\u043e\u0438 \u043f\u0443\u043b\u0438\u043d\u0433\u0430: <code>MaxPooling1D<\/code>, <code>MaxPooling2D<\/code>, <code>MaxPooling3D<\/code>, <code>AveragePooling1D<\/code>, \u0438 \u0442.\u0434.<br \/>  \u0421\u043b\u043e\u0438 RNN: <code>GRU<\/code>, <code>LSTM<\/code>, <code>ConvLSTM2D<\/code>, \u0438 \u0442.\u0434.<br \/>  <code>BatchNormalization<\/code>, <code>Dropout<\/code>, <code>Embedding<\/code>, \u0438 \u0442.\u0434.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u043d\u0430\u0448\u043b\u0438 \u0442\u043e, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c API \u0441\u043e\u0437\u0434\u0430\u0432 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u043e\u0439.<\/p>\n<p>  \u0412\u0441\u0435 \u0441\u043b\u043e\u0438 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u0443\u044e\u0442 \u043a\u043b\u0430\u0441\u0441 <code>Layer<\/code> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442:<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>call<\/code>, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0441\u043b\u043e\u0435\u043c.<br \/>  \u041c\u0435\u0442\u043e\u0434 <code>build<\/code>, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0441\u0430 \u0441\u043b\u043e\u044f (\u0437\u0430\u043c\u0435\u0442\u0438\u043c \u0447\u0442\u043e \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0441\u0442\u0438\u043b\u0435\u0432\u043e\u0435 \u0441\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0435; \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0435\u0441\u0430 \u0432 <code>__init__<\/code>).<\/p>\n<p>  \u0412\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>Dense<\/code> \u0441\u043b\u043e\u044f:<\/p>\n<pre><code class=\"python\">class CustomDense(layers.Layer):    def __init__(self, units=32):     super(CustomDense, self).__init__()     self.units = units    def build(self, input_shape):     self.w = self.add_weight(shape=(input_shape[-1], self.units),                              initializer='random_normal',                              trainable=True)     self.b = self.add_weight(shape=(self.units,),                              initializer='random_normal',                              trainable=True)    def call(self, inputs):     return tf.matmul(inputs, self.w) + self.b  inputs = keras.Input((4,)) outputs = CustomDense(10)(inputs)  model = keras.Model(inputs, outputs)<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0432\u0430\u0448 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0441\u043b\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u043b \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>get_config<\/code>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u043b\u043e\u044f:<\/p>\n<pre><code class=\"python\">class CustomDense(layers.Layer):    def __init__(self, units=32):     super(CustomDense, self).__init__()     self.units = units    def build(self, input_shape):     self.w = self.add_weight(shape=(input_shape[-1], self.units),                              initializer='random_normal',                              trainable=True)     self.b = self.add_weight(shape=(self.units,),                              initializer='random_normal',                              trainable=True)    def call(self, inputs):     return tf.matmul(inputs, self.w) + self.b    def get_config(self):     return {'units': self.units}   inputs = keras.Input((4,)) outputs = CustomDense(10)(inputs)  model = keras.Model(inputs, outputs) config = model.get_config()  new_model = keras.Model.from_config(     config, custom_objects={'CustomDense': CustomDense})<\/code><\/pre>\n<p>  \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 <code>from_config (cls, config)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u043b\u043e\u044f, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>from_config<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def from_config(cls, config):   return cls(**config)<\/code><\/pre>\n<h2>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Functional API<\/h2>\n<p>  \u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Functional API \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>Model<\/code> \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e?<\/p>\n<p>  \u0412 \u0446\u0435\u043b\u043e\u043c, Functional API \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438, \u043e\u043d \u0438\u043c\u0435\u0435\u0442 \u0440\u044f\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438.<\/p>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 Model \u0434\u0430\u0435\u0442 \u0432\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043b\u0435\u0433\u043a\u043e \u0432 \u0432\u0438\u0434\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0441\u043b\u043e\u0435\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c Tree-RNN \u0441 Functional API, \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e <code>Model<\/code>).<\/p>\n<h3>C\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b Functional API:<\/h3>\n<p>  \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043d\u0438\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u0432\u0435\u0440\u043d\u044b\u043c\u0438 \u0438 \u0434\u043b\u044f Sequential \u043c\u043e\u0434\u0435\u043b\u0435\u0439 (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445), \u043d\u043e \u043e\u043d\u0438 \u0432\u0435\u0440\u043d\u044b \u0434\u043b\u044f \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u043a\u043e\u0434 Python, \u0430 \u043d\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445).<\/p>\n<h4>\u0421 Functional API \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u043a\u043e\u0434.<\/h4>\n<p>  \u041d\u0435\u0442 <code>super(MyClass, self).__init__(...)<\/code>, \u043d\u0435\u0442 <code>def call(self, ...):<\/code>, \u0438 \u0442.\u0434.<\/p>\n<p>  \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435:<\/p>\n<pre><code class=\"python\">inputs = keras.Input(shape=(32,)) x = layers.Dense(64, activation='relu')(inputs) outputs = layers.Dense(10)(x) mlp = keras.Model(inputs, outputs)<\/code><\/pre>\n<p>  \u0421 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439:<\/p>\n<pre><code class=\"python\">class MLP(keras.Model):    def __init__(self, **kwargs):     super(MLP, self).__init__(**kwargs)     self.dense_1 = layers.Dense(64, activation='relu')     self.dense_2 = layers.Dense(10)    def call(self, inputs):     x = self.dense_1(inputs)     return self.dense_2(x)  # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043c\u043e\u0434\u0435\u043b\u0438. mlp = MLP() # \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. # \u0423 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u043a\u0430 \u043e\u043d\u0430 \u043d\u0435 \u0431\u044b\u043b\u0430 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0445\u043e\u0442\u044f \u0431\u044b \u0440\u0430\u0437. _ = mlp(tf.zeros((1, 32))) <\/code><\/pre>\n<h4>\u0412\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u043e \u0445\u043e\u0434\u0443 \u0435\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f<\/h4>\n<p>  \u0412 Functional API \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 (shape \u0438 dtype) \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435 (\u0447\u0435\u0440\u0435\u0437 `Input`), \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0441\u043b\u043e\u0439, \u0441\u043b\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0447\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0435\u043c\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0442\u043e \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<p>  \u042d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043b\u044e\u0431\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u0435 \u0441 Functional API \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0441\u044f. \u0412\u0441\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0430 (\u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0430\u044f\u0441\u044f \u043a \u043e\u0442\u043b\u0430\u0434\u043a\u0435 \u0441\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438) \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u0430 \u043d\u0435 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0442\u0438\u043f\u0430 \u0432 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0435.<\/p>\n<h4>\u0412\u0430\u0448\u0443 Functional \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u0430.<\/h4>\n<p>  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0447\u0435\u0440\u0442\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u0432\u0438\u0434\u0435 \u0433\u0440\u0430\u0444\u0430, \u0438 \u0432\u044b \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u043c \u0443\u0437\u043b\u0430\u043c \u0433\u0440\u0430\u0444\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0441\u043b\u043e\u0435\u0432, \u043a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0435\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435:<\/p>\n<p>  <code>features_list = [layer.output for layer in vgg19.layers]<br \/>  feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list)<\/code><\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 Functional \u043c\u043e\u0434\u0435\u043b\u044c \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u043c \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430, \u043e\u043d\u0430 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0430 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u0432\u043e\u0441\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0442\u0443 \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u0431\u0435\u0437 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443.<\/p>\n<h3>\u0421\u043b\u0430\u0431\u044b\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b Functional API<\/h3>\n<p>  <\/p>\n<h4>\u041e\u043d \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u044b\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b.<\/h4>\n<p>  Functional API \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u0430\u043a DAG \u0441\u043b\u043e\u0435\u0432. \u042d\u0442\u043e \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043d\u043e \u043d\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u0441\u0435\u0442\u0438 \u0438\u043b\u0438 Tree RNN \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0432 Functional API.<\/p>\n<h4>\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u0441 \u043d\u0443\u043b\u044f.<\/h4>\n<p>  \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u00ab\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f DAG \u0441\u043b\u043e\u0435\u0432\u00bb: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0432\u043e\u0434\u0430 \u043d\u0430 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0435 \u0432\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u042d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<h2>\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u0438\u043b\u0435\u0439 API<\/h2>\n<p>  \u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0431\u043e\u0440 \u043c\u0435\u0436\u0434\u0443 Functional API \u0438\u043b\u0438 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c Model \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u0430\u0441 \u043e\u0434\u043d\u043e\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0412\u0441\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 API tf.keras \u043c\u043e\u0433\u0443\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c, \u0431\u0443\u0434\u044c \u0442\u043e Sequential \u043c\u043e\u0434\u0435\u043b\u0438, Functional \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u043b\u0438 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 Models\/Layers, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0441 \u043d\u0443\u043b\u044f.<\/p>\n<p>  \u0412\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Functional \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u043b\u0438 Sequential \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e Model\/Layer:<\/p>\n<pre><code class=\"python\">units = 32 timesteps = 10 input_dim = 5  # Define a Functional model inputs = keras.Input((None, units)) x = layers.GlobalAveragePooling1D()(inputs) outputs = layers.Dense(1, activation='sigmoid')(x) model = keras.Model(inputs, outputs)   class CustomRNN(layers.Layer):    def __init__(self):     super(CustomRNN, self).__init__()     self.units = units     self.projection_1 = layers.Dense(units=units, activation='tanh')     self.projection_2 = layers.Dense(units=units, activation='tanh')     # Our previously-defined Functional model     self.classifier = model    def call(self, inputs):     outputs = []     state = tf.zeros(shape=(inputs.shape[0], self.units))     for t in range(inputs.shape[1]):       x = inputs[:, t, :]       h = self.projection_1(x)       y = h + self.projection_2(state)       state = y       outputs.append(y)     features = tf.stack(outputs, axis=1)     print(features.shape)     return self.classifier(features)  rnn_model = CustomRNN() _ = rnn_model(tf.zeros((1, timesteps, input_dim)))  <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u043d\u043e, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0441\u0430\u0431\u043a\u043b\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 Layer \u0438\u043b\u0438 Model \u0432 Functional API \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043c\u0435\u0442\u043e\u0434 <code>call<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432:<\/p>\n<p>  <code>call(self, inputs, **kwargs)<\/code> \u0433\u0434\u0435 <code>inputs<\/code> \u044d\u0442\u043e \u0442\u0435\u043d\u0437\u043e\u0440 \u0438\u043b\u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u0442\u0443\u0440\u0430 \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432 (\u043d\u0430\u043f\u0440. \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432), \u0438 \u0433\u0434\u0435 <code>**kwargs<\/code> \u044d\u0442\u043e \u043d\u0435\u0442\u0435\u043d\u0437\u043e\u0440\u043d\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b (\u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435).<br \/>  <code>call(self, inputs, training=None, **kwargs)<\/code> \u0433\u0434\u0435 <code>training<\/code> \u044d\u0442\u043e \u0431\u0443\u043b\u0435\u0432\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0432 \u043a\u0430\u043a\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u0441\u043b\u043e\u0439, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u0432\u044b\u0432\u043e\u0434\u0430.<br \/>  <code>call(self, inputs, mask=None, **kwargs)<\/code> \u0433\u0434\u0435 <code>mask<\/code> \u044d\u0442\u043e \u0442\u0435\u043d\u0437\u043e\u0440 \u0431\u0443\u043b\u0435\u0432\u043e\u0439 \u043c\u0430\u0441\u043a\u0438 (\u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f RNN, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440).<br \/>  <code>call(self, inputs, training=None, mask=None, **kwargs)<\/code> \u2014 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0445 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u044f.<\/p>\n<p>  \u0412 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 `get_config` \u043d\u0430 \u0432\u0430\u0448\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c Layer \u0438\u043b\u0438 Model, Functional \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u0442\u0435 \u0441 \u043d\u0438\u043c \u0431\u0443\u0434\u0443\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u044b \u0438 \u043a\u043b\u043e\u043d\u0438\u0440\u0443\u0435\u043c\u044b.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0433\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 RNN \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0441 \u043d\u0443\u043b\u044f Functional \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">units = 32 timesteps = 10 input_dim = 5 batch_size = 16   class CustomRNN(layers.Layer):    def __init__(self):     super(CustomRNN, self).__init__()     self.units = units     self.projection_1 = layers.Dense(units=units, activation='tanh')     self.projection_2 = layers.Dense(units=units, activation='tanh')     self.classifier = layers.Dense(1, activation='sigmoid')    def call(self, inputs):     outputs = []     state = tf.zeros(shape=(inputs.shape[0], self.units))     for t in range(inputs.shape[1]):       x = inputs[:, t, :]       h = self.projection_1(x)       y = h + self.projection_2(state)       state = y       outputs.append(y)     features = tf.stack(outputs, axis=1)     return self.classifier(features)  # \u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435 \u0447\u0442\u043e \u043c\u044b \u0437\u0430\u0434\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 # \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c `batch_shape`, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f `CustomRNN` \u0442\u0440\u0435\u0431\u0443\u044e\u0442  # \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 (\u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u0442\u0435\u043d\u0437\u043e\u0440\u044b `state`). inputs = keras.Input(batch_shape=(batch_size, timesteps, input_dim)) x = layers.Conv1D(32, 3)(inputs) outputs = CustomRNN()(x)  model = keras.Model(inputs, outputs)  rnn_model = CustomRNN() _ = rnn_model(tf.zeros((1, 10, 5))) <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u043d\u0430\u0448\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e Functional API!<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u0432\u0430\u0441 \u043f\u043e\u0434 \u0440\u0443\u043a\u043e\u0439 \u043c\u043e\u0449\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  <i>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0442\u0430\u043a\u0436\u0435 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 Tensorflow.org. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0441\u0430\u0439\u0442\u0430 Tensorflow.org \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u0438\u0439, \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435\u0441\u044c \u0432 \u043b\u0438\u0447\u043a\u0443 \u0438\u043b\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438. \u041b\u044e\u0431\u044b\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 GoogLeNet, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u043e\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u043c \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0433\u0440\u0430\u0444\u043e\u043c.<\/i><\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <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\/post\/483664\/\"> https:\/\/habr.com\/ru\/post\/483664\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/483664\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/w1\/zr\/n8\/w1zrn8ydafoxahso_ig7vx1stfg.png\"><\/p>\n<p>  \u0412 Keras \u0435\u0441\u0442\u044c \u0434\u0432\u0430 API \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439 Sequential \u0438 Functional. \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Functional API \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0412 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0430, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c Functional API, \u0441 \u0441\u0430\u0439\u0442\u0430 TensorFlow.  <\/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-297060","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297060","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=297060"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297060\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}