{"id":318496,"date":"2021-02-24T09:00:32","date_gmt":"2021-02-24T09:00:32","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318496"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318496","title":{"rendered":"\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u043d\u0430 \u0441\u0438\u043d\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c.<\/p>\n<pre><code class=\"python\">import random import math  import numpy as np import pandas as pd import matplotlib.pyplot as plt from tqdm.notebook import tqdm  import torch import torch.utils.data as tdutils from torch import nn, optim <\/code><\/pre>\n<h2>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 dataset&#8217;\u0430<\/h2>\n<p>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043b\u044e\u0431\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u043d\u043a\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u044d\u0442\u043e\u0433\u043e. \u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0441\u0438\u043d\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0438\u0437 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439. \u0415\u0441\u043b\u0438 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 &#171;2+2=4&#187; \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f, \u043d\u043e \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0446\u0438\u0438 \u043e\u0448\u0438\u0431\u043e\u043a. \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u0434\u0430\u0442\u044c \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0432 \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u043e\u043c \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 pytorch \u0432\u0438\u0434\u0435, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a IterableDataset \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>__iter__<\/code><\/p>\n<pre><code class=\"python\">OPS = '+-*\/%' DIGITS = '0123456789' CHARS = ' ' + DIGITS + OPS + '=' OPS_METHODS = {     '+': lambda v1, v2: v1 + v2,     '-': lambda v1, v2: v1 - v2,     '*': lambda v1, v2: v1 * v2,     '\/': lambda v1, v2: 0 if v2 == 0 else v1 \/\/ v2,     '%': lambda v1, v2: v1 % v2  }  class SampleSet(tdutils.IterableDataset):     def __init__(self, val_min=0, val_max=99):         self.val_min, self.val_max = val_min, val_max         assert val_min &gt; 0         max_res = val_max * val_max         self.str_size = len(f'{val_max}*{val_max}={max_res}')              def __iter__(self):         while True:             yield self.make_sample()                  def to_tensor(self, str_value):         res = torch.zeros([self.str_size], dtype=torch.uint8)         converted = torch.tensor([             CHARS.index(char) for char in str_value         ])         res[0:len(converted)] = converted         return res                           def make_sample(self):         val1 = random.randint(self.val_min, self.val_max)         val2 = random.randint(self.val_min, self.val_max)         op = OPS[random.randint(0, len(OPS) - 1)]         res = OPS_METHODS[op](val1, val2)          original = f'{val1}{op}{val2}={res}'         lst = list(original)         lst[random.randint(0, len(original) - 1)] = CHARS[random.randint(0, len(CHARS)-1)]         replaced = ''.join(lst)                                  return {             'task': self.to_tensor(replaced),             'answer': self.to_tensor(original)         }               # \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 _sample = SampleSet(1, 99).make_sample() _sample # output {   'task': tensor([ 4, 10, 11,  6,  6, 16,  2,  2,  5,  0], dtype=torch.uint8),   'answer': tensor([ 6, 10, 11,  6,  6, 16,  2,  2,  5,  0], dtype=torch.uint8) }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u0442\u044c \u0432 DataLoader<\/p>\n<pre><code class=\"python\">_dataset = tdutils.DataLoader(     dataset=SampleSet(1, 9),     batch_size=8 ) _dataset_iter = iter(_dataset) _batch = next(_dataset_iter) _batch  # output {'task': tensor([[ 7,  2,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16, 15,  0],          [ 7, 13,  7, 18,  4,  7],          [ 2, 15,  2, 16,  2,  0],          [ 2, 15,  9, 16,  2,  0],          [ 6, 13,  3,  5,  2,  1],          [10, 13,  3, 14,  2,  9]], dtype=torch.uint8),  'answer': tensor([[ 7, 14,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16,  1,  0],          [ 7, 13,  7, 16,  4,  7],          [ 2, 15,  4, 16,  2,  0],          [ 2, 15,  5, 16,  2,  0],          [ 6, 13,  3, 16,  2,  1],          [10, 13,  3, 16,  2,  9]], dtype=torch.uint8)}<\/code><\/pre>\n<p>\u0410 \u0435\u0449\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u043e\u043c \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/p>\n<pre><code class=\"python\">def tensor_to_str(tensor):         res = ''.join([         CHARS[val] for val in tensor     ])     return res.strip(' ')           def show_sample(dct):     task = tensor_to_str(dct['task'])     answer = tensor_to_str(dct['answer'])     return f'{task}-&gt;{answer}'  show_sample(_sample) # output '39+55=114-&gt;59+55=114'<\/code><\/pre>\n<h2>Embeddings<\/h2>\n<p>\u0412\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\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 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0432 pytorch \u043c\u043e\u0434\u0443\u043b\u0435\u043c nn.Embedding. \u041e\u043d \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u0438\u043a (\u0442\u0435\u043d\u0437\u043e\u0440 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>d_chars * d_models)<\/code><\/p>\n<pre><code class=\"python\">class Embed(nn.Module):     def __init__(self, d_chars, d_model):         super().__init__()                         self.embedding = nn.Embedding(d_chars, d_model)              def forward(self, batch):         return self.embedding(batch['task'].long())         _embed = Embed(len(CHARS), 32)(_batch) _embed.shape # output torch.Size([8, 6, 32])<\/code><\/pre>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 d_model \u044d\u0442\u043e \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u043d\u0435 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430. \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0442\u044f\u0436\u0435\u043d\u0438\u0438 \u0432\u0441\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0445 \u043f\u0440\u043e\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435. \u0415\u0433\u043e \u0432\u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u043e\u0433\u043e \u0436\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u0438\u043a\u0430, \u043d\u043e \u043f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043b\u043e\u0435\u043c<\/p>\n<pre><code class=\"python\">class DecodeEmbed(nn.Module):     def __init__(self, d_chars, d_model):         super().__init__()         self.decode = nn.Linear(d_model, d_chars)         self.softmax = nn.Softmax(dim=-1)              def forward(self, embed):                 return torch.argmax(self.softmax(self.decode(embed)), dim=-1)      DecodeEmbed(len(CHARS), 32)(_embed).shape # output torch.Size([8, 6])<\/code><\/pre>\n<h2>Attention<\/h2>\n<p>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u044d\u0442\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0442\u043a\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0430 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. E\u0441\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e, \u0442\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043b\u044e\u0447 K \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 V. \u0414\u0430\u043b\u044c\u0448\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c Q \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043a\u0430\u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u0432\u0437\u044f\u0442 DotProduct Attention. \u0412 \u043d\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0435\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0431\u0435\u0440\u0435\u0442\u0441\u044f softmax \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f Q \u0438 K<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"\\mathrm{softmax}(Q \\cdot K^T) \\cdot V\" alt=\"\\mathrm{softmax}(Q \\cdot K^T) \\cdot V\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fa0\/23b\/a08\/fa023ba088519611e35f2959227f05a0.svg\" width=\"151\" height=\"23\"><\/p>\n<p>\u041a\u0430\u043a \u043d\u0430 \u043f\u0430\u043b\u044c\u0446\u0430\u0445 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041f\u0443\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043b\u0435\u0436\u0438\u0442 \u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0432 \u0434\u0432\u0443\u0445\u043c\u0435\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435.<\/p>\n<pre><code class=\"python\">_k = torch.tensor([     [1, 1], [-1, 1], [0.01, 0.02] ]).float() _q = torch.tensor([     [-1, 1], [1, 1], [0, 1] ]).float() * 10  _v = torch.tensor([     [0, 1, 2, 3],     [4, 5, 6, 7],     [8, 9, 10, 11] ]).float()<\/code><\/pre>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0430 K \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043e\u0440\u0442\u043e\u0433\u043e\u043d\u0430\u043b\u044c\u043d\u044b. \u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u043b\u0435\u0436\u0438\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0448\u0443\u043c. \u0412\u0435\u043a\u0442\u043e\u0440 _q \u0438\u043c\u0435\u0435\u0442 \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443<\/p>\n<pre><code class=\"python\">torch.matmul(_q, _k.T) # output tensor([[ 0.0000, 20.0000,  0.1000],         [20.0000,  0.0000,  0.3000],         [10.0000, 10.0000,  0.2000]])<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439. \u0417\u0430\u043f\u0440\u043e\u0441 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 \u043f\u043e\u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c \u0438 \u0432\u0442\u043e\u0440\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c. \u0412 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0440\u0430\u0432\u043d\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e. \u0422\u0440\u0435\u0442\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446) \u043d\u0435 \u0432\u043d\u043e\u0441\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0432\u043a\u043b\u0430\u0434\u0430 \u0432 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041a\u0430\u0440\u0442\u0438\u043d\u0443 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u0433\u043b\u0430\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 softmax&#8217;\u0430<\/p>\n<pre><code class=\"python\">_atw = nn.Softmax(dim=-1)(torch.matmul(_q, _k.T)) (_atw * 100).numpy().astype(int) #output  array([[  0, 100,   0],        [100,   0,   0],        [ 49,  49,   0]])<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c<\/p>\n<pre><code>_att_res = torch.matmul(_atw, _v) _att_res.cpu().numpy().astype(int) # output array([[4, 5, 6, 7],        [0, 1, 2, 3],        [2, 3, 4, 5]])<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 \u0438 \u0432\u0442\u043e\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b, \u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 \u0438\u0445 \u0441\u0440\u0435\u0434\u043d\u0435\u0435<\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0432\u0441\u0435. \u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f multihead attention. \u041e\u043d\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 dot product \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041a\u043e\u043b-\u0432\u043e \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u044d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440. \u0412\u044b\u0432\u043e\u0434\u044b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0433\u043e\u043b\u043e\u0432 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0432\u044b\u0443\u0447\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e. \u0410 \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0442\u043a\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0449\u0435 \u043e\u0431\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u043a\u043e\u0440\u0435\u043d\u044c \u0438\u0437 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u043e\u0441\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0431\u044b\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043a\u0430\u043a \u0438 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435. <\/p>\n<pre><code class=\"python\">class Attention(nn.Module):     def forward(self, q, k, v):         sel = torch.matmul(q, k.transpose(-1, -2))         weights = nn.Softmax(dim=-1)(sel \/ math.sqrt(k.shape[-1]))         return torch.matmul(weights, v)                  class ProjectedAttention(nn.Module):     def __init__(self, d_qk1, d_qk2, d_v1, d_v2):         super().__init__()         self.keys = nn.Linear(d_qk1, d_qk2)         self.queries = nn.Linear(d_qk1, d_qk2)         self.values = nn.Linear(d_v1, d_v2)         self.att = Attention()              def forward(self, q, k, v):         return self.att(             self.queries(q),             self.keys(k),             self.values(v)         )                   class MultiHeadAttention(nn.Module):     def __init__(self, d_model, h):         super().__init__()         self.heads = nn.ModuleList([             ProjectedAttention(                 d_model, d_model \/\/ h,                  d_model, d_model \/\/ h             )              for _ in range(h)         ])                    self.final = nn.Linear(d_model, d_model)              def forward(self, q, k, v):         head_res = torch.cat([             head(q, k, v)             for head in self.heads         ], dim=-1)         return self.final(head_res)          _model = MultiHeadAttention(d_model=32, h=2) _multi = _model(_embed, _embed, _embed) _multi.shape # output torch.Size([8, 6, 32])<\/code><\/pre>\n<h2>Positional Encoding<\/h2>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u0442\u043e\u043a\u0435\u043d\u0430\u0445, \u043a\u0440\u043e\u043c\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u0430\u0431\u0434\u0438\u0442\u044c \u0435\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u044f\u0447\u0435\u0435\u043a, \u043a \u044d\u043c\u0431\u0435\u043d\u0434\u0438\u043d\u0433\u0430\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u041a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0432 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0439 \u043d\u043e\u0442\u0430\u0446\u0438\u0438: \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0447\u0435\u0442\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 v_len \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c\u044e \u0432 \u0434\u0432\u043e\u0435 \u043c\u0435\u043d\u044c\u0448\u0435. \u0414\u043b\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u0438 pos \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 l \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440 <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"e^{\\left( \\mathrm{pos} \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}\" alt=\"e^{\\left( \\mathrm{pos} \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/272\/f48\/64a\/272f4864a3074c2f77d9b825cfa60405.svg\" width=\"0\" height=\"0\"><\/p>\n<p>\u0433\u0434\u0435 M &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043a \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u0430\u043c \u0442\u0430\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 sin \u0438 cos. \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0441\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u0432 \u0442\u0443 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"e^{\\left( (\\mathrm{pos}+ k) \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}=e^{\\left( \\mathrm{pos} \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}\\cdot e^{\\left( k \\cdot M^{l} \\cdot \\sqrt{-1} \\right)} \" alt=\"e^{\\left( (\\mathrm{pos}+ k) \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}=e^{\\left( \\mathrm{pos} \\cdot M^{l} \\cdot \\sqrt{-1} \\right)}\\cdot e^{\\left( k \\cdot M^{l} \\cdot \\sqrt{-1} \\right)} \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b50\/f0c\/93d\/b50f0c93d52e4e804f85d7ebabf2274a.svg\" width=\"313\" height=\"22\"><\/p>\n<pre><code class=\"python\">M = 1\/10000  def pos_tensor(seq_len, v_len):     power = 2 * torch.arange(v_len \/\/ 2).float() \/ v_len     arg = torch.outer(         torch.arange(seq_len),         M ** power              )     res = torch.cat([torch.sin(arg), torch.cos(arg)], dim=-1)     return res      class PositionInfo(nn.Module):     def forward(self, data):         pos_info = pos_tensor(data.shape[-2], data.shape[-1]).to(data.device)         return data + pos_info      PositionInfo()(_embed).shape<\/code><\/pre>\n<h2>Encoders \u0438 Decoders<\/h2>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 Encoder \u0438 Decoder \u0431\u043b\u043e\u043a\u043e\u0432. Encoder \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e multihead self attention \u0441\u043b\u043e\u0439 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c feed-forward \u0441\u043b\u043e\u0435\u043c. Feed forward \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0445  \u0441\u043b\u043e\u0435\u0432 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0432\u0435\u0441\u0430\u043c\u0438 (\u0441\u0432\u0435\u0440\u0442\u043a\u0430 \u0441 \u044f\u0434\u0440\u043e\u043c 1). \u041e\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0430 \u044d\u0442\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u0430  skip connection&#8217;\u0430\u043c\u0438 \u0438 \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u0439 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439. <\/p>\n<pre><code>class BigBatch(nn.Module):     def __init__(self, net):         super().__init__()         self.net = net              def forward(self, batch):         first_size = batch.shape[0]         second_size = batch.shape[1]         big_batch = batch.reshape(             first_size * second_size, *tuple(batch.shape[2:])         )         big_res = self.net(big_batch)         return big_res.reshape(             first_size, second_size, *tuple(big_res.shape[1:])         )          class Encoder(nn.Module):     def __init__(self, d_model, h):         super().__init__()         self.self_att = MultiHeadAttention(d_model, h)         self.norm1 = BigBatch(nn.BatchNorm1d(d_model))         self.feed_forward = nn.Sequential(             BigBatch(nn.Linear(d_model, d_model)),             nn.ReLU(),             BigBatch(nn.Linear(d_model, d_model)),         )         self.norm2 = BigBatch(nn.BatchNorm1d(d_model))                        def forward(self, data):         res1 = self.self_att(data, data, data)         res1r = self.norm1(data + res1)         res2 = self.feed_forward(res1r)         res2r = self.norm2(res1r + res2)         return res2r          _encode = Encoder(32, 2)(_embed) _encode.shape  # output torch.Size([8, 6, 32])<\/code><\/pre>\n<p>Decoder \u0447\u0443\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u041a\u0440\u043e\u043c\u0435 self attention&#8217;\u0430 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u043b\u043e\u0439 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0432 \u0432\u044b\u0445\u043b\u043e\u043f\u0443 encoder&#8217;\u0430<\/p>\n<pre><code class=\"python\">class Decoder(nn.Module):     def __init__(self, d_model, h):         super().__init__()         self.self_att = MultiHeadAttention(d_model, h)         self.norm1 = BigBatch(nn.BatchNorm1d(d_model))                  self.src_att = MultiHeadAttention(d_model, h)         self.norm2 = BigBatch(nn.BatchNorm1d(d_model))                  self.feed_forward = nn.Sequential(             BigBatch(nn.Linear(d_model, d_model)),             nn.ReLU(),             BigBatch(nn.Linear(d_model, d_model)),         )         self.norm3 = BigBatch(nn.BatchNorm1d(d_model))                       def forward(self, src, tgt):         res1r = self.self_att(tgt, tgt, tgt)         res1 = self.norm1(res1r + tgt)         res2r = self.src_att(res1, res1, src)         res2 = self.norm2(res1 + res2r)         res3r = self.feed_forward(res2)         res3 = self.norm3(res2 + res3r)         return res3          _decode = Decoder(32, 2)(_embed, _embed) _decode.shape # output torch.Size([8, 6, 32])<\/code><\/pre>\n<h2>\u0412\u0441\u0435 \u0432 \u0441\u0431\u043e\u0440\u0435<\/h2>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 embedding&#8217;\u043e\u0432, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0435\u0432 Encoder&#8217;\u0430, Decoder&#8217;\u0430 \u0438 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f. <\/p>\n<pre><code class=\"python\">class Model(nn.Module):     def __init__(self, d_chars, d_model, h, n_layers=2):         super().__init__()         self.embed = Embed(d_chars, d_model)         self.pos = PositionInfo()         self.encoders = nn.ModuleList([             Encoder(d_model, h) for _ in range(n_layers)         ])         self.decoders = nn.ModuleList([             Decoder(d_model, h) for _ in range(n_layers)         ])         self.embed_decoder = nn.Linear(d_model, d_chars)                       def forward(self, batch):         enc_out = self.pos(self.embed(batch))         for layer in self.encoders:             enc_out = layer(enc_out)         dec_out = enc_out         for layer in self.decoders:             dec_out = layer(enc_out, dec_out)         char_out = self.embed_decoder(dec_out)         return char_out               _model_out = Model(len(CHARS), 32, 2, 2)(_batch) _model_out.shape # output torch.Size([8, 6, 19])<\/code><\/pre>\n<p>\u041c\u0430\u043b\u0435\u043d\u044c\u043a\u0430\u044f \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0430: \u0432\u0441\u0435, \u0434\u0430 \u043d\u0435 \u0432\u0441\u0435. \u0417\u0434\u0435\u0441\u044c \u043d\u0435\u0442 dropout&#8217;\u043e\u0432 \u0438 mask&#8217;ed attention&#8217;\u0430. \u0412 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0443 \u043f\u043e\u0437\u0432\u043e\u043b\u0435\u043d\u043e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u043e\u0439. \u0417\u0434\u0435\u0441\u044c \u0436\u0435, \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0437\u0430\u0447\u0435\u043c \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f. <\/p>\n<h2>\u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c: \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0442\u0435\u0440\u044c. \u0414\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c cross entropy loss \u0434\u043b\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. <\/p>\n<pre><code>def mean_batch_loss(answer, model_out):     return nn.CrossEntropyLoss()(         model_out.permute(0, 2, 1),         answer.long()     )      mean_batch_loss(_batch['answer'], _model_out) # output tensor(2.7526, grad_fn=&lt;NllLoss2DBackward&gt;)<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f, \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0433\u043d\u0430\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. <\/p>\n<pre><code class=\"python\">DEVICE = 'cuda:0'  class Context():     def __init__(self):         self.train_epoch = 10000         self.val_samples = 10000         self.device = DEVICE         self.batch_loss = 0         self.batch_discont = 0.8         self.history = []          _ctx = Context() _ctx.model = Model(len(CHARS), 32, 2, 2).to(_ctx.device) _ctx.opt = optim.SGD(_ctx.model.parameters(), lr=0.01) _ctx.epoch_size = 1000 _ctx.val_samples = 1000<\/code><\/pre>\n<p>\u0423\u0447\u0438\u0442\u0441\u044f \u0431\u0443\u0434\u0435\u043c \u043d\u0430 GPU \u0438 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u0442\u0443\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<pre><code class=\"python\">def to_device(device, data):     if isinstance(data, torch.Tensor):         return data.to(device)     elif isinstance(data, dict):         return {             key: to_device(device, value)             for key, value in data.items()         }      to_device(DEVICE, _batch) # output {'task': tensor([[ 7,  2,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16, 15,  0],          [ 7, 13,  7, 18,  4,  7],          [ 2, 15,  2, 16,  2,  0],          [ 2, 15,  9, 16,  2,  0],          [ 6, 13,  3,  5,  2,  1],          [10, 13,  3, 14,  2,  9]], device='cuda:0', dtype=torch.uint8),  'answer': tensor([[ 7, 14,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16,  1,  0],          [ 7, 13,  7, 16,  4,  7],          [ 2, 15,  4, 16,  2,  0],          [ 2, 15,  5, 16,  2,  0],          [ 6, 13,  3, 16,  2,  1],          [10, 13,  3, 16,  2,  9]], device='cuda:0', dtype=torch.uint8)}<\/code><\/pre>\n<p>\u041e\u0441\u043d\u043e\u0432\u0430 \u043e\u0441\u043d\u043e\u0432 &#8212; \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e batch&#8217;\u0430 \u0432 \u0441\u0435\u0442\u043a\u0443<\/p>\n<pre><code class=\"python\">def feed_batch(ctx, batch):     ctx.opt.zero_grad()     on_device = to_device(ctx.device, batch)     model_out = ctx.model(on_device)     loss = mean_batch_loss(on_device['answer'], model_out)     loss.backward()     ctx.opt.step()     ctx.batch_loss = (         (1 - ctx.batch_discont) * ctx.batch_loss         + ctx.batch_discont * loss.detach().cpu().numpy()     )   feed_batch(_ctx, _batch)<\/code><\/pre>\n<p>\u0412\u0441\u044e \u043d\u0443\u0434\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u043e \u0440\u0430\u0441\u0447\u0435\u0442\u0443 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043e\u0432 pytorch \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0435\u043d\u0437\u043e\u0440\u0430 (\u0435\u0441\u043b\u0438 \u044f\u0432\u043d\u043e \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435) \u043e\u043d \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043d\u0430\u043a\u043e\u043f\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0430 \u0430 \u0442\u0430\u043a\u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0434\u0435\u043b\u0430\u044e\u0449\u0438\u0435 back propagation. \u041d\u0430\u043c \u0436\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 (zero_grad), \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c loss.backward(), opt.step() \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<p>\u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u043e\u043b\u0433\u0430\u044f. \u041d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043b\u043e\u0441\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u043f\u043e\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u0442\u044c. \u0412\u0434\u0440\u0443\u0433 \u043e\u043d \u0443\u0448\u0435\u043b \u0432 \u0432\u0432\u0435\u0440\u0445 \u0438\u043b\u0438 \u0437\u0430\u0441\u0442\u043e\u043f\u043e\u0440\u0438\u043b\u0441\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u0438\u0439 batch_loss c \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c backoff&#8217;\u043e\u043c. <\/p>\n<p>\u041d\u043e \u0438\u043c \u043e\u0434\u043d\u0438\u043c \u0434\u0435\u043b\u043e \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f.  \u0425\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0447\u0435\u0441\u0442\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u0440\u0438\u043a\u0438. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0435\u0442\u0440\u0438\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c \u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0441\u0435\u043c\u043f\u043b\u0430\u043c. \u0424\u0443\u043d\u043a\u0446\u0438\u044f feed_epoch \u0441\u043b\u0443\u0436\u0438\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438. \u041e\u043d\u0430 \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u0435\u0442 \u0432 \u0441\u0435\u0442\u043a\u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u0441\u0435\u043c\u043f\u043b\u043e\u0432, \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0445 \u0434\u043b\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u0438. <\/p>\n<pre><code class=\"python\">def calc_metrics(ctx, dataset_iter):     ctx.model.eval()     counters = {         'batch': 0,         'batch_loss': 0.0,         'batch_char_acc': 0.0,         'sample_acc': 0.0     }     with torch.no_grad():         with tqdm(total=ctx.val_samples, leave=False) as pbar:             num_samples = 0             while num_samples &lt; ctx.val_samples:                 batch = next(dataset_iter)                 batch_size = len(batch['task'])                 on_device = to_device(ctx.device, batch)                                  pred = ctx.model(on_device)                 counters['batch'] += 1                 counters['batch_loss'] += mean_batch_loss(                     on_device['answer'],                      pred                 ).cpu().numpy()                 char_pred = torch.argmax(pred, dim=-1)                 correct = char_pred == on_device['answer']                                                   counters['batch_char_acc'] += correct.float().mean().cpu().numpy()                 counters['sample_acc'] += correct.min(dim=-1).values.float().mean().cpu().numpy()                                  num_samples += batch_size                 pbar.update(batch_size)                      return {         'loss': counters['batch_loss'] \/ counters['batch'],         'char_acc': counters['batch_char_acc'] \/ counters['batch'],          'sample_acc': counters['sample_acc'] \/ counters['batch']     }                      calc_metrics(_ctx, _dataset_iter)<\/code><\/pre>\n<pre><code class=\"python\">def feed_epoch(ctx, dataset_iter):     ctx.model.train()     num_samples = 0     with tqdm(total=ctx.train_epoch, leave=False) as pbar:         while num_samples &lt; ctx.train_epoch:             batch = next(dataset_iter)             feed_batch(ctx, batch)                      batch_size = len(batch['task'])             num_samples += batch_size             pbar.update(batch_size)                                        pbar.set_postfix(batch_loss = ctx.batch_loss)     ctx.history.append(calc_metrics(ctx, dataset_iter))                    _ctx.train_epoch = 10000             feed_epoch(_ctx, _dataset_iter)<\/code><\/pre>\n<h2>\u0427\u0438\u0441\u0442\u043e\u0432\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0432\u0437\u044f\u0442\u044c \u0438 \u043e\u0431\u0443\u0447\u0438\u0442\u044c. \u0420\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0442\u0447\u0430 \u0438 learning rate \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u043d\u044b \u0434\u043b\u044f \u0422\u0435\u0441\u043b\u0430 V100. \u0414\u043b\u044f \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438 \u043f\u043e-\u0441\u043a\u0440\u043e\u043c\u043d\u0435\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u043f\u043e-\u043c\u0435\u043d\u044c\u0448\u0435 (\u0447\u0442\u043e\u0431\u044b \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0432\u044b\u043b\u0435\u0442\u0435\u0442\u044c), \u0438 learning rate, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0442\u043e\u0436\u0435 (\u0447\u0442\u043e\u0431\u044b \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u043d\u0435\u0448\u0438\u0431\u043a\u043e \u0448\u0430\u0442\u0430\u043b\u043e). <\/p>\n<pre><code class=\"python\">dataset = tdutils.DataLoader(     dataset=SampleSet(1, 99999999),     batch_size=1024 * 2,     num_workers=8 ) dataset_iter = iter(dataset)<\/code><\/pre>\n<pre><code class=\"python\">ctx = Context() ctx.train_epoch = 10000 ctx.val_samples = 5000 ctx.model = Model(len(CHARS), 256, h=8, n_layers=3).to(ctx.device) ctx.opt = optim.SGD(ctx.model.parameters(), lr=1.0) ctx.seq_len = dataset.dataset.str_size ctx.history = []<\/code><\/pre>\n<pre><code>%%time _num_epoches = 100 for _ in tqdm(range(_num_epoches), leave=False):     feed_epoch(ctx, dataset_iter) # output CPU times: user 4min 34s, sys: 1min 47s, total: 6min 22s Wall time: 6min 20s<\/code><\/pre>\n<h2>\u0412\u044b\u0432\u043e\u0434 \u0438\u0441\u0442\u043e\u0440\u0438\u0438<\/h2>\n<p>\u0421\u0430\u043c\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0434\u043b\u044f baseline&#8217;\u0430 &#8212; \u0442\u0443\u043f\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. <\/p>\n<pre><code class=\"python\">def plot_history(ctx):     plt.subplot(1, 3, 1)     loss = [ record['loss'] for record in ctx.history]         plt.plot(loss)     plt.title('loss')          plt.subplot(1, 3, 2)     char_acc = [ record['char_acc'] for record in ctx.history]     plt.plot(char_acc)     baseline = (ctx.seq_len - 1) \/ ctx.seq_len + 1\/ctx.seq_len * 1\/len(CHARS)     plt.plot([0, len(char_acc) - 1], [baseline, baseline], c='g')     plt.plot([0, len(char_acc) - 1], [1, 1], c='g')      plt.title('char_acc')          plt.subplot(1, 3, 3)     baseline = (1\/len(CHARS))     sample_acc = [record['sample_acc'] for record in ctx.history]     plt.plot(sample_acc)     plt.plot([0, len(sample_acc) - 1], [baseline, baseline], c='g')     plt.title('sample_acc')      plt.figure(figsize=(15,5)) plot_history(ctx)<\/code><\/pre>\n<figure class=\"bordered full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/240\/1e3\/6f9\/2401e36f95b73c92c5118e45c808dc0b.png\" alt=\"\u0412\u044b\u0432\u043e\u0434 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f\" title=\"\u0412\u044b\u0432\u043e\u0434 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f\" width=\"881\" height=\"319\"><figcaption>\u0412\u044b\u0432\u043e\u0434 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f<\/figcaption><\/figure>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0443\u0447\u0438\u0442\u0441\u044f \ud83d\ude42 <\/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\/542334\/\"> https:\/\/habr.com\/ru\/post\/542334\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c.<\/p>\n<pre><code class=\"python\">import random import math  import numpy as np import pandas as pd import matplotlib.pyplot as plt from tqdm.notebook import tqdm  import torch import torch.utils.data as tdutils from torch import nn, optim <\/code><\/pre>\n<h2>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 dataset&#8217;\u0430<\/h2>\n<p>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043b\u044e\u0431\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u043d\u043a\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u044d\u0442\u043e\u0433\u043e. \u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0441\u0438\u043d\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0438\u0437 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439. \u0415\u0441\u043b\u0438 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 &#171;2+2=4&#187; \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f, \u043d\u043e \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0446\u0438\u0438 \u043e\u0448\u0438\u0431\u043e\u043a. \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u0434\u0430\u0442\u044c \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0432 \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u043e\u043c \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 pytorch \u0432\u0438\u0434\u0435, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a IterableDataset \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>__iter__<\/code><\/p>\n<pre><code class=\"python\">OPS = '+-*\/%' DIGITS = '0123456789' CHARS = ' ' + DIGITS + OPS + '=' OPS_METHODS = {     '+': lambda v1, v2: v1 + v2,     '-': lambda v1, v2: v1 - v2,     '*': lambda v1, v2: v1 * v2,     '\/': lambda v1, v2: 0 if v2 == 0 else v1 \/\/ v2,     '%': lambda v1, v2: v1 % v2  }  class SampleSet(tdutils.IterableDataset):     def __init__(self, val_min=0, val_max=99):         self.val_min, self.val_max = val_min, val_max         assert val_min &gt; 0         max_res = val_max * val_max         self.str_size = len(f'{val_max}*{val_max}={max_res}')              def __iter__(self):         while True:             yield self.make_sample()                  def to_tensor(self, str_value):         res = torch.zeros([self.str_size], dtype=torch.uint8)         converted = torch.tensor([             CHARS.index(char) for char in str_value         ])         res[0:len(converted)] = converted         return res                           def make_sample(self):         val1 = random.randint(self.val_min, self.val_max)         val2 = random.randint(self.val_min, self.val_max)         op = OPS[random.randint(0, len(OPS) - 1)]         res = OPS_METHODS[op](val1, val2)          original = f'{val1}{op}{val2}={res}'         lst = list(original)         lst[random.randint(0, len(original) - 1)] = CHARS[random.randint(0, len(CHARS)-1)]         replaced = ''.join(lst)                                  return {             'task': self.to_tensor(replaced),             'answer': self.to_tensor(original)         }               # \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 _sample = SampleSet(1, 99).make_sample() _sample # output {   'task': tensor([ 4, 10, 11,  6,  6, 16,  2,  2,  5,  0], dtype=torch.uint8),   'answer': tensor([ 6, 10, 11,  6,  6, 16,  2,  2,  5,  0], dtype=torch.uint8) }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u0430\u0442\u0430\u0441\u0435\u0442 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u0442\u044c \u0432 DataLoader<\/p>\n<pre><code class=\"python\">_dataset = tdutils.DataLoader(     dataset=SampleSet(1, 9),     batch_size=8 ) _dataset_iter = iter(_dataset) _batch = next(_dataset_iter) _batch  # output {'task': tensor([[ 7,  2,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16, 15,  0],          [ 7, 13,  7, 18,  4,  7],          [ 2, 15,  2, 16,  2,  0],          [ 2, 15,  9, 16,  2,  0],          [ 6, 13,  3,  5,  2,  1],          [10, 13,  3, 14,  2,  9]], dtype=torch.uint8),  'answer': tensor([[ 7, 14,  7, 16,  2,  0],          [ 8, 13,  2, 16,  8,  0],          [ 4, 12,  4, 16,  1,  0],          [ 7, 13,  7, 16,  4,  7],          [ 2, 15,  4, 16,  2,  0],          [ 2, 15,  5, 16,  2,  0],          [ 6, 13,  3, 16,  2,  1],          [10, 13,  3, 16,  2,  9]], dtype=torch.uint8)}<\/code><\/pre>\n<p>\u0410 \u0435\u0449\u0435 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u043e\u043c \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u044e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/p>\n<pre><code class=\"python\">def tensor_to_str(tensor):         res = ''.join([         CHARS[val] for val in tensor     ])     return res.strip(' ')           def show_sample(dct):     task = tensor_to_str(dct['task'])     answer = tensor_to_str(dct['answer'])     return f'{task}-&gt;{answer}'  show_sample(_sample) # output '39+55=114-&gt;59+55=114'<\/code><\/pre>\n<h2>Embeddings<\/h2>\n<p>\u0412\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\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 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0432 pytorch \u043c\u043e\u0434\u0443\u043b\u0435\u043c nn.Embedding. \u041e\u043d \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u0438\u043a (\u0442\u0435\u043d\u0437\u043e\u0440 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>d_chars * d_models)<\/code><\/p>\n<pre><code class=\"python\">class Embed(nn.Module):     def __init__(self, d_chars, d_model):         super().__init__()                         self.embedding = nn.Embedding(d_chars, d_model)              def forward(self, batch):         return self.embedding(batch['task'].long())         _embed = Embed(len(CHARS), 32)(_batch) _embed.shape # output torch.Size([8, 6, 32])<\/code><\/pre>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 d_model \u044d\u0442\u043e \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u043d\u0435 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430. \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0442\u044f\u0436\u0435\u043d\u0438\u0438 \u0432\u0441\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0445 \u043f\u0440\u043e\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435. \u0415\u0433\u043e \u0432\u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u043e\u0433\u043e \u0436\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u0438\u043a\u0430, \u043d\u043e \u043f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043b\u043e\u0435\u043c<\/p>\n<pre><code class=\"python\">class DecodeEmbed(nn.Module):     def __init__(self, d_chars, d_model):         super().__init__()         self.decode = nn.Linear(d_model, d_chars)         self.softmax = nn.Softmax(dim=-1)              def forward(self, embed):                 return torch.argmax(self.softmax(self.decode(embed)), dim=-1)      DecodeEmbed(len(CHARS), 32)(_embed).shape # output torch.Size([8, 6])<\/code><\/pre>\n<h2>Attention<\/h2>\n<p>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u044d\u0442\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0442\u043a\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u0430 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. E\u0441\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e, \u0442\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043b\u044e\u0447 K \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 V. \u0414\u0430\u043b\u044c\u0448\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c Q \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043a\u0430\u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c. \u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u0432\u0437\u044f\u0442 DotProduct Attention. \u0412 \u043d\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0435\u0441\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0431\u0435\u0440\u0435\u0442\u0441\u044f softmax \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f Q \u0438 K<\/p>\n<p>\u041a\u0430\u043a \u043d\u0430 \u043f\u0430\u043b\u044c\u0446\u0430\u0445 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041f\u0443\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043b\u0435\u0436\u0438\u0442 \u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0432 \u0434\u0432\u0443\u0445\u043c\u0435\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435.<\/p>\n<pre><code class=\"python\">_k = torch.tensor([     [1, 1], [-1, 1], [0.01, 0.02] ]).float() _q = torch.tensor([     [-1, 1], [1, 1], [0, 1] ]).float() * 10  _v = torch.tensor([     [0, 1, 2, 3],     [4, 5, 6, 7],     [8, 9, 10, 11] ]).float()<\/code><\/pre>\n<p>\u0412\u0435\u043a\u0442\u043e\u0440\u0430 K \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043e\u0440\u0442\u043e\u0433\u043e\u043d\u0430\u043b\u044c\u043d\u044b. \u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u043b\u0435\u0436\u0438\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0448\u0443\u043c. \u0412\u0435\u043a\u0442\u043e\u0440 _q \u0438\u043c\u0435\u0435\u0442 \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443<\/p>\n<pre><code class=\"python\">torch.matmul(_q, _k.T) # output tensor([[ 0.0000, 20.0000,  0.1000],         [20.0000,  0.0000,  0.3000],         [10.0000, 10.0000,  0.2000]])<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439. \u0417\u0430\u043f\u0440\u043e\u0441 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 \u043f\u043e\u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c \u0438 \u0432\u0442\u043e\u0440\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c. \u0412 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0440\u0430\u0432\u043d\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438 \u0432\u0442\u043e\u0440\u043e\u0433\u043e. \u0422\u0440\u0435\u0442\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446) \u043d\u0435 \u0432\u043d\u043e\u0441\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0432\u043a\u043b\u0430\u0434\u0430 \u0432 \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041a\u0430\u0440\u0442\u0438\u043d\u0443 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u0433\u043b\u0430\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 softmax&#8217;\u0430<\/p>\n<pre><code class=\"python\">_atw = nn.Softmax(dim=-1)(torch.matmul(_q, _k.T)) (_atw * 100).numpy().astype(int) #output  array([[  0, 100,   0],        [100,   0,   0],        [ 49,  49,   0]])<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c<\/p>\n<pre><code>_att_res = torch.matmul(_atw, _v) _att_res.cpu().numpy().astype(int) # output array([[4, 5, 6, 7],        [0, 1, 2, 3],        [2, 3, 4, 5]])<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 \u0438 \u0432\u0442\u043e\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b, \u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 \u0438\u0445 \u0441\u0440\u0435\u0434\u043d\u0435\u0435<\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0432\u0441\u0435. \u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f multihead attention. \u041e\u043d\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 dot product \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043f\u043e\u043d\u0438\u0436\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041a\u043e\u043b-\u0432\u043e \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u044d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440. \u0412\u044b\u0432\u043e\u0434\u044b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0433\u043e\u043b\u043e\u0432 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0432\u044b\u0443\u0447\u0438\u0432\u0430\u0435\u043c\u044b\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e. \u0410 \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0442\u043a\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0449\u0435 \u043e\u0431\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u043a\u043e\u0440\u0435\u043d\u044c \u0438\u0437 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u043e\u0441\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0431\u044b\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043a\u0430\u043a \u0438 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435. <\/p>\n<pre><code class=\"python\">class Attention(nn.Module):     def forward(self, q, k, v):         sel = torch.matmul(q, k.transpose(-1, -2))         weights = nn.Softmax(dim=-1)(sel \/ math.sqrt(k.shape[-1]))         return torch.matmul(weights, v)                  class ProjectedAttention(nn.Module):     def __init__(self, d_qk1, d_qk2, d_v1, d_v2):         super().__init__()         self.keys = nn.Linear(d_qk1, d_qk2)         self.queries = nn.Linear(d_qk1, d_qk2)         self.values = nn.Linear(d_v1, d_v2)         self.att = Attention()              def forward(self, q, k, v):         return self.att(             self.queries(q),             self.keys(k),             self.values(v)         )                   class MultiHeadAttention(nn.Module):     def __init__(self, d_model, h):         super().__init__()         self.heads = nn.ModuleList([             ProjectedAttention(                 d_model, d_model \/\/ h,                  d_model, d_model \/\/ h             )              for _ in range(h)         ])                    self.final = nn.Linear(d_model, d_model)              def forward(self, q, k, v):         head_res = torch.cat([             head(q, k, v)             for head in self.heads         ], dim=-1)         return self.final(head_res)          _model = MultiHeadAttention(d_model=32, h=2) _multi = _model(_embed, _embed, _embed) _multi.shape # output torch.Size([8, 6, 32])<\/code><\/pre>\n<h2>Positional Encoding<\/h2>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0432\u044f\u0437\u0438 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u0442\u043e\u043a\u0435\u043d\u0430\u0445, \u043a\u0440\u043e\u043c\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u0430\u0431\u0434\u0438\u0442\u044c \u0435\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u044f\u0447\u0435\u0435\u043a, \u043a \u044d\u043c\u0431\u0435\u043d\u0434\u0438\u043d\u0433\u0430\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u041a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0432 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0439 \u043d\u043e\u0442\u0430\u0446\u0438\u0438: \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0447\u0435\u0442\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 v_len \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441\u043d\u043e\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c\u044e \u0432 \u0434\u0432\u043e\u0435 \u043c\u0435\u043d\u044c\u0448\u0435. \u0414\u043b\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u0438 pos \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 l \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440 <\/p>\n<p>\u0433\u0434\u0435 M &#8212; \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043a \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u0430\u043c \u0442\u0430\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 sin \u0438 cos. \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0441\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u0432 \u0442\u0443 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c. <\/p>\n<pre><code class=\"python\">M = 1\/10000  def pos_tensor(seq_len, v_len):     power = 2 * torch.arange(v_len \/\/ 2).float() \/ v_len     arg = torch.outer(         torch.arange(seq_len),         M ** power              )     res = torch.cat([torch.sin(arg), torch.cos(arg)], dim=-1)     return res      class PositionInfo(nn.Module):     def forward(self, data):         pos_info = pos_tensor(data.shape[-2], data.shape[-1]).to(data.device)         return data + pos_info      PositionInfo()(_embed).shape<\/code><\/pre>\n<h2>Encoders \u0438 Decoders<\/h2>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 Encoder \u0438<\/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-318496","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318496","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=318496"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318496\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}