{"id":378457,"date":"2024-06-09T15:19:12","date_gmt":"2024-06-09T15:19:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=378457"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-09T15:19:12","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=378457","title":{"rendered":"<span>\u041a\u0440\u0430\u0442\u043a\u043e \u043f\u0440\u043e Seq2Seq-\u043c\u043e\u0434\u0435\u043b\u0438<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p><strong>Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438<\/strong> \u2014 \u044d\u0442\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b ML, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0442\u0438\u043f\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430, \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u043a \u043f\u0438\u043a\u0447\u0430\u043c \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u0433\u0434\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0432 \u043e\u0431\u0449\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/h2>\n<p>Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u0435\u0439: <strong>\u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430<\/strong> \u0438 <strong>\u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430<\/strong>.   <\/p>\n<h4>\u042d\u043d\u043a\u043e\u0434\u0435\u0440 <\/h4>\n<p>\u042d\u043d\u043a\u043e\u0434\u0435\u0440 <strong>\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440,<\/strong> \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0439 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u042d\u0442\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043e \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0435 \u0447\u0443\u0442\u044c \u043d\u0438\u0436\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043f\u043e\u0434\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u044d\u043d\u043a\u043e\u0434\u0435\u0440, \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430. \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u043e\u044f <code>Embedding<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0432\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0432\u043e <em>milk<\/em> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a \u0432\u0435\u043a\u0442\u043e\u0440 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 300.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u0443 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 RNN, \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c LSTM \u0438\u043b\u0438 GRU. \u042d\u0442\u0438 \u0441\u0435\u0442\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0432\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 RNN \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0448\u0430\u0433\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0445\u043e\u0434 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0448\u0430\u0433 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 RNN \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c. \u042d\u0442\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u043e\u0431\u043e\u0431\u0449\u0430\u0435\u0442 \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u2014 \u044d\u0442\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 <em>\u0441\u0436\u0430\u0442\u0430\u044f<\/em> \u0432\u0435\u0440\u0441\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0430\u044f \u0432 \u0441\u0435\u0431\u044f \u0435\u0451 \u0441\u043c\u044b\u0441\u043b.<\/p>\n<p>\u0414\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 RNN. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0432\u0430 RNN \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e: \u043e\u0434\u0438\u043d \u2014 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e. \u0418\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435, \u0442\u0430\u043a \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 Keras \u0441 LSTM:<\/p>\n<pre><code class=\"python\">from keras.layers import Input, LSTM, Embedding from keras.models import Model  # \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 embed_size = 256 latent_dim = 512  # \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 encoder_inputs = Input(shape=(None,)) # \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439 encoder_embedding = Embedding(input_dim=vocab_size, output_dim=embed_size)(encoder_inputs) # LSTM \u0441\u043b\u043e\u0439 encoder_lstm = LSTM(latent_dim, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding) # \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f encoder_states = [state_h, state_c]<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0438\u0445 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u0417\u0430\u0442\u0435\u043c \u044d\u0442\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u043f\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u0432 LSTM, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0434\u0430\u0451\u0442 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430.<\/p>\n<h4>\u0414\u0435\u043a\u043e\u0434\u0435\u0440<\/h4>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430, <strong>\u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430<\/strong>, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u043e\u043c.<\/p>\n<p>\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0443, \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u043e \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445.<\/p>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RNN, \u043a\u0430\u043a LSTM \u0438\u043b\u0438 GRU. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442:<\/p>\n<ol>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d (\u0438\u043b\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0448\u0430\u0433\u0430).<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0448\u0430\u0433\u0430 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430. \u042d\u0442\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 Softmax, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440. \u042d\u0442\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 Softmax, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/p>\n<p>\u0414\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0443 \u0444\u043e\u043a\u0443\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0432\u0435\u0441\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044f \u0432\u0430\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 Keras \u0441 LSTM \u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">from keras.layers import Input, LSTM, Dense, Embedding from keras.models import Model  # \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 embed_size = 256 latent_dim = 512  # \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=vocab_size, output_dim=embed_size)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states) # \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f attention = Dense(1, activation='tanh')(decoder_outputs) attention = Flatten()(attention) attention = Activation('softmax')(attention) attention = RepeatVector(latent_dim)(attention) attention = Permute([2, 1])(attention) decoder_combined_context = Concatenate(axis=-1)([decoder_outputs, attention])  # \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 decoder_dense = Dense(vocab_size, activation='softmax') decoder_outputs = decoder_dense(decoder_combined_context)  # \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 decoder_model = Model([decoder_inputs] + encoder_states, decoder_outputs)<\/code><\/pre>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0440\u0435\u0445 Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0435\u0439   <\/h2>\n<h4>\u041c\u0430\u0448\u0438\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434<\/h4>\n<p><strong>\u041c\u0430\u0448\u0438\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434<\/strong> \u2014 \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Seq2Seq \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u0441 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0433\u043e \u043d\u0430 \u0444\u0440\u0430\u043d\u0446\u0443\u0437\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Keras.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u044b \u0444\u0440\u0430\u043d\u0446\u0443\u0437\u0441\u043a\u0438\u0445 \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439 (\u043e\u043d\u0438 \u0435\u0441\u0442\u044c \u043d\u0430 kaggle):<\/p>\n<pre><code class=\"python\">import numpy as np import pandas as pd from keras.models import Model from keras.layers import Input, LSTM, Dense, Embedding from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences  # \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 data_path = 'fra.txt' lines = open(data_path, encoding='utf-8').read().strip().split('\\n') eng_texts = [] fra_texts = [] for line in lines[: min(10000, len(lines) - 1)]:     eng_text, fra_text = line.split('\\t')     eng_texts.append(eng_text)     fra_texts.append(fra_text)  # \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f eng_tokenizer = Tokenizer() eng_tokenizer.fit_on_texts(eng_texts) eng_sequences = eng_tokenizer.texts_to_sequences(eng_texts) eng_word_index = eng_tokenizer.word_index  fra_tokenizer = Tokenizer() fra_tokenizer.fit_on_texts(fra_texts) fra_sequences = fra_tokenizer.texts_to_sequences(fra_texts) fra_word_index = fra_tokenizer.word_index  # \u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 max_len_eng = max([len(txt) for txt in eng_sequences]) max_len_fra = max([len(txt) for txt in fra_sequences])  encoder_input_data = pad_sequences(eng_sequences, maxlen=max_len_eng, padding='post') decoder_input_data = pad_sequences(fra_sequences, maxlen=max_len_fra, padding='post')  # \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0442\u043e\u043a\u0435\u043d \u0432\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f decoder_target_data decoder_target_data = np.zeros((len(fra_texts), max_len_fra, len(fra_word_index) + 1), dtype='float32') for i, seqs in enumerate(fra_sequences):     for t, token in enumerate(seqs):         if t > 0:             decoder_target_data[i, t - 1, token] = 1.0  # \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b latent_dim = 256 num_encoder_tokens = len(eng_word_index) + 1 num_decoder_tokens = len(fra_word_index) + 1  # \u044d\u043d\u043a\u043e\u0434\u0435\u0440 encoder_inputs = Input(shape=(None,)) encoder_embedding = Embedding(input_dim=num_encoder_tokens, output_dim=latent_dim)(encoder_inputs) encoder_lstm, state_h, state_c = LSTM(latent_dim, return_state=True)(encoder_embedding) encoder_states = [state_h, state_c]  # \u0434\u0435\u043a\u043e\u0434\u0435\u0440 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=num_decoder_tokens, output_dim=latent_dim)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states) decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs)  # \u043c\u043e\u0434\u0435\u043b\u044c Seq2Seq model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy') model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=64, epochs=30, validation_split=0.2)<\/code><\/pre>\n<h4>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 &#8212; <strong>\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong>. \u042d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Seq2Seq \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<a href=\"https:\/\/github.com\/sunnysai12345\/News_Summary\/blob\/master\/news_summary.csv\"> \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u043d\u043e\u0432\u043e\u0441\u0442\u0435\u0439,<\/a> \u0433\u0434\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u044b\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np import pandas as pd from keras.models import Model from keras.layers import Input, LSTM, Dense, Embedding, Concatenate, Attention from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences  # \u0427\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 data_path = 'news_summary.csv' df = pd.read_csv(data_path) articles = df['text'].values summaries = df['summary'].values  # \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f article_tokenizer = Tokenizer() article_tokenizer.fit_on_texts(articles) article_sequences = article_tokenizer.texts_to_sequences(articles) article_word_index = article_tokenizer.word_index  summary_tokenizer = Tokenizer() summary_tokenizer.fit_on_texts(summaries) summary_sequences = summary_tokenizer.texts_to_sequences(summaries) summary_word_index = summary_tokenizer.word_index  # \u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 max_len_article = 300 max_len_summary = 50  encoder_input_data = pad_sequences(article_sequences, maxlen=max_len_article, padding='post') decoder_input_data = pad_sequences(summary_sequences, maxlen=max_len_summary, padding='post')  # \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0442\u043e\u043a\u0435\u043d \u0432\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f decoder_target_data decoder_target_data = np.zeros((len(summaries), max_len_summary, len(summary_word_index) + 1), dtype='float32') for i, seqs in enumerate(summary_sequences):     for t, token in enumerate(seqs):         if t > 0:             decoder_target_data[i, t - 1, token] = 1.0  # \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b latent_dim = 256 num_encoder_tokens = len(article_word_index) + 1 num_decoder_tokens = len(summary_word_index) + 1  # \u044d\u043d\u043a\u043e\u0434\u0435\u0440 encoder_inputs = Input(shape=(None,)) encoder_embedding = Embedding(input_dim=num_encoder_tokens, output_dim=latent_dim)(encoder_inputs) encoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding) encoder_states = [state_h, state_c]  # \u0434\u0435\u043a\u043e\u0434\u0435\u0440 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=num_decoder_tokens, output_dim=latent_dim)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)  # \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f attention = Attention()([decoder_outputs, encoder_outputs]) decoder_combined_context = Concatenate(axis=-1)([decoder_outputs, attention])  # \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_combined_context)  # \u043c\u043e\u0434\u0435\u043b\u044c Seq2Seq model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy') model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=64, epochs=30, validation_split=0.2)<\/code><\/pre>\n<h4>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c<\/h4>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c <strong>\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c<\/strong> \u2014 \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430, \u0433\u0434\u0435 Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430, \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c <a href=\"https:\/\/keras.io\/api\/applications\/inceptionv3\/\">InceptionV3 <\/a>\u0434\u043b\u044f \u044d\u043a\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 Seq2Seq \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<pre><code class=\"python\">import numpy as np import pandas as pd from keras.models import Model from keras.layers import Input, LSTM, Dense, Embedding, Concatenate from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.applications.inception_v3 import InceptionV3 from keras.applications.inception_v3 import preprocess_input from keras.preprocessing import image from keras.models import Model  # \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 InceptionV3 base_model = InceptionV3(weights='imagenet') model = Model(base_model.input, base_model.layers[-2].output)  def extract_features(img_path, model):     img = image.load_img(img_path, target_size=(299, 299))     x = image.img_to_array(img)     x = np.expand_dims(x, axis=0)     x = preprocess_input(x)     features = model.predict(x)     return features  # \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 captions_data_path = 'captions.txt' images_path = 'images\/'  captions = open(captions_data_path, 'r').read().strip().split('\\n') image_ids = [] captions_list = [] for line in captions:     img_id, caption = line.split('\\t')     image_ids.append(img_id)     captions_list.append(caption)  # \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f caption_tokenizer = Tokenizer() caption_tokenizer.fit_on_texts(captions_list) caption_sequences = caption_tokenizer.texts_to_sequences(captions_list) caption_word_index = caption_tokenizer.word_index  # \u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 max_len_caption = max([len(txt) for txt in caption_sequences])  decoder_input_data = pad_sequences(caption_sequences, maxlen=max_len_caption, padding='post')  # \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0442\u043e\u043a\u0435\u043d \u0432\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f decoder_target_data decoder_target_data = np.zeros((len(captions_list), max_len_caption, len(caption_word_index) + 1), dtype='float32') for i, seqs in enumerate(caption_sequences):     for t, token in enumerate(seqs):         if t > 0:             decoder_target_data[i, t - 1, token] = 1.0  # \u044d\u043a\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 image_features = np.zeros((len(image_ids), 2048)) for i, img_id in enumerate(image_ids):     img_path = images_path + img_id     image_features[i] = extract_features(img_path, model)  # \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b latent_dim = 256 num_decoder_tokens = len(caption_word_index) + 1  # \u0432\u0445\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f image_input = Input(shape=(2048,))  # \u0432\u0445\u043e\u0434 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=num_decoder_tokens, output_dim=latent_dim)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=[image_input, image_input]) decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs)  # \u043c\u043e\u0434\u0435\u043b\u044c Seq2Seq model = Model([image_input, decoder_inputs], decoder_outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy') model.fit([image_features, decoder_input_data], decoder_target_data, batch_size=64, epochs=30, validation_split=0.2)<\/code><\/pre>\n<hr\/>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438 &#8212; \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c).<\/p>\n<p>\u0412 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0435 \u0432\u0435\u0431\u0438\u043d\u0430\u0440\u044b \u043a\u0443\u0440\u0441\u0430 ML Advanced:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/3mJ1E\/\"><strong>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430 TimesNet \u0438 TimeGPT<\/strong>  <\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/oUfy\/\"><strong>H2O, TPOT, Autokeras &#8212; \u0430 \u0432\u044b \u0447\u0442\u043e, \u0437\u0430 \u043c\u0435\u043d\u044f \u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0431\u0443\u0434\u0435\u0442\u0435?<\/strong>  <\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/816341\/\"> https:\/\/habr.com\/ru\/articles\/816341\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p><strong>Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438<\/strong> \u2014 \u044d\u0442\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b ML, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0442\u0438\u043f\u043e \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430, \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u043a \u043f\u0438\u043a\u0447\u0430\u043c \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u0433\u0434\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0432 \u043e\u0431\u0449\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/h2>\n<p>Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u0435\u0439: <strong>\u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430<\/strong> \u0438 <strong>\u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430<\/strong>.   <\/p>\n<h4>\u042d\u043d\u043a\u043e\u0434\u0435\u0440 <\/h4>\n<p>\u042d\u043d\u043a\u043e\u0434\u0435\u0440 <strong>\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440,<\/strong> \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0439 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u042d\u0442\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043e \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0435 \u0447\u0443\u0442\u044c \u043d\u0438\u0436\u0435.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043f\u043e\u0434\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u044d\u043d\u043a\u043e\u0434\u0435\u0440, \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430. \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u043e\u044f <code>Embedding<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0432\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0432\u043e <em>milk<\/em> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a \u0432\u0435\u043a\u0442\u043e\u0440 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 300.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u0443 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 RNN, \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c LSTM \u0438\u043b\u0438 GRU. \u042d\u0442\u0438 \u0441\u0435\u0442\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0432\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 RNN \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0448\u0430\u0433\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0445\u043e\u0434 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0448\u0430\u0433 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 RNN \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043a\u0440\u044b\u0442\u044b\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c. \u042d\u0442\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440 \u043e\u0431\u043e\u0431\u0449\u0430\u0435\u0442 \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u2014 \u044d\u0442\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 <em>\u0441\u0436\u0430\u0442\u0430\u044f<\/em> \u0432\u0435\u0440\u0441\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0430\u044f \u0432 \u0441\u0435\u0431\u044f \u0435\u0451 \u0441\u043c\u044b\u0441\u043b.<\/p>\n<p>\u0414\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 RNN. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0432\u0430 RNN \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e: \u043e\u0434\u0438\u043d \u2014 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e. \u0418\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435, \u0442\u0430\u043a \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 Keras \u0441 LSTM:<\/p>\n<pre><code class=\"python\">from keras.layers import Input, LSTM, Embedding from keras.models import Model  # \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 embed_size = 256 latent_dim = 512  # \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 encoder_inputs = Input(shape=(None,)) # \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439 encoder_embedding = Embedding(input_dim=vocab_size, output_dim=embed_size)(encoder_inputs) # LSTM \u0441\u043b\u043e\u0439 encoder_lstm = LSTM(latent_dim, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding) # \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f encoder_states = [state_h, state_c]<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0438\u0445 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u0417\u0430\u0442\u0435\u043c \u044d\u0442\u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u044b \u043f\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u0432 LSTM, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0434\u0430\u0451\u0442 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043a\u0440\u044b\u0442\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430.<\/p>\n<h4>\u0414\u0435\u043a\u043e\u0434\u0435\u0440<\/h4>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430, <strong>\u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u0430<\/strong>, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u043e\u043c.<\/p>\n<p>\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0443, \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u0434\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u043e \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445.<\/p>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RNN, \u043a\u0430\u043a LSTM \u0438\u043b\u0438 GRU. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442:<\/p>\n<ol>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d (\u0438\u043b\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0448\u0430\u0433\u0430).<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0448\u0430\u0433\u0430 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430. \u042d\u0442\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 Softmax, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 RNN \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u043e\u0435 \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440. \u042d\u0442\u043e\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u043e\u0439 Softmax, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/p>\n<p>\u0414\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0443 \u0444\u043e\u043a\u0443\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0432\u0435\u0441\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044f \u0432\u0430\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043d\u0430 Keras \u0441 LSTM \u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">from keras.layers import Input, LSTM, Dense, Embedding from keras.models import Model  # \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 embed_size = 256 latent_dim = 512  # \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=vocab_size, output_dim=embed_size)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states) # \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f attention = Dense(1, activation='tanh')(decoder_outputs) attention = Flatten()(attention) attention = Activation('softmax')(attention) attention = RepeatVector(latent_dim)(attention) attention = Permute([2, 1])(attention) decoder_combined_context = Concatenate(axis=-1)([decoder_outputs, attention])  # \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 decoder_dense = Dense(vocab_size, activation='softmax') decoder_outputs = decoder_dense(decoder_combined_context)  # \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 decoder_model = Model([decoder_inputs] + encoder_states, decoder_outputs)<\/code><\/pre>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0445\u043e\u0434\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0440\u0435\u0445 Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0435\u0439   <\/h2>\n<h4>\u041c\u0430\u0448\u0438\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434<\/h4>\n<p><strong>\u041c\u0430\u0448\u0438\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434<\/strong> \u2014 \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f Seq2Seq \u043c\u043e\u0434\u0435\u043b\u0435\u0439. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Seq2Seq \u043c\u043e\u0434\u0435\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u0441 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0433\u043e \u043d\u0430 \u0444\u0440\u0430\u043d\u0446\u0443\u0437\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Keras.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u044b \u0444\u0440\u0430\u043d\u0446\u0443\u0437\u0441\u043a\u0438\u0445 \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439 (\u043e\u043d\u0438 \u0435\u0441\u0442\u044c \u043d\u0430 kaggle):<\/p>\n<pre><code class=\"python\">import numpy as np import pandas as pd from keras.models import Model from keras.layers import Input, LSTM, Dense, Embedding from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences  # \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 data_path = 'fra.txt' lines = open(data_path, encoding='utf-8').read().strip().split('\\n') eng_texts = [] fra_texts = [] for line in lines[: min(10000, len(lines) - 1)]:     eng_text, fra_text = line.split('\\t')     eng_texts.append(eng_text)     fra_texts.append(fra_text)  # \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f eng_tokenizer = Tokenizer() eng_tokenizer.fit_on_texts(eng_texts) eng_sequences = eng_tokenizer.texts_to_sequences(eng_texts) eng_word_index = eng_tokenizer.word_index  fra_tokenizer = Tokenizer() fra_tokenizer.fit_on_texts(fra_texts) fra_sequences = fra_tokenizer.texts_to_sequences(fra_texts) fra_word_index = fra_tokenizer.word_index  # \u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 max_len_eng = max([len(txt) for txt in eng_sequences]) max_len_fra = max([len(txt) for txt in fra_sequences])  encoder_input_data = pad_sequences(eng_sequences, maxlen=max_len_eng, padding='post') decoder_input_data = pad_sequences(fra_sequences, maxlen=max_len_fra, padding='post')  # \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0442\u043e\u043a\u0435\u043d \u0432\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f decoder_target_data decoder_target_data = np.zeros((len(fra_texts), max_len_fra, len(fra_word_index) + 1), dtype='float32') for i, seqs in enumerate(fra_sequences):     for t, token in enumerate(seqs):         if t > 0:             decoder_target_data[i, t - 1, token] = 1.0  # \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b latent_dim = 256 num_encoder_tokens = len(eng_word_index) + 1 num_decoder_tokens = len(fra_word_index) + 1  # \u044d\u043d\u043a\u043e\u0434\u0435\u0440 encoder_inputs = Input(shape=(None,)) encoder_embedding = Embedding(input_dim=num_encoder_tokens, output_dim=latent_dim)(encoder_inputs) encoder_lstm, state_h, state_c = LSTM(latent_dim, return_state=True)(encoder_embedding) encoder_states = [state_h, state_c]  # \u0434\u0435\u043a\u043e\u0434\u0435\u0440 decoder_inputs = Input(shape=(None,)) decoder_embedding = Embedding(input_dim=num_decoder_tokens, output_dim=latent_dim)(decoder_inputs) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states) decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs)  # \u043c\u043e\u0434\u0435\u043b\u044c Seq2Seq model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer='rmsprop', loss='categorical_crossentropy') model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=64, epochs=30, validation_split=0.2)<\/code><\/pre>\n<h4>\u0422\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 &#8212; <strong>\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong>. \u042d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c Seq2Seq \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<a href=\"https:\/\/github.com\/sunnysai12345\/News_Summary\/blob\/master\/news_summary.csv\"> \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u043d\u043e\u0432\u043e\u0441\u0442\u0435\u0439,<\/a> \u0433\u0434\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u044b\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np import pandas as pd from keras.models import Model from keras.layers import Input, LSTM, Dense, Embedding, Concatenate, Attention from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences  # \u0427\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 data_path = 'news_summary.csv' df = pd.read_csv(data_path) articles = df['text'].values summaries = df['summary'].values  # \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f article_tokenizer = Tokenizer() article_tokenizer.fit_on_texts(articles) article_sequences = article_tokenizer.texts_to_sequences(articles) article_word_index = article_tokenizer.word_index  summary_tokenizer = Tokenizer() summary_tokenizer.fit_on_texts(summaries) summary_sequences = summary_tokenizer.texts_to_sequences(summaries) summary_word_index = summary_tokenizer.word_index  # \u043f\u0430\u0434\u0434\u0438\u043d\u0433 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 max_len_article = 300 max_len_summary = 50  encoder_input_data = pad_sequences(article_sequences, maxlen=max_len_article, padding='post') decoder_input_data = pad_sequences(summary_sequences, maxlen=max_len_summary, padding='post')  # \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0442\u043e\u043a\u0435\u043d \u0432\u043f\u0440\u0430\u0432\u043e \u0434\u043b\u044f decoder_target_data decoder_target_data = np.zeros((len(summaries), max_len_summary, len(summary_word_index) + 1), dtype='float32') for i, seqs in<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-378457","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378457","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=378457"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378457\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}