{"id":326464,"date":"2021-07-15T03:00:17","date_gmt":"2021-07-15T03:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326464"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326464","title":{"rendered":"\u041c\u043e\u0434\u0435\u043b\u0438 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439 sequence-to-sequence \u043d\u0430 PyTorch \u0427\u0430\u0441\u0442\u044c3"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u043f\u043e\u0441\u0442\u0435 \u043e \u043c\u043e\u0434\u0435\u043b\u044f\u0445 sequence-to-sequence \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c PyTorch \u0438 torchText \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0437 \u0441\u0442\u0430\u0442\u044c <a href=\"https:\/\/arxiv.org\/abs\/1409.0473\" rel=\"noopener noreferrer nofollow\">Neural Machine Translation by Jointly Learning to Align and Translate<\/a>. \u042d\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043b\u0443\u0447\u0448\u0443\u044e \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0438\u0437 \u0438\u0437 \u0442\u0440\u0451\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 &nbsp;27\u043f\u043e\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e\u0441&nbsp;34\u0443\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439\u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<p>\u041a\u0430\u043a \u0438 \u0440\u0430\u043d\u0435\u0435, \u0435\u0441\u043b\u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043f\u043e\u0441\u0442\u0430 \u0432\u0430\u0441 \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442, \u0442\u043e \u043d\u0438\u0436\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u0438 \u0440\u0443\u0441\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e jupyter notebook:<\/p>\n<p><a href=\"https:\/\/github.com\/bentrevett\/pytorch-seq2seq\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <\/a>(<a href=\"https:\/\/colab.research.google.com\/github\/bentrevett\/pytorch-seq2seq\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">Open jupyter notebook In Colab<\/a>)<\/p>\n<p><a href=\"https:\/\/github.com\/vasiliyeskin\/bentrevett-pytorch-seq2seq_ru\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">\u0420\u0443\u0441\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f<\/a> (<a href=\"https:\/\/colab.research.google.com\/github\/vasiliyeskin\/bentrevett-pytorch-seq2seq_ru\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">Open jupyter notebook In Colab<\/a>)<\/p>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043e\u0431\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0434\u0435\u0440\u0430-\u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2ea\/527\/37f\/2ea52737f8b23db2c21775bfefb6bbdc.png\" width=\"695\" height=\"421\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0448\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0431\u044b\u043b\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u00ab\u0441\u0436\u0430\u0442\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438\u00bb \u043f\u0443\u0442\u0435\u043c \u044f\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"z\" alt=\"z\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7ec\/96d\/36d\/7ec96d36dba26495da3f67e05d5664f7.svg\"> \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043d\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 <img decoding=\"async\" class=\"formula inline\" source=\"f\" alt=\"f\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e5\/88c\/d87\/1e588cd876440b728158ac30c45b256c.svg\"> \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0448\u0430\u0433\u0435, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430, <img decoding=\"async\" class=\"formula inline\" source=\"d(y_t)\" alt=\"d(y_t)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/30b\/16b\/216\/30b16b216aadf84c551bd753294eb842.svg\"> \u0438 \u0441\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c <img decoding=\"async\" class=\"formula inline\" source=\"s_t\" alt=\"s_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/723\/69c\/208\/72369c208fe2ac204aa94b47600725ce.svg\">.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8cd\/1cb\/6fa\/8cd1cb6fa9090497ea69f677c389aa13.png\" width=\"695\" height=\"421\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u0438 \u0441\u0436\u0430\u0442\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043d\u0430\u0448 \u0432\u0435\u043a\u0442\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041c\u043e\u0434\u0435\u043b\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0432 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435, \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0436\u0430\u0442\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0443 \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437\u0435\u0433\u043e\u0441\u043a\u0440\u044b\u0442\u044b\u0435\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f! \u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c? \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f <em>\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e<\/em>.<\/p>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"a\" alt=\"a\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e04\/700\/545\/e0470054506370127494bb7edba04900.svg\">. \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1, \u0430 \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0440\u0430\u0432\u043d\u0430 1. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/055\/7d3\/ed7\/0557d3ed71fe796b32ca57beeb99da8c.svg\">, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 <img decoding=\"async\" class=\"formula inline\" source=\"w\" alt=\"w\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ca7\/1d7\/b03\/ca71d7b031d0700f5243e3ca6de5e8ea.svg\">.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"w = \\sum_{i}a_ih_i\" alt=\"w = \\sum_{i}a_ih_i\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/946\/057\/058\/9460570589315cd4ac5ecb2497d6a383.svg\" width=\"106\" height=\"45\"><\/p>\n<p>\u041c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0448\u0430\u0433\u0435 \u043f\u0440\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0435\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041c\u044b \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043c, \u043a\u0430\u043a \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u0430\u043b\u0435\u0435.<\/p>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>\u0421\u043d\u043e\u0432\u0430 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0439.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438.<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F  from torchtext.legacy.datasets import Multi30k from torchtext.legacy.data import Field, BucketIterator  import spacy import numpy as np  import random import math import time<\/code><\/pre>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<pre><code class=\"python\">SEED = 1234  random.seed(SEED) np.random.seed(SEED) torch.manual_seed(SEED) torch.cuda.manual_seed(SEED) torch.backends.cudnn.deterministic = True\t<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043d\u0435\u043c\u0435\u0446\u043a\u0443\u044e \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u0438 spaCy.<\/p>\n<pre><code class=\"python\">python -m spacy download en_core_web_sm python -m spacy download de_core_news_sm<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 Google Colab \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 colab runtime! \u041d\u0430\u0438\u0431\u044b\u0441\u0442\u0440\u0435\u0439\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u043a\u043e\u043c\u0430\u0434\u0443\uff1a <strong>Ctrl + M + .<\/strong>):<\/p>\n<pre><code class=\"python\">!pip install -U spacy==3.0 !python -m spacy download en_core_web_sm !python -m spacy download de_core_news_sm<\/code><\/pre>\n<pre><code class=\"python\">spacy_de = spacy.load('de_core_news_sm') spacy_en = spacy.load('en_core_web_sm')<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u044b.<\/p>\n<pre><code class=\"python\">def tokenize_de(text):     \"\"\"     Tokenizes German text from a string into a list of strings     \"\"\"     return [tok.text for tok in spacy_de.tokenizer(text)]  def tokenize_en(text):     \"\"\"     Tokenizes English text from a string into a list of strings     \"\"\"     return [tok.text for tok in spacy_en.tokenizer(text)]<\/code><\/pre>\n<p>\u041f\u043e\u043b\u044f \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0442\u0435\u043c\u0438 \u0436\u0435, \u0447\u0442\u043e \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<pre><code class=\"python\">SRC = Field(tokenize = tokenize_de,              init_token = '&lt;sos&gt;',              eos_token = '&lt;eos&gt;',              lower = True)  TRG = Field(tokenize = tokenize_en,              init_token = '&lt;sos&gt;',              eos_token = '&lt;eos&gt;',              lower = True)<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<pre><code class=\"python\">train_data, valid_data, test_data = Multi30k.splits(exts = ('.de', '.en'),                                                      fields = (SRC, TRG))<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0438.<\/p>\n<pre><code class=\"python\">SRC.build_vocab(train_data, min_freq = 2) TRG.build_vocab(train_data, min_freq = 2)<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<pre><code class=\"python\">device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b.<\/p>\n<pre><code class=\"python\">BATCH_SIZE = 128  train_iterator, valid_iterator, test_iterator = BucketIterator.splits(     (train_data, valid_data, test_data),      batch_size = BATCH_SIZE,     device = device)<\/code><\/pre>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 Seq2Seq<\/h3>\n<h4>\u041a\u043e\u0434\u0435\u0440<\/h4>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u0434\u0435\u0440. \u041a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0441\u043b\u043e\u0439 GRU, \u043e\u0434\u043d\u0430\u043a\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0434 <em>\u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN<\/em>. \u0412 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 RNN \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435. <em>\u0412\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f RNN<\/em> \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430, \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e\u043d\u0438\u0436\u0435\u0437\u0435\u043b\u0435\u043d\u044b\u043c\u0446\u0432\u0435\u0442\u043e\u043c, \u0430 <em>\u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f RNN<\/em> \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433, \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0431\u0438\u0440\u044e\u0437\u043e\u0432\u044b\u0439. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043a\u043e\u0434\u0435 \u2014 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>bidirectional = True<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430 \u0432 RNN, \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b24\/32e\/e39\/b2432ee39f34e971a66baacb43ac6586.png\" width=\"689\" height=\"362\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"\\begin{align*} h_t^\\rightarrow &amp;= \\text{EncoderGRU}^\\rightarrow(e(x_t^\\rightarrow),h_{t-1}^\\rightarrow)\\\\ h_t^\\leftarrow &amp;= \\text{EncoderGRU}^\\leftarrow(e(x_t^\\leftarrow),h_{t-1}^\\leftarrow) \\end{align*}\" alt=\"\\begin{align*} h_t^\\rightarrow &amp;= \\text{EncoderGRU}^\\rightarrow(e(x_t^\\rightarrow),h_{t-1}^\\rightarrow)\\\\ h_t^\\leftarrow &amp;= \\text{EncoderGRU}^\\leftarrow(e(x_t^\\leftarrow),h_{t-1}^\\leftarrow) \\end{align*}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eb5\/a4e\/26f\/eb5a4e26f2088ce3a5eb592c41c27c3d.svg\" width=\"298\" height=\"54\"><\/p>\n<p>\u0413\u0434\u0435 <img decoding=\"async\" class=\"formula inline\" source=\"x_0^\\rightarrow = \\text{}, x_1^\\rightarrow = \\text{guten}\" alt=\"x_0^\\rightarrow = \\text{}, x_1^\\rightarrow = \\text{guten}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/362\/636\/7a7\/3626367a72706b3ceed5464554149cfe.svg\"> \u0438 <img decoding=\"async\" class=\"formula inline\" source=\"x_0^\\leftarrow = \\text{}, x_1^\\leftarrow = \\text{morgen}\" alt=\"x_0^\\leftarrow = \\text{}, x_1^\\leftarrow = \\text{morgen}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3cd\/e4e\/8cd\/3cde4e8cd62faa0c869e86e8b5c538f6.svg\">.<\/p>\n<p>\u041a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 RNN \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0432\u043e\u0434 (<code>embedded<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 PyTorch \u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u044f\u043c\u043e\u0435, \u0442\u0430\u043a \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f ( <img decoding=\"async\" class=\"formula inline\" source=\"h_0^\\rightarrow\" alt=\"h_0^\\rightarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/121\/a75\/c88\/121a75c88c716cf5c46a2306af1e2695.svg\">and <img decoding=\"async\" class=\"formula inline\" source=\"h_0^\\leftarrow\" alt=\"h_0^\\leftarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ba0\/82b\/39b\/ba082b39be4647b369c4057e1a5407bd.svg\">, respectively) \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u043c \u0441 \u043d\u0443\u043b\u0435\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430: \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u044f\u043c\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u043d\u0430 \u0443\u0432\u0438\u0434\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043b\u043e\u0432\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"z^\\rightarrow=h_T^\\rightarrow\" alt=\"z^\\rightarrow=h_T^\\rightarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c3e\/a6a\/bd4\/c3ea6abd47d9693fce863f4bb3be3160.svg\">, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0438\u0437 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u043d\u0430 \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"z^\\leftarrow=h_T^\\leftarrow\" alt=\"z^\\leftarrow=h_T^\\leftarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad6\/8b5\/8a8\/ad68b58a8117c63c66e8185f1453bc9a.svg\">.<\/p>\n<p>RNN \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>outputs<\/code> \u0438 <code>hidden<\/code>.<\/p>\n<p><code>outputs<\/code> \u0438\u043c\u0435\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 <strong>srclen,batchsize,hiddim\u2217numdirections<\/strong> \u0433\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 <code>hid_dim<\/code> \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0438 &#8212; \u044d\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 <code>hid_dim<\/code> \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u2014 \u044d\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0443\u043c\u0430\u0442\u044c \u043e \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0438 \u043a\u0430\u043a \u043e \u043f\u0440\u044f\u043c\u043e\u043c \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u0445, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c, \u0442.\u0435. <img decoding=\"async\" class=\"formula inline\" source=\"h_1 = [h_1^\\rightarrow; h_{T}^\\leftarrow]\" alt=\"h_1 = [h_1^\\rightarrow; h_{T}^\\leftarrow]\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/553\/322\/361\/553322361e9cc75e0461c58e76fcdeea.svg\">, <img decoding=\"async\" class=\"formula inline\" source=\"h_2 = [h_2^\\rightarrow; h_{T-1}^\\leftarrow]\" alt=\"h_2 = [h_2^\\rightarrow; h_{T-1}^\\leftarrow]\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/68b\/e1e\/769\/68be1e7698ef1966fb021b45b050a52d.svg\"> \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u043f\u0440\u044f\u043c\u043e\u0435\u0438\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435\u0441\u0446\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u0432\u043c\u0435\u0441\u0442\u0435 \u043a\u0430\u043a <img decoding=\"async\" class=\"formula inline\" source=\"H={ h_1, h_2, ..., h_T}\" alt=\"H={ h_1, h_2, ..., h_T}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fc2\/49a\/b2f\/fc249ab2f27ea51d7f915b00b0a5b558.svg\"> \u0442\u0435\u043d\u0437\u043e\u0440\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<\/p>\n<p><code>hidden<\/code> \u0438\u043c\u0435\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 <strong>nlayers\u2217numdirections,batchsize,hiddim<\/strong>, where <strong>\u22122,:,:<\/strong> \u0434\u0430\u0435\u0442 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0442.\u0435.\u043f\u043e\u0441\u043b\u0435\u0442\u043e\u0433\u043e,\u043a\u0430\u043a\u043e\u043d\u0443\u0432\u0438\u0434\u0435\u043b\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435\u0441\u043b\u043e\u0432\u043e\u0432\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 <strong>\u22121,:,:<\/strong> \u0434\u0430\u0435\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u0443\u0440\u043e\u0432\u043d\u044e \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0442\u043d\u043e-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0442.\u0435.\u043f\u043e\u0441\u043b\u0435\u0442\u043e\u0433\u043e,\u043a\u0430\u043a\u043e\u043d\u0443\u0432\u0438\u0434\u0435\u043b\u043f\u0435\u0440\u0432\u043e\u0435\u0441\u043b\u043e\u0432\u043e\u0432\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u043c, \u0435\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"z\" alt=\"z\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b7b\/c79\/7de\/b7bc797de524f355e76f07baf7ca5f68.svg\"> \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"s_0\" alt=\"s_0\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0d3\/283\/e4d\/0d3283e4d84341a31999db239ac87971.svg\">, \u043d\u043e \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 ( <img decoding=\"async\" class=\"formula inline\" source=\"z^\\rightarrow=h_T^\\rightarrow\" alt=\"z^\\rightarrow=h_T^\\rightarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/688\/ac7\/e9c\/688ac7e9c26c05b52a9776f89d617945.svg\"> \u0438 <img decoding=\"async\" class=\"formula inline\" source=\"z^\\leftarrow=h_T^\\leftarrow\" alt=\"z^\\leftarrow=h_T^\\leftarrow\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/614\/3f5\/6df\/6143f56df25d6e4d917776fc5ee40c70.svg\">, respectively). \u041c\u044b \u0440\u0435\u0448\u0430\u0435\u043c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0432 \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u0435, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0432 \u0438\u0445 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 <img decoding=\"async\" class=\"formula inline\" source=\"g\" alt=\"g\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d54\/3f4\/463\/d543f4463bf356c0e820ab2958fe69b7.svg\"> \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"\\tanh\" alt=\"\\tanh\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6c9\/2da\/4bc\/6c92da4bc6e91d9d24844385a0aa77f4.svg\">.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"\\begin{align*} z=\\tanh(g(z^\\rightarrow, z^\\leftarrow))\\\\ z^\\rightarrow=h_T^\\rightarrow, z^\\leftarrow=h_T^\\leftarrow, z = s_0 \\end{align*}\" alt=\"\\begin{align*} z=\\tanh(g(z^\\rightarrow, z^\\leftarrow))\\\\ z^\\rightarrow=h_T^\\rightarrow, z^\\leftarrow=h_T^\\leftarrow, z = s_0 \\end{align*}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/41a\/da8\/74a\/41ada874a972d33bc9360cdf0668fd7d.svg\" width=\"231\" height=\"51\"><\/p>\n<p><strong>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/strong>: \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043a\u043b\u043e\u043d\u0435\u043d\u0438\u0435 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0430\u0432\u0442\u043e\u0440\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u043e\u0435 \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 RNN \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430. \u042d\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0430 \u0432\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <code>outputs<\/code>, \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u0445 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432\u043f\u0435\u0440\u0435\u0434 \u0438 \u043d\u0430\u0437\u0430\u0434 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <code>hidden<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0435.<\/p>\n<pre><code class=\"python\">class Encoder(nn.Module):     def __init__(self, input_dim, emb_dim, enc_hid_dim, dec_hid_dim, dropout):         super().__init__()                  self.embedding = nn.Embedding(input_dim, emb_dim)                  self.rnn = nn.GRU(emb_dim, enc_hid_dim, bidirectional = True)                  self.fc = nn.Linear(enc_hid_dim * 2, dec_hid_dim)                  self.dropout = nn.Dropout(dropout)              def forward(self, src):                  #src = [src len, batch size]                  embedded = self.dropout(self.embedding(src))                  #embedded = [src len, batch size, emb dim]                  outputs, hidden = self.rnn(embedded)                          #outputs = [src len, batch size, hid dim * num directions]         #hidden = [n layers * num directions, batch size, hid dim]                  #hidden is stacked [forward_1, backward_1, forward_2, backward_2, ...]         #outputs are always from the last layer                  #hidden [-2, :, : ] is the last of the forwards RNN          #hidden [-1, :, : ] is the last of the backwards RNN                  #initial decoder hidden is final hidden state of the forwards and backwards          #  encoder RNNs fed through a linear layer         hidden = torch.tanh(self.fc(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim = 1)))                  #outputs = [src len, batch size, enc hid dim * 2]         #hidden = [batch size, dec hid dim]                  return outputs, hidden<\/code><\/pre>\n<h4>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/h4>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0441\u043b\u043e\u0439 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u042d\u0442\u043e\u0442 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1}\" alt=\"s_{t-1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6f1\/abe\/7ae\/6f1abe7aecbdfc462ee2ce0286fe12dc.svg\"> \u0438 \u0432\u0441\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0434\u0435\u0440\u0430, \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0435\u043d\u0437\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b35\/4c8\/f64\/b354c8f64ae7674cd593f32131cf748d.svg\">. \u0421\u043b\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1a3\/f76\/f94\/1a3f76f94d0129502c4f1ad7e9c16759.svg\"> \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1, \u0430 \u0432\u0441\u044f \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0440\u0430\u0432\u043d\u0430 1.<\/p>\n<p>\u0418\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0441\u043b\u043e\u0439 \u0431\u0435\u0440\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0443\u0436\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438, <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1}\" alt=\"s_{t-1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d5f\/3f7\/c33\/d5f3f7c33c170f5b28b8534ee0e1407c.svg\">, \u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u044b \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0432 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8be\/f73\/bff\/8bef73bff80aa9f1d47677e8d9bed252.svg\">, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cb6\/4ee\/828\/cb64ee82821bb8435d27f5607057a7ea.svg\">, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442, \u043a\u0430\u043a\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u043c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0434\u0435\u043b\u044f\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043b\u043e\u0432\u0430 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c, <img decoding=\"async\" class=\"formula inline\" source=\"\\hat{y}_{t+1}\" alt=\"\\hat{y}_{t+1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/678\/239\/7d4\/6782397d464c6d801ed1290013e054e4.svg\">.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c <em>\u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f<\/em> \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u043c\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438 \u043a\u043e\u0434\u0435\u0440\u0430. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0435\u0440\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <img decoding=\"async\" class=\"formula inline\" source=\"T\" alt=\"T\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b74\/c1f\/1fb\/b74c1f1fb9548d9fce18288539a5f8bd.svg\">\u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432, \u0438 \u043d\u0430\u0448\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0439 \u0442\u0435\u043d\u0437\u043e\u0440, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c, \u044d\u0442\u043e <code>\u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c<\/code> \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"T\" alt=\"T\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5ab\/9cb\/fd3\/5ab9cbfd3a3bbaf1605982d558a76753.svg\"> \u0440\u0430\u0437. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"E_t\" alt=\"E_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7a0\/0ee\/957\/7a00ee9579d2d8d686906da0f4d4d47f.svg\"> \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0432 \u0438\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0432 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 (<code>attn<\/code>) \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"\\tanh\" alt=\"\\tanh\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5ce\/1d3\/687\/5ce1d368722dc258571aa0972db58d71.svg\">.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"E_t = \\tanh(\\text{attn}(s_{t-1}, H))\" alt=\"E_t = \\tanh(\\text{attn}(s_{t-1}, H))\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cd8\/5f5\/717\/cd85f5717610811f9f65b119b23dc907.svg\" width=\"211\" height=\"22\"><\/p>\n<p>\u042d\u0442\u0443 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0434\u0435\u0440\u0430 \u00ab\u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442\u00bb \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430.<\/p>\n<p>\u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <strong>dec hid dim, src len<\/strong> \u0442\u0435\u043d\u0437\u043e\u0440 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u0431\u0430\u0442\u0447\u0435. \u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0431\u044b\u043b \u0434\u043b\u0438\u043d\u044b <strong>src len<\/strong> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u0431\u0430\u0442\u0447\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f <code>\u044d\u043d\u0435\u0440\u0433\u0438\u0438<\/code> \u043d\u0430 <strong>1, dec hid dim<\/strong>-\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0439 \u0442\u0435\u0437\u043e\u0440 <img decoding=\"async\" class=\"formula inline\" source=\"v\" alt=\"v\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9b7\/076\/1e9\/9b70761e9aca6e0ad9d72af3dc69d706.svg\">.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"\\hat{a}_t = v E_t\" alt=\"\\hat{a}_t = v E_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ef1\/81b\/85f\/ef181b85fe11fe6e4115a5994e3a159a.svg\" width=\"74\" height=\"20\"><\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0443\u043c\u0430\u0442\u044c \u043e <img decoding=\"async\" class=\"formula inline\" source=\"v\" alt=\"v\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8a7\/351\/57b\/8a735157bae423cfc1c9347784d52898.svg\">\u043a\u0430\u043a \u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0435\u0441\u043e\u0432 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u044d\u043d\u0435\u0440\u0433\u0438\u0438 \u043f\u043e \u0432\u0441\u0435\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430. \u042d\u0442\u0438 \u0432\u0435\u0441\u0430 \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u043d\u0430\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0434\u0435\u043b\u044f\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0442\u043e\u043a\u0435\u043d\u0443 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <img decoding=\"async\" class=\"formula inline\" source=\"v\" alt=\"v\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/476\/168\/eb4\/476168eb4426e4334bae028291680dca.svg\">\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u043e \u0438\u0437\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043a\u0438. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043a\u0430\u043a v \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0438 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f <img decoding=\"async\" class=\"formula inline\" source=\"v\" alt=\"v\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7ad\/b2a\/31b\/7adb2a31bca8422eec34f0840c8cba5f.svg\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c <img decoding=\"async\" class=\"formula inline\" source=\"v\" alt=\"v\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1b4\/8ea\/3a3\/1b48ea3a393f6ef695ce3b5e0b93ed06.svg\"> \u043a\u0430\u043a \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u0431\u0435\u0437 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043c\u044b \u0441\u043b\u0435\u0434\u0438\u043c \u0437\u0430 \u0442\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c, \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u044b\u043c \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u044d\u0442\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439  <img decoding=\"async\" class=\"formula inline\" source=\"\\text{softmax}\" alt=\"\\text{softmax}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/378\/cbd\/2ef\/378cbd2ef01ec7c6917e6dc46cf42f8c.svg\">: \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 0 \u0438 1, \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0430\u0451\u0442 1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"a_t = \\text{softmax}(\\hat{a_t})\" alt=\"a_t = \\text{softmax}(\\hat{a_t})\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/813\/e18\/e4d\/813e18e4da302da678d341b572875943.svg\" width=\"142\" height=\"22\"><\/p>\n<p>\u042d\u0442\u043e \u043f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044e!<\/p>\n<p>\u0413\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435. \u0422\u0430\u043a \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f  <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1} = s_0 = z\" alt=\"s_{t-1} = s_0 = z\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9a1\/dcf\/cba\/9a1dcfcba11289ce36efb6cf0f9bdc23.svg\">. \u0417\u0435\u043b\u0435\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u0430\u043a \u043e\u0442 \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439, \u0442\u0430\u043a \u0438 \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN, \u0438 \u0432\u0441\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0440\u043e\u0437\u043e\u0432\u043e\u043c \u0431\u043b\u043e\u043a\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bf1\/c96\/a01\/bf1c96a01f192c7b03516afd32f95998.png\" width=\"512\" height=\"312\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"python\"> class Attention(nn.Module):     def __init__(self, enc_hid_dim, dec_hid_dim):         super().__init__()                  self.attn = nn.Linear((enc_hid_dim * 2) + dec_hid_dim, dec_hid_dim)         self.v = nn.Linear(dec_hid_dim, 1, bias = False)              def forward(self, hidden, encoder_outputs):                  #hidden = [batch size, dec hid dim]         #encoder_outputs = [src len, batch size, enc hid dim * 2]                  batch_size = encoder_outputs.shape[1]         src_len = encoder_outputs.shape[0]                  #repeat decoder hidden state src_len times         hidden = hidden.unsqueeze(1).repeat(1, src_len, 1)                  encoder_outputs = encoder_outputs.permute(1, 0, 2)                  #hidden = [batch size, src len, dec hid dim]         #encoder_outputs = [batch size, src len, enc hid dim * 2]                  energy = torch.tanh(self.attn(torch.cat((hidden, encoder_outputs), dim = 2)))                   #energy = [batch size, src len, dec hid dim]          attention = self.v(energy).squeeze(2)                  #attention= [batch size, src len]                  return F.softmax(attention, dim=1) <\/code><\/pre>\n<h4>\u0414\u0435\u043a\u043e\u0434\u0435\u0440<\/h4>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0434\u0435\u043a\u043e\u0434\u0435\u0440.<\/p>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u043b\u043e\u0439 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, <code>attention<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1}\" alt=\"s_{t-1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/380\/0a3\/84a\/3800a384ab7e6a9734d5bc8cbdbcd193.svg\">, \u0432\u0441\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c75\/29d\/bb9\/c7529dbb95f6a66f3f8cc36042f9e78f.svg\">, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1c1\/15c\/fb1\/1c115cfb1257955854f7ed08ce507bce.svg\">.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"w_t\" alt=\"w_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a79\/c07\/43b\/a79c0743be3d3a09199acca7b4e425e6.svg\">, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>weighted<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bc9\/e6c\/9b8\/bc9e6c9b8a0f6afe5e9830d1cc40793d.svg\">, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 \u0432\u0435\u0441\u0430\u043c\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/51d\/3e0\/d1d\/51d3e0d1d0ad8f67e64b62d4144c657a.svg\">.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"w_t = a_t H\" alt=\"w_t = a_t H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e4e\/9dd\/8fb\/e4e9dd8fb3af94c6c529c2de110286d5.svg\" width=\"81\" height=\"20\"><\/p>\n<p>\u0412\u0445\u043e\u0434\u043d\u043e\u0435 \u0441\u043b\u043e\u0432\u043e, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"d(y_t)\" alt=\"d(y_t)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/115\/dae\/01a\/115dae01a3ed8b6834641005d3bb4259.svg\">, \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 <img decoding=\"async\" class=\"formula inline\" source=\"w_t\" alt=\"w_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9e2\/bdc\/4bd\/9e2bdc4bd7d03a8b9d19cb44e5a8fe4e.svg\">, \u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1}\" alt=\"s_{t-1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0a0\/f7d\/e90\/0a0f7de90dd0c103f79a35c72ebfa039.svg\">, \u0432\u0441\u0435 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 RNN, \u0441 <img decoding=\"async\" class=\"formula inline\" source=\"d(y_t)\" alt=\"d(y_t)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3af\/077\/039\/3af0770392a2c0cb66149023209ce687.svg\">\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"w_t\" alt=\"w_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7ca\/2ee\/b27\/7ca2eeb27e142b37444187c7e866eacd.svg\">\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"s_t = \\text{DecoderGRU}(d(y_t), w_t, s_{t-1})\" alt=\"s_t = \\text{DecoderGRU}(d(y_t), w_t, s_{t-1})\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/800\/123\/feb\/800123feb083fe957a71d5d1293de583.svg\" width=\"283\" height=\"22\"><\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c <img decoding=\"async\" class=\"formula inline\" source=\"d(y_t)\" alt=\"d(y_t)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ee5\/bb8\/c26\/ee5bb8c26fe6b21e472b8e4f5dc73146.svg\">, <img decoding=\"async\" class=\"formula inline\" source=\"w_t\" alt=\"w_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8bc\/3e2\/a5d\/8bc3e2a5d068999f06a421cb6cb066fa.svg\"> \u0438 <img decoding=\"async\" class=\"formula inline\" source=\"s_t\" alt=\"s_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/125\/8d4\/1e9\/1258d41e9c7b2949cbd80bfaecbe08ce.svg\">\u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 <img decoding=\"async\" class=\"formula inline\" source=\"f\" alt=\"f\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dd0\/ad6\/aad\/dd0ad6aad745f78156b2531121c2e7d2.svg\">\u0434\u043b\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"\\hat{y}_{t+1}\" alt=\"\\hat{y}_{t+1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/46a\/933\/2c4\/46a9332c4b6084943f73b1508c72b041.svg\">. \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u0438\u0445 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"\\hat{y}_{t+1} = f(d(y_t), w_t, s_t)\" alt=\"\\hat{y}_{t+1} = f(d(y_t), w_t, s_t)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a26\/d32\/528\/a26d32528e7e555ff24e013f7cba56c0.svg\" width=\"182\" height=\"23\"><\/p>\n<p>\u041d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e34\/da6\/b31\/e34da6b31765b7c156b81de3006cc723.png\" width=\"537\" height=\"421\"><figcaption><\/figcaption><\/figure>\n<p>\u0417\u0435\u043b\u0451\u043d\u044b\u0435\/\u0431\u0438\u0440\u044e\u0437\u043e\u0432\u044b\u0439 \u0431\u043b\u043e\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 RNNs \u043a\u043e\u0434\u0435\u0440\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0434\u0430\u044e\u0442 H, \u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0431\u043b\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, <img decoding=\"async\" class=\"formula inline\" source=\"z = h_T = \\tanh(g(h^\\rightarrow_T,h^\\leftarrow_T)) = \\tanh(g(z^\\rightarrow, z^\\leftarrow)) = s_0\" alt=\"z = h_T = \\tanh(g(h^\\rightarrow_T,h^\\leftarrow_T)) = \\tanh(g(z^\\rightarrow, z^\\leftarrow)) = s_0\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dbc\/387\/658\/dbc387658083f0984081c96492f7befd.svg\">, \u0441\u0438\u043d\u0438\u0439 \u0431\u043b\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 s_t, \u0444\u0438\u043e\u043b\u0435\u0442\u043e\u0432\u044b\u0439 \u0431\u043b\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 <img decoding=\"async\" class=\"formula inline\" source=\"f\" alt=\"f\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b83\/8f5\/de7\/b838f5de73640b30c5a970354cdb00b3.svg\">, \u0432\u044b\u0432\u043e\u0434\u0438\u0442 <img decoding=\"async\" class=\"formula inline\" source=\"\\hat{y}_{t+1}\" alt=\"\\hat{y}_{t+1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/524\/d37\/6e8\/524d376e85278a48e00ab2287a8e3180.svg\">, \u0430 \u043e\u0440\u0430\u043d\u0436\u0435\u0432\u044b\u0439 \u0431\u043b\u043e\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u043f\u043e <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c1a\/7ea\/a86\/c1a7eaa86dac4f782ea5b9bad542dd42.svg\"> \u043e\u0442 <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ccc\/2ba\/a6c\/ccc2baa6c4b0469f245caf2ed295b627.svg\">\u0438 \u0432\u044b\u0445\u043e\u0434\u043e\u0432 <img decoding=\"async\" class=\"formula inline\" source=\"w_t\" alt=\"w_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b5d\/55c\/b04\/b5d55cb04401ebdaa5f5bc1e8a25aaf4.svg\">. \u041d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0440\u0430\u0441\u0447\u0435\u0442 <img decoding=\"async\" class=\"formula inline\" source=\"a_t\" alt=\"a_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/43b\/7fc\/be1\/43b7fcbe1302efeacbe36b2512e16eb4.svg\">.<\/p>\n<pre><code class=\"python\">class Decoder(nn.Module):     def __init__(self, output_dim, emb_dim, enc_hid_dim, dec_hid_dim, dropout, attention):         super().__init__()          self.output_dim = output_dim         self.attention = attention                  self.embedding = nn.Embedding(output_dim, emb_dim)                  self.rnn = nn.GRU((enc_hid_dim * 2) + emb_dim, dec_hid_dim)                  self.fc_out = nn.Linear((enc_hid_dim * 2) + dec_hid_dim + emb_dim, output_dim)                  self.dropout = nn.Dropout(dropout)              def forward(self, input, hidden, encoder_outputs):                       #input = [batch size]         #hidden = [batch size, dec hid dim]         #encoder_outputs = [src len, batch size, enc hid dim * 2]                  input = input.unsqueeze(0)                  #input = [1, batch size]                  embedded = self.dropout(self.embedding(input))                  #embedded = [1, batch size, emb dim]                  a = self.attention(hidden, encoder_outputs)                          #a = [batch size, src len]                  a = a.unsqueeze(1)                  #a = [batch size, 1, src len]                  encoder_outputs = encoder_outputs.permute(1, 0, 2)                  #encoder_outputs = [batch size, src len, enc hid dim * 2]                  weighted = torch.bmm(a, encoder_outputs)                  #weighted = [batch size, 1, enc hid dim * 2]                  weighted = weighted.permute(1, 0, 2)                  #weighted = [1, batch size, enc hid dim * 2]                  rnn_input = torch.cat((embedded, weighted), dim = 2)                  #rnn_input = [1, batch size, (enc hid dim * 2) + emb dim]                      output, hidden = self.rnn(rnn_input, hidden.unsqueeze(0))                  #output = [seq len, batch size, dec hid dim * n directions]         #hidden = [n layers * n directions, batch size, dec hid dim]                  #seq len, n layers and n directions will always be 1 in this decoder, therefore:         #output = [1, batch size, dec hid dim]         #hidden = [1, batch size, dec hid dim]         #this also means that output == hidden         assert (output == hidden).all()                  embedded = embedded.squeeze(0)         output = output.squeeze(0)         weighted = weighted.squeeze(0)                  prediction = self.fc_out(torch.cat((output, weighted, embedded), dim = 1))                  #prediction = [batch size, output dim]                  return prediction, hidden.squeeze(0)<\/code><\/pre>\n<h4>Seq2Seq<\/h4>\n<p>\u042d\u0442\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b RNN \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u0438 RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u0438\u043c\u0435\u043b\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b, \u043e\u0434\u043d\u0430\u043a\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u043c. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u0432\u0441\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 <code>enc_dim * 2<\/code> \u043d\u0430 <code>enc_dim * 2 if encoder_is_bidirectional else enc_dim<\/code>.<\/p>\n<p>\u042d\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u044c seq2seq \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0434\u0435\u0440 \u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043a\u0430\u043a \u0438 \u0432 \u0434\u0432\u0443\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>encoder<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u043a \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043a\u0430\u043a \u043e\u0442 \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e, \u0442\u0430\u043a \u0438 \u043e\u0442 \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e RNN \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u043f\u0435\u0440\u0451\u0434- \u0438 \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 RNNN, \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0434\u0440\u0443\u0433 \u043d\u0430 \u0434\u0440\u0443\u0433\u0430. \u041d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b <code>hidden<\/code> \u0438 <code>encoder_outputs<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440.<\/p>\n<p>\u041a\u0440\u0430\u0442\u043a\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u044d\u0442\u0430\u043f\u0430\u043c:<\/p>\n<ul>\n<li>\n<p>\u0442\u0435\u043d\u0437\u043e\u0440 <code>outputs<\/code> \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u043e\u0432 <img decoding=\"async\" class=\"formula inline\" source=\"\\hat{Y}\" alt=\"\\hat{Y}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/478\/250\/260\/47825026015f826bd9570600b2d70bfb.svg\"><\/p>\n<\/li>\n<li>\n<p>\u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <img decoding=\"async\" class=\"formula inline\" source=\"X\" alt=\"X\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/275\/9d7\/5fd\/2759d75fd70daf6b0347af2d95c8fb7b.svg\">, \u043f\u043e\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <img decoding=\"async\" class=\"formula inline\" source=\"z\" alt=\"z\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f57\/c62\/b21\/f57c62b21810cbf4d276032cf162e80f.svg\">\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/180\/35a\/d53\/18035ad530b1c45612b82192e373982b.svg\"><\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a \u0432\u0435\u043a\u0442\u043e\u0440 <code>context<\/code> <img decoding=\"async\" class=\"formula inline\" source=\"s_0 = z = h_T\" alt=\"s_0 = z = h_T\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c39\/1f9\/52d\/c391f952dc6f2a02ae3bfb7afbd1df7d.svg\"><\/p>\n<\/li>\n<li>\n<p>\u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0431\u0430\u0442\u0447 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 <code>&lt;sos&gt;<\/code> \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0439 <code>input<\/code> <img decoding=\"async\" class=\"formula inline\" source=\"y_1\" alt=\"y_1\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cbf\/caa\/cea\/cbfcaaceaee60fac2df731773414c40b.svg\"><\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u0442\u0435\u043c \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0432 \u0446\u0438\u043a\u043b\u0435:<\/p>\n<ul>\n<li>\n<p>\u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"y_t\" alt=\"y_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/997\/3df\/fca\/9973dffca0355ccb6ee58f31fcd2b97c.svg\">, \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <img decoding=\"async\" class=\"formula inline\" source=\"s_{t-1}\" alt=\"s_{t-1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/412\/a7c\/d86\/412a7cd86c26b9642c01d00b9e6bcd84.svg\">, \u0438 \u0432\u0441\u0435 \u0432\u044b\u0445\u043e\u0434\u044b \u043a\u043e\u0434\u0435\u0440\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"H\" alt=\"H\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dd9\/3a3\/43d\/dd93a343d6e68f17184ae848efa37713.svg\">\u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0430 <img decoding=\"async\" class=\"formula inline\" source=\"\\hat{y}_{t+1}\" alt=\"\\hat{y}_{t+1}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/991\/483\/d64\/991483d64e4c85afa6c3cea47d9e76e5.svg\">\u0438 \u043d\u043e\u0432\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <img decoding=\"async\" class=\"formula inline\" source=\"s_t\" alt=\"s_t\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/180\/13c\/cfc\/18013ccfce0c73b880976c1892f39d03.svg\"><\/p>\n<\/li>\n<li>\n<p>\u0437\u0430\u0442\u0435\u043c \u043c\u044b \u0440\u0435\u0448\u0430\u0435\u043c, \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043b\u0438 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043d\u0443\u0436\u0434\u0435\u043d\u0438\u0435\u043c \u0438\u043b\u0438 \u043d\u0435\u0442, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0432\u043e\u0434 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">class Seq2Seq(nn.Module):     def __init__(self, encoder, decoder, device):         super().__init__()                  self.encoder = encoder         self.decoder = decoder         self.device = device              def forward(self, src, trg, teacher_forcing_ratio = 0.5):                  #src = [src len, batch size]         #trg = [trg len, batch size]         #teacher_forcing_ratio is probability to use teacher forcing         #e.g. if teacher_forcing_ratio is 0.75 we use teacher forcing 75% of the time                  batch_size = src.shape[1]         trg_len = trg.shape[0]         trg_vocab_size = self.decoder.output_dim                  #tensor to store decoder outputs         outputs = torch.zeros(trg_len, batch_size, trg_vocab_size).to(self.device)                  #encoder_outputs is all hidden states of the input sequence, back and forwards         #hidden is the final forward and backward hidden states, passed through a linear layer         encoder_outputs, hidden = self.encoder(src)                          #first input to the decoder is the &lt;sos&gt; tokens         input = trg[0,:]                  for t in range(1, trg_len):                          #insert input token embedding, previous hidden state and all encoder hidden states             #receive output tensor (predictions) and new hidden state             output, hidden = self.decoder(input, hidden, encoder_outputs)                          #place predictions in a tensor holding predictions for each token             outputs[t] = output                          #decide if we are going to use teacher forcing or not             teacher_force = random.random() &lt; teacher_forcing_ratio                          #get the highest predicted token from our predictions             top1 = output.argmax(1)                           #if teacher forcing, use actual next token as next input             #if not, use predicted token             input = trg[t] if teacher_force else top1          return outputs<\/code><\/pre>\n<h3>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 Seq2Seq<\/h3>\n<p>\u041e\u0441\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u0443\u0440\u043e\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439.<\/p>\n<p>\u041c\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043a\u043e\u0434\u0435\u0440\u0430, \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u0438 \u043c\u043e\u0434\u0435\u043b\u0438 seq2seq  \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0432 \u0435\u0433\u043e \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440, \u0435\u0441\u043b\u0438 \u043e\u043d \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c.<\/p>\n<pre><code class=\"python\">INPUT_DIM = len(SRC.vocab) OUTPUT_DIM = len(TRG.vocab) ENC_EMB_DIM = 256 DEC_EMB_DIM = 256 ENC_HID_DIM = 512 DEC_HID_DIM = 512 ENC_DROPOUT = 0.5 DEC_DROPOUT = 0.5  attn = Attention(ENC_HID_DIM, DEC_HID_DIM) enc = Encoder(INPUT_DIM, ENC_EMB_DIM, ENC_HID_DIM, DEC_HID_DIM, ENC_DROPOUT) dec = Decoder(OUTPUT_DIM, DEC_EMB_DIM, ENC_HID_DIM, DEC_HID_DIM, DEC_DROPOUT, attn)  model = Seq2Seq(enc, dec, device).to(device)<\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0441\u0445\u0435\u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0435\u0441\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0432 \u0441\u0442\u0430\u0442\u044c\u0435. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u044b\u043c\u0438 \u043d\u0443\u043b\u044e \u0438 \u0432\u0441\u0435 \u0432\u0435\u0441\u0430 \u0438\u0437 <img decoding=\"async\" class=\"formula inline\" source=\"\\mathcal{N}(0, 0.01)\" alt=\"\\mathcal{N}(0, 0.01)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5d5\/b24\/e2d\/5d5b24e2d48335f85c49281201cea29d.svg\">.<\/p>\n<pre><code class=\"python\">def init_weights(m):     for name, param in m.named_parameters():         if 'weight' in name:             nn.init.normal_(param.data, mean=0, std=0.01)         else:             nn.init.constant_(param.data, 0)              model.apply(init_weights)<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u043a\u0443 \u043f\u043e\u0447\u0442\u0438 50% \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<pre><code class=\"python\">def count_parameters(model):     return sum(p.numel() for p in model.parameters() if p.requires_grad)  print(f'The model has {count_parameters(model):,} trainable parameters')<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440.<\/p>\n<pre><code class=\"python\">optimizer = optim.Adam(model.parameters())<\/code><\/pre>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0442\u0435\u0440\u044c.<\/p>\n<pre><code class=\"python\">TRG_PAD_IDX = TRG.vocab.stoi[TRG.pad_token]  criterion = nn.CrossEntropyLoss(ignore_index = TRG_PAD_IDX)<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f &#8230;<\/p>\n<pre><code class=\"python\">def train(model, iterator, optimizer, criterion, clip):          model.train()          epoch_loss = 0          for i, batch in enumerate(iterator):                  src = batch.src         trg = batch.trg                  optimizer.zero_grad()                  output = model(src, trg)                  #trg = [trg len, batch size]         #output = [trg len, batch size, output dim]                  output_dim = output.shape[-1]                  output = output[1:].view(-1, output_dim)         trg = trg[1:].view(-1)                  #trg = [(trg len - 1) * batch size]         #output = [(trg len - 1) * batch size, output dim]                  loss = criterion(output, trg)                  loss.backward()                  torch.nn.utils.clip_grad_norm_(model.parameters(), clip)                  optimizer.step()                  epoch_loss += loss.item()              return epoch_loss \/ len(iterator)<\/code><\/pre>\n<p>&#8230;\u0438 \u0446\u0438\u043a\u043b \u043e\u0446\u0435\u043d\u043a\u0438, \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 <code>eval<\/code> \u0440\u0435\u0436\u0438\u043c \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0432 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441 \u043f\u0440\u0438\u043d\u0443\u0436\u0434\u0435\u043d\u0438\u0435\u043c.<\/p>\n<pre><code class=\"python\">def evaluate(model, iterator, criterion):          model.eval()          epoch_loss = 0          with torch.no_grad():              for i, batch in enumerate(iterator):              src = batch.src             trg = batch.trg              output = model(src, trg, 0) #turn off teacher forcing              #trg = [trg len, batch size]             #output = [trg len, batch size, output dim]              output_dim = output.shape[-1]                          output = output[1:].view(-1, output_dim)             trg = trg[1:].view(-1)              #trg = [(trg len - 1) * batch size]             #output = [(trg len - 1) * batch size, output dim]              loss = criterion(output, trg)              epoch_loss += loss.item()              return epoch_loss \/ len(iterator)<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<pre><code class=\"python\">def epoch_time(start_time, end_time):     elapsed_time = end_time - start_time     elapsed_mins = int(elapsed_time \/ 60)     elapsed_secs = int(elapsed_time - (elapsed_mins * 60))     return elapsed_mins, elapsed_secs<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0430\u044e\u0442 \u043d\u0430\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u043f\u043e\u0442\u0435\u0440\u0438 \u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435.<\/p>\n<pre><code class=\"python\">N_EPOCHS = 10 CLIP = 1  best_valid_loss = float('inf')  for epoch in range(N_EPOCHS):          start_time = time.time()          train_loss = train(model, train_iterator, optimizer, criterion, CLIP)     valid_loss = evaluate(model, valid_iterator, criterion)          end_time = time.time()          epoch_mins, epoch_secs = epoch_time(start_time, end_time)          if valid_loss &lt; best_valid_loss:         best_valid_loss = valid_loss         torch.save(model.state_dict(), 'tut3-model.pt')          print(f'Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s')     print(f'\\tTrain Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f}')     print(f'\\t Val. Loss: {valid_loss:.3f} |  Val. PPL: {math.exp(valid_loss):7.3f}')<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043c\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u0438 \u00ab\u043b\u0443\u0447\u0448\u0438\u0435\u00bb \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<pre><code class=\"python\">model.load_state_dict(torch.load('tut3-model.pt'))  test_loss = evaluate(model, test_iterator, criterion)  print(f'| Test Loss: {test_loss:.3f} | Test PPL: {math.exp(test_loss):7.3f} |')<\/code><\/pre>\n<p>\u041c\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u043d\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0437\u0430 \u0441\u0447\u0435\u0442 \u0443\u0434\u0432\u043e\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0443 \u0436\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443, \u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0435\u043c\u043e\u0432 \u043a\u043e \u0432\u0441\u0435\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c RNN &#8212; \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u041c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043d\u0430 \u043a\u0430\u043a\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u0432\u043e \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 RNN \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/567794\/\"> https:\/\/habr.com\/ru\/post\/567794\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u043f\u043e\u0441\u0442\u0435 \u043e \u043c\u043e\u0434\u0435\u043b\u044f\u0445 sequence-to-sequence \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c PyTorch \u0438 torchText \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0437 \u0441\u0442\u0430\u0442\u044c <a href=\"https:\/\/arxiv.org\/abs\/1409.0473\" rel=\"noopener noreferrer nofollow\">Neural Machine Translation by Jointly Learning to Align and Translate<\/a>. \u042d\u0442\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043b\u0443\u0447\u0448\u0443\u044e \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0438\u0437 \u0438\u0437 \u0442\u0440\u0451\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 &nbsp;27\u043f\u043e\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e\u0441&nbsp;34\u0443\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439\u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<p>\u041a\u0430\u043a \u0438 \u0440\u0430\u043d\u0435\u0435, \u0435\u0441\u043b\u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043f\u043e\u0441\u0442\u0430 \u0432\u0430\u0441 \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442, \u0442\u043e \u043d\u0438\u0436\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u0438 \u0440\u0443\u0441\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e jupyter notebook:<\/p>\n<p><a href=\"https:\/\/github.com\/bentrevett\/pytorch-seq2seq\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <\/a>(<a href=\"https:\/\/colab.research.google.com\/github\/bentrevett\/pytorch-seq2seq\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">Open jupyter notebook In Colab<\/a>)<\/p>\n<p><a href=\"https:\/\/github.com\/vasiliyeskin\/bentrevett-pytorch-seq2seq_ru\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">\u0420\u0443\u0441\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f<\/a> (<a href=\"https:\/\/colab.research.google.com\/github\/vasiliyeskin\/bentrevett-pytorch-seq2seq_ru\/blob\/master\/3%20-%20Neural%20Machine%20Translation%20by%20Jointly%20Learning%20to%20Align%20and%20Translate.ipynb\" rel=\"noopener noreferrer nofollow\">Open jupyter notebook In Colab<\/a>)<\/p>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043e\u0431\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0434\u0435\u0440\u0430-\u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0448\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0431\u044b\u043b\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u00ab\u0441\u0436\u0430\u0442\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438\u00bb \u043f\u0443\u0442\u0435\u043c \u044f\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430  \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043d\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439  \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0448\u0430\u0433\u0435, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430,  \u0438 \u0441\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c .<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u0438 \u0441\u0436\u0430\u0442\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043d\u0430\u0448 \u0432\u0435\u043a\u0442\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041c\u043e\u0434\u0435\u043b\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0432 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435, \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0436\u0430\u0442\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0443 \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437\u0435\u0433\u043e\u0441\u043a\u0440\u044b\u0442\u044b\u0435\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f! \u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c? \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f <em>\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e<\/em>.<\/p>\n<p>\u0414\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f . \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1, \u0430 \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0440\u0430\u0432\u043d\u0430 1. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f , \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 .<\/p>\n<p>\u041c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0448\u0430\u0433\u0435 \u043f\u0440\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0435\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041c\u044b \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043c, \u043a\u0430\u043a \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u0430\u043b\u0435\u0435.<\/p>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>\u0421\u043d\u043e\u0432\u0430 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0439.<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438.<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F  from torchtext.legacy.datasets import Multi30k from torchtext.legacy.data import Field, BucketIterator  import spacy import numpy as np  import random import math import time<\/code><\/pre>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<pre><code class=\"python\">SEED = 1234  random.seed(SEED) np.random.seed(SEED) torch.manual_seed(SEED) torch.cuda.manual_seed(SEED) torch.backends.cudnn.deterministic = True\t<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043d\u0435\u043c\u0435\u0446\u043a\u0443\u044e \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u0438 spaCy.<\/p>\n<pre><code class=\"python\">python -m spacy download en_core_web_sm python -m spacy download de_core_news_sm<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 Google Colab \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b (\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 colab runtime! \u041d\u0430\u0438\u0431\u044b\u0441\u0442\u0440\u0435\u0439\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u043a\u043e\u043c\u0430\u0434\u0443\uff1a <strong>Ctrl + M + .<\/strong>):<\/p>\n<pre><code class=\"python\">!pip install -U spacy==3.0 !python -m spacy download en_core_web_sm !python -m spacy download de_core_news_sm<\/code><\/pre>\n<pre><code class=\"python\">spacy_de = spacy.load('de_core_news_sm') spacy_en = spacy.load('en_core_web_sm')<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u044b.<\/p>\n<pre><code class=\"python\">def tokenize_de(text):     \"\"\"     Tokenizes German text from a string into a list of strings     \"\"\"     return [tok.text for tok in spacy_de.tokenizer(text)]  def tokenize_en(text):     \"\"\"     Tokenizes English text from a string into a list of strings     \"\"\"     return [tok.text for tok in spacy_en.tokenizer(text)]<\/code><\/pre>\n<p>\u041f\u043e\u043b\u044f \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0442\u0435\u043c\u0438 \u0436\u0435, \u0447\u0442\u043e \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<pre><code class=\"python\">SRC = Field(tokenize = tokenize_de,              init_token = '&lt;sos&gt;',              eos_token = '&lt;eos&gt;',              lower = True)  TRG = Field(tokenize = tokenize_en,              init_token = '&lt;sos&gt;',              eos_token = '&lt;eos&gt;',              lower = True)<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<pre><code class=\"python\">train_data, valid_data, test_data = Multi30k.splits(exts = ('.de', '.en'),                                                      fields = (SRC, TRG))<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0438.<\/p>\n<pre><code class=\"python\">SRC.build_vocab(train_data, min_freq = 2) TRG.build_vocab(train_data, min_freq = 2)<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e.<\/p>\n<pre><code class=\"python\">device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b.<\/p>\n<pre><code class=\"python\">BATCH_SIZE = 128  train_iterator, valid_iterator, test_iterator = BucketIterator.splits(     (train_data, valid_data, test_data),      batch_size = BATCH_SIZE,     device = device)<\/code><\/pre>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 Seq2Seq<\/h3>\n<h4>\u041a\u043e\u0434\u0435\u0440<\/h4>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u0434\u0435\u0440. \u041a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0441\u043b\u043e\u0439 GRU, \u043e\u0434\u043d\u0430\u043a\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0434 <em>\u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN<\/em>. \u0412 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 RNN \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435. <em>\u0412\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f RNN<\/em> \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430, \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e\u043d\u0438\u0436\u0435\u0437\u0435\u043b\u0435\u043d\u044b\u043c\u0446\u0432\u0435\u0442\u043e\u043c, \u0430 <em>\u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f RNN<\/em> \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433, \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0431\u0438\u0440\u044e\u0437\u043e\u0432\u044b\u0439. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043a\u043e\u0434\u0435 \u2014 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <code>bidirectional = True<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430 \u0432 RNN, \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:<\/p>\n<p>\u0413\u0434\u0435  \u0438 .<\/p>\n<p>\u041a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 RNN \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0432\u043e\u0434 (<code>embedded<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 PyTorch \u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u044f\u043c\u043e\u0435, \u0442\u0430\u043a \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f ( and , respectively) \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u043c \u0441 \u043d\u0443\u043b\u0435\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430: \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u044f\u043c\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u043d\u0430 \u0443\u0432\u0438\u0434\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043b\u043e\u0432\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 , \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0438\u0437 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043e\u043d\u0430 \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u0442 \u043f\u0435\u0440\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 .<\/p>\n<p>RNN \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>outputs<\/code> \u0438 <code>hidden<\/code>.<\/p>\n<p><code>outputs<\/code> \u0438\u043c\u0435\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 <strong>srclen,batchsize,hiddim\u2217numdirections<\/strong> \u0433\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 <code>hid_dim<\/code> \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0438 &#8212; \u044d\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 <code>hid_dim<\/code> \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u2014 \u044d\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0443\u043c\u0430\u0442\u044c \u043e \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0438 \u043a\u0430\u043a \u043e \u043f\u0440\u044f\u043c\u043e\u043c \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u0445, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c, \u0442.\u0435. ,  \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u043f\u0440\u044f\u043c\u043e\u0435\u0438\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435\u0441\u0446\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u0432\u043c\u0435\u0441\u0442\u0435 \u043a\u0430\u043a  \u0442\u0435\u043d\u0437\u043e\u0440\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<\/p>\n<p><code>hidden<\/code> \u0438\u043c\u0435\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 <strong>nlayers\u2217numdirections,batchsize,hiddim<\/strong>, where <strong>\u22122,:,:<\/strong> \u0434\u0430\u0435\u0442 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u043f\u0435\u0440\u0451\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0442.\u0435.\u043f\u043e\u0441\u043b\u0435\u0442\u043e\u0433\u043e,\u043a\u0430\u043a\u043e\u043d\u0443\u0432\u0438\u0434\u0435\u043b\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435\u0441\u043b\u043e\u0432\u043e\u0432\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 <strong>\u22121,:,:<\/strong> \u0434\u0430\u0435\u0442 \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u0443\u0440\u043e\u0432\u043d\u044e \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0442\u043d\u043e-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 RNN \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0442.\u0435.\u043f\u043e\u0441\u043b\u0435\u0442\u043e\u0433\u043e,\u043a\u0430\u043a\u043e\u043d\u0443\u0432\u0438\u0434\u0435\u043b\u043f\u0435\u0440\u0432\u043e\u0435\u0441\u043b\u043e\u0432\u043e\u0432\u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u043c, \u0435\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0432\u0435\u043a\u0442\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430  \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f , \u043d\u043e \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 (  \u0438 , respectively). \u041c\u044b \u0440\u0435\u0448\u0430\u0435\u043c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0432 \u0434\u0432\u0430 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u0435, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0432 \u0438\u0445 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439  \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 .<\/p>\n<p><strong>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435<\/strong>: \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043a\u043b\u043e\u043d\u0435\u043d\u0438\u0435 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0430\u0432\u0442\u043e\u0440\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u043e\u0435 \u043d\u0430\u0437\u0430\u0434-\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 RNN \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430. \u042d\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u043e\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0430 \u0432\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <code>outputs<\/code>, \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0451\u043d\u043d\u044b\u0445 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432\u043f\u0435\u0440\u0435\u0434 \u0438 \u043d\u0430\u0437\u0430\u0434 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c <code>hidden<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0435.<\/p>\n<pre><code class=\"python\">class Encoder(nn.Module):     def __init__(self, input_dim, emb_dim, enc_hid_dim, dec_hid_dim, dropout):         super().__init__()                  self.embedding = nn.Embedding(input_dim, emb_dim)                  self.rnn = nn.GRU(emb_dim, enc_hid_dim, bidirectional = True)                  self.fc = nn.Linear(enc_hid_dim * 2, dec_hid_dim)                  self.dropout = nn.Dropout(dropout)              def forward(self, src):                  #src = [src len, batch size]                  embedded = self.dropout(self.embedding(src))                  #embedded = [src len, batch size, emb dim]                  outputs, hidden = self.rnn(embedded)                          #outputs = [src len, batch size, hid dim * num directions]         #hidden = [n layers * num directions, batch size, hid dim]                  #hidden is stacked [forward_1, backward_1, forward_2, backward_2, ...]         #outputs are always from the last layer                  #hidden [-2, :, : ] is the last of the forwards RNN          #hidden [-1, :, : ] is the last of the backwards RNN                  #initial decoder hidden is final hidden state of the forwards and backwards          #  encoder RNNs fed through a linear layer         hidden = torch.tanh(self.fc(torch.cat((hidden[-2,:,:], hidden[-1,:,:]), dim = 1)))                  #outputs = [src len, batch size, enc hid dim * 2]         #hidden = [batch size, dec hid dim]                  return outputs, hidden<\/code><\/pre>\n<h4>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/h4>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0441\u043b\u043e\u0439 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f. \u042d\u0442\u043e\u0442 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430  \u0438 \u0432\u0441\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0434\u0435\u0440\u0430, \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0435\u043d\u0437\u043e\u0440 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 . \u0421\u043b\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f  \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1, \u0430 \u0432\u0441\u044f \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u0430 \u0440\u0430\u0432\u043d\u0430 1.<\/p>\n<p>\u0418\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0441\u043b\u043e\u0439 \u0431\u0435\u0440\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0443\u0436\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438, , \u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u044b \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0432 , \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430 , \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442, \u043a\u0430\u043a\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u043c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0434\u0435\u043b\u044f\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043b\u043e\u0432\u0430 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c, .<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c <em>\u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f<\/em> \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u043c\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u043c\u0438 \u043a\u043e\u0434\u0435\u0440\u0430. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0435\u0440\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432, \u0438 \u043d\u0430\u0448\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0439 \u0442\u0435\u043d\u0437\u043e\u0440, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c, \u044d\u0442\u043e <code>\u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c<\/code> \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430  \u0440\u0430\u0437. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u044d\u043d\u0435\u0440\u0433\u0438\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f  \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0432 \u0438\u0445 \u0432\u043c\u0435\u0441\u0442\u0435 \u0438 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0432 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 (<code>attn<\/code>) \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 .<\/p>\n<p>\u042d\u0442\u0443 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0434\u0435\u0440\u0430 \u00ab\u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442\u00bb \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430.<\/p>\n<p>\u0412 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <strong>dec hid dim, src len<\/strong> \u0442\u0435\u043d\u0437\u043e\u0440 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u0431\u0430\u0442\u0447\u0435. \u041c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0431\u044b\u043b \u0434\u043b\u0438\u043d\u044b <strong>src len<\/strong> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432 \u0431\u0430\u0442\u0447\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f <code>\u044d\u043d\u0435\u0440\u0433\u0438\u0438<\/code> \u043d\u0430 <strong>1, dec hid dim<\/strong>-\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0439 \u0442\u0435\u0437\u043e\u0440 .<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0443\u043c\u0430\u0442\u044c \u043e \u043a\u0430\u043a \u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435<\/p>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326464","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326464","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=326464"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326464\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}