{"id":345503,"date":"2023-02-16T09:05:16","date_gmt":"2023-02-16T09:05:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345503"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345503","title":{"rendered":"<span>\u041f\u0438\u0448\u0435\u043c GPT \u0432 60 \u0441\u0442\u0440\u043e\u043a NumPy<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/w1\/br\/oi\/w1broiflq1cuye8nzwg4ptlaeoc.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/w1\/br\/oi\/w1broiflq1cuye8nzwg4ptlaeoc.jpeg\" data-blurred=\"true\"\/><\/div>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441 \u043d\u0443\u043b\u044f GPT \u0432\u0441\u0435\u0433\u043e \u0432 <a href=\"https:\/\/github.com\/jaymody\/picoGPT\/blob\/29e78cc52b58ed2c1c483ffea2eb46ff6bdec785\/gpt2_pico.py#L3-L58\" rel=\"nofollow noopener noreferrer\"><code>60 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 numpy<\/code><\/a>. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0432 \u043d\u0430\u0448\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 OpenAI \u0432\u0435\u0441\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 GPT-2 \u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u043a\u0441\u0442.<br \/>  <a name=\"habracut\"><\/a><br \/>  <strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435:<\/strong><\/p>\n<ul>\n<li>\u0414\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0432 Python, NumPy \u0438 \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u043f\u044b\u0442\u043e\u043c \u0432 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439.<\/li>\n<li>\u0412 \u044d\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438; \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0435\u0451, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0432 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c. \u041c\u043e\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 <strong>\u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432 GPT \u043a\u0430\u043a \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430<\/strong>.<\/li>\n<li>\u041f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b GPT \u2014 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f (\u0445\u043e\u0442\u044f \u0438 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u0430\u044f) \u0447\u0430\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u043d\u043e\u0439 \u0442\u0435\u043c\u044b \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 (LLM). [\u041c\u0430\u0441\u0448\u0442\u0430\u0431\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u0441\u0431\u043e\u0440 \u0442\u0435\u0440\u0430\u0431\u0430\u0439\u0442\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u0446\u0435\u043d\u043a\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043f\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0443 \u0437\u0430\u0434\u0430\u0447 \u2014 \u0434\u0435\u043b\u043e \u0432\u0441\u0435\u0439 \u0436\u0438\u0437\u043d\u0438 \u0434\u043b\u044f \u0441\u043e\u0442\u0435\u043d \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043e\u0432 \u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0440\u0430\u0431\u043e\u0442\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0430\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c LLM \u0432 \u0442\u043e, \u0447\u0435\u043c \u043e\u043d\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0435\u0433\u043e\u0434\u043d\u044f; \u043e\u0434\u043d\u043e\u0439 \u043b\u0438\u0448\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 GPT \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043f\u0435\u0440\u0432\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439, \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0449\u0435\u0439 \u0443\u0434\u043e\u0431\u043d\u044b\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 GPU \u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439. \u041d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u043c \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u0438\u043d\u0433\u0440\u0435\u0434\u0438\u0435\u043d\u0442\u043e\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043c\u043e\u0434\u0435\u043b\u0438 (<a href=\"http:\/\/www.incompleteideas.net\/IncIdeas\/BitterLesson.html\" rel=\"nofollow noopener noreferrer\">\u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e<\/a>), \u0430 GPT \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0434\u0435\u043b\u0430\u0442\u044c. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0438\u0433\u0440\u0430\u043b \u0432 <a href=\"https:\/\/hardwarelottery.github.io\" rel=\"nofollow noopener noreferrer\">\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u0443\u044e \u043b\u043e\u0442\u0435\u0440\u0435\u044e<\/a> \u0438 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u0434\u0440\u0443\u0433\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0441\u0431\u0440\u043e\u0441\u0438\u0442 \u0435\u0433\u043e \u0441 \u0442\u0440\u043e\u043d\u0430.]<\/li>\n<li>\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 \u0432\u044b\u043b\u043e\u0436\u0435\u043d \u0432 <a href=\"https:\/\/github.com\/jaymody\/picoGPT\" rel=\"nofollow noopener noreferrer\">github.com\/jaymody\/picoGPT<\/a>.<\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 GPT?<\/h2>\n<p>  GPT \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <strong>Generative Pre-trained Transformer<\/strong>. \u042d\u0442\u043e\u0442 \u0442\u0438\u043f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 <a href=\"https:\/\/arxiv.org\/pdf\/1706.03762.pdf\" rel=\"nofollow noopener noreferrer\"><strong>\u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0435<\/strong><\/a>. \u0421\u0442\u0430\u0442\u044c\u044f <a href=\"https:\/\/jalammar.github.io\/how-gpt3-works-visualizations-animations\/\" rel=\"nofollow noopener noreferrer\">How GPT3 Works<\/a> \u0414\u0436\u0435\u044f \u0410\u043b\u0430\u043c\u043c\u0430\u0440\u0430 \u2014 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 GPT, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u0442\u0430\u043a:<\/p>\n<ul>\n<li><strong>Generative<\/strong>: GPT <em>\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442<\/em> \u0442\u0435\u043a\u0441\u0442.<\/li>\n<li><strong>Pre-trained<\/strong>: GPT <em>\u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f<\/em> \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432 \u0438\u0437 \u043a\u043d\u0438\u0433, \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435<\/li>\n<li><strong>Transformer<\/strong>: GPT \u2014 \u044d\u0442\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f \u0432 \u0441\u0435\u0431\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 <em>\u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440<\/em>.<\/li>\n<\/ul>\n<p>  \u0411\u043e\u043b\u044c\u0448\u0438\u0435 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 (Large Language Model, LLM) \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 <a href=\"https:\/\/en.wikipedia.org\/wiki\/GPT-3\" rel=\"nofollow noopener noreferrer\">GPT-3<\/a> \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 OpenAI, <a href=\"https:\/\/blog.google\/technology\/ai\/lamda\/\" rel=\"nofollow noopener noreferrer\">LaMDA<\/a> \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 Google \u0438 <a href=\"https:\/\/docs.cohere.ai\/docs\/command-beta\" rel=\"nofollow noopener noreferrer\">Command XLarge<\/a> \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 Cohere \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c GPT. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u044b\u043c\u0438 \u0438\u0445 \u0434\u0435\u043b\u0430\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u043e\u043d\u0438 <strong>1)<\/strong> \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 (\u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432) \u0438 <strong>2)<\/strong> \u043e\u0431\u0443\u0447\u0435\u043d\u044b \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0441\u043e\u0442\u043d\u0438 \u0433\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0430).<\/p>\n<p>  \u041f\u043e \u0441\u0443\u0442\u0438, GPT <strong>\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0442\u0435\u043a\u0441\u0442<\/strong> \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 <strong>\u043f\u0440\u043e\u043c\u043f\u0442\u0430 (\u0437\u0430\u043f\u0440\u043e\u0441\u0430)<\/strong>. \u0414\u0430\u0436\u0435 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u043c API (\u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u0442\u0435\u043a\u0441\u0442, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0442\u0435\u043a\u0441\u0442), \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f GPT \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u0442\u0440\u044f\u0441\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/machinelearningknowledge.ai\/ezoimgfmt\/b2611031.smushcdn.com\/2611031\/wp-content\/uploads\/2022\/12\/ChatGPT-Demo-of-Drafting-an-Email.png?lossy=0&amp;amp;strip=1&amp;amp;webp=1&amp;amp;ezimgfmt=ng:webp\/ngcb1\" rel=\"nofollow noopener noreferrer\">\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0430 \u0432\u0430\u0441 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u044b\u0435 \u043f\u0438\u0441\u044c\u043c\u0430<\/a>, <a href=\"https:\/\/machinelearningknowledge.ai\/ezoimgfmt\/b2611031.smushcdn.com\/2611031\/wp-content\/uploads\/2022\/12\/ChatGPT-Example-Book-Summarization.png?lossy=0&amp;amp;strip=1&amp;amp;webp=1&amp;amp;ezimgfmt=ng:webp\/ngcb1\" rel=\"nofollow noopener noreferrer\">\u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0437\u044e\u043c\u0435 \u043a\u043d\u0438\u0433\u0438<\/a>, <a href=\"https:\/\/khrisdigital.com\/wp-content\/uploads\/2022\/12\/image-1.png\" rel=\"nofollow noopener noreferrer\">\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0435\u0439 \u043a \u043f\u043e\u0441\u0442\u0430\u043c \u0432 \u0441\u043e\u0446\u0441\u0435\u0442\u044f\u0445<\/a>, <a href=\"https:\/\/machinelearningknowledge.ai\/ezoimgfmt\/b2611031.smushcdn.com\/2611031\/wp-content\/uploads\/2022\/12\/ChatGPT-Examples-Explaining-Black-Holes.png?lossy=0&amp;amp;strip=1&amp;amp;webp=1&amp;amp;ezimgfmt=ng:webp\/ngcb1\" rel=\"nofollow noopener noreferrer\">\u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u0447\u0451\u0440\u043d\u044b\u0435 \u0434\u044b\u0440\u044b \u043f\u044f\u0442\u0438\u043b\u0435\u0442\u043d\u0435\u043c\u0443 \u0440\u0435\u0431\u0451\u043d\u043a\u0443<\/a>, <a href=\"https:\/\/machinelearningknowledge.ai\/ezoimgfmt\/b2611031.smushcdn.com\/2611031\/wp-content\/uploads\/2022\/12\/ChatGPT-Demo-of-Writing-SQL-Queries.png?lossy=0&amp;amp;strip=1&amp;amp;webp=1&amp;amp;ezimgfmt=ng:webp\/ngcb1\" rel=\"nofollow noopener noreferrer\">\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 SQL<\/a> \u0438 <a href=\"https:\/\/machinelearningknowledge.ai\/ezoimgfmt\/b2611031.smushcdn.com\/2611031\/wp-content\/uploads\/2022\/12\/Chat-GPT-Example-Writing-a-Will.png?lossy=0&amp;amp;strip=1&amp;amp;webp=1&amp;amp;ezimgfmt=ng:webp\/ngcb1\" rel=\"nofollow noopener noreferrer\">\u0434\u0430\u0436\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u0432\u0435\u0449\u0430\u043d\u0438\u0435<\/a>.<\/p>\n<p>  \u042d\u0442\u043e \u0431\u044b\u043b\u043e \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 GPT \u0438 \u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0433\u043b\u0443\u0431\u0438\u043c\u0441\u044f \u0432 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438.<\/p>\n<h3>\u0412\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434<\/h3>\n<p>  \u0421\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 GPT \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def gpt(inputs: list[int]) -> list[list[float]]:     # \u0432\u0432\u043e\u0434\u044b \u0438\u043c\u0435\u044e\u0442 \u0444\u043e\u0440\u043c\u0443 [n_seq]     # \u0432\u044b\u0432\u043e\u0434\u044b \u0438\u043c\u0435\u044e\u0442 \u0444\u043e\u0440\u043c\u0443 [n_seq, n_vocab]     output = # \u0431\u0438\u043f-\u0431\u0438\u043f, \u043c\u0430\u0433\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438     return output<\/code><\/pre>\n<p>  <\/p>\n<h4>\u0412\u0432\u043e\u0434<\/h4>\n<p>  \u0412\u0432\u043e\u0434\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <strong>\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/strong> \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0445 \u0441\u043e\u0431\u043e\u0439 <em>\u0442\u043e\u043a\u0435\u043d\u044b<\/em> \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e <em>\u0442\u0435\u043a\u0441\u0442\u0430<\/em>:<\/p>\n<pre><code class=\"python\"># \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442 \u0442\u043e\u043a\u0435\u043d\u044b \u0432 \u043d\u0430\u0448\u0435\u043c \u0442\u0435\u043a\u0441\u0442\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: # \u0442\u0435\u043a\u0441\u0442 = \"not all heroes wear capes\": # \u0442\u043e\u043a\u0435\u043d\u044b = \"not\"  \"all\" \"heroes\" \"wear\" \"capes\" inputs =   [1,     0,    2,      4,     6]<\/code><\/pre>\n<p>  \u041c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u044f <em>\u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430<\/em>:<\/p>\n<pre><code class=\"python\"># \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u044d\u0442\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 # \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \"heroes\" \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d 2, \u0442\u0430\u043a \u043a\u0430\u043a vocab[2] = \"heroes\" vocab = [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"]  # \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0430\u044f \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u0440\u043e\u0431\u0435\u043b\u0443 tokenizer = WhitespaceTokenizer(vocab)  # \u043c\u0435\u0442\u043e\u0434 encode() \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 str -> list[int] ids = tokenizer.encode(\"not all heroes wear\") # ids = [1, 0, 2, 4]  # \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u044f tokens = [tokenizer.vocab[i] for i in ids] # tokens = [\"not\", \"all\", \"heroes\", \"wear\"]  # \u043c\u0435\u0442\u043e\u0434 decode() \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 list[int] -> str text = tokenizer.decode(ids) # text = \"not all heroes wear\"<\/code><\/pre>\n<p>  \u0412\u043a\u0440\u0430\u0442\u0446\u0435:<\/p>\n<ul>\n<li>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0430.<\/li>\n<li>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0435\u0451 \u043d\u0430 \u0447\u0430\u0441\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u0430\u043c\u0438.<\/li>\n<li>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d\u044b \u0441 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438.<\/li>\n<\/ul>\n<p>  \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u043e\u0431\u0435\u043b\u0430\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/huggingface.co\/course\/chapter6\/5?fw=pt\" rel=\"nofollow noopener noreferrer\">Byte-Pair Encoding<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/huggingface.co\/course\/chapter6\/6?fw=pt\" rel=\"nofollow noopener noreferrer\">WordPiece<\/a>, \u043d\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0442\u0435\u043c \u0436\u0435:<\/p>\n<ol>\n<li>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 <code>vocab<\/code>, \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0442\u043e\u043a\u0435\u043d\u044b \u0441\u0442\u0440\u043e\u043a \u0441 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438<\/li>\n<li>\u0415\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>encode<\/code>, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>str -> list[int]<\/code><\/li>\n<li>\u0415\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>decode<\/code>, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>list[int] -> str<\/code><\/li>\n<\/ol>\n<p>  <\/p>\n<h4>\u0412\u044b\u0432\u043e\u0434<\/h4>\n<p>  \u0412\u044b\u0432\u043e\u0434\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <strong>2D-\u043c\u0430\u0441\u0441\u0438\u0432<\/strong>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c <code>output[i][j]<\/code> \u2014 \u044d\u0442\u043e <strong>\u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u043c\u0430\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c<\/strong> \u043c\u043e\u0434\u0435\u043b\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0442\u043e\u043a\u0435\u043d \u0432 <code>vocab[j]<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c <code>inputs[i+1]<\/code>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">vocab = [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"] inputs = [1, 0, 2, 4] # \"not\" \"all\" \"heroes\" \"wear\" output = gpt(inputs) #              [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"] # output[0] =  [0.75    0.1     0.0       0.15    0.0   0.0    0.0  ] # \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \"not\" \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u043e \"all\"  #              [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"] # output[1] =  [0.0     0.0      0.8     0.1    0.0    0.0   0.1  ] # \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 [\"not\", \"all\"] \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u043e \"heroes\"  #              [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"] # output[-1] = [0.0     0.0     0.0     0.1     0.0    0.05  0.85  ] # \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u043b\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 [\"not\", \"all\", \"heroes\", \"wear\"] \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u043e \"capes\"<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u043d\u043e\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0435\u0440\u0451\u043c \u0442\u043e\u043a\u0435\u043d \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432 <code>output[-1]<\/code>:<\/p>\n<pre><code class=\"python\">vocab = [\"all\", \"not\", \"heroes\", \"the\", \"wear\", \".\", \"capes\"] inputs = [1, 0, 2, 4] # \"not\" \"all\" \"heroes\" \"wear\" output = gpt(inputs) next_token_id = np.argmax(output[-1]) # next_token_id = 6 next_token = vocab[next_token_id] # next_token = \"capes\"<\/code><\/pre>\n<p>  \u0412\u0437\u044f\u0442\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0430 \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <a href=\"https:\/\/docs.cohere.ai\/docs\/controlling-generation-with-top-k-top-p#1-pick-the-top-token-greedy-decoding\" rel=\"nofollow noopener noreferrer\"><strong>greedy decoding<\/strong><\/a> (\u0436\u0430\u0434\u043d\u044b\u043c \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c) \u0438\u043b\u0438 <strong>greedy sampling<\/strong> (\u0436\u0430\u0434\u043d\u044b\u043c \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c).<\/p>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>\u044f\u0437\u044b\u043a\u043e\u0432\u044b\u043c \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c<\/strong>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c GPT <strong>\u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e<\/strong>.<\/p>\n<p>  \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e \u0441\u043b\u043e\u0432\u043e \u2014 \u044d\u0442\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0437\u0434\u043e\u0440\u043e\u0432\u043e, \u043d\u043e \u043a\u0430\u043a \u043d\u0430\u0441\u0447\u0451\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0435\u043b\u044b\u0445 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0430\u0431\u0437\u0430\u0446\u0435\u0432 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435?<\/p>\n<h3>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430<\/h3>\n<p>  <\/p>\n<h4>\u0410\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u0430\u044f<\/h4>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044f \u0443 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0440\u043e\u0433\u043d\u043e\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043a \u0432\u0432\u043e\u0434\u0443:<\/p>\n<pre><code class=\"python\">def generate(inputs, n_tokens_to_generate):     for _ in range(n_tokens_to_generate): # \u0446\u0438\u043a\u043b \u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f         output = gpt(inputs) # \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438         next_id = np.argmax(output[-1]) # \u0436\u0430\u0434\u043d\u043e\u0435 \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435         inputs = np.append(out, [next_id]) # \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0430 \u043a \u0432\u0432\u043e\u0434\u0443     return list(inputs[len(inputs) - n_tokens_to_generate :])  # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 id  input_ids = [1, 0] # \"not\" \"all\" output_ids = generate(input_ids, 3) # output_ids = [2, 4, 6] output_tokens = [vocab[i] for i in output_ids] # \"heroes\" \"wear\" \"capes\"<\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u044f) \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432\u043e \u0432\u0432\u043e\u0434 (\u00ab\u0430\u0432\u0442\u043e\u00bb) \u0438 \u0441\u0442\u0430\u043b \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c GPT \u0438\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <strong>\u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u043e\u0439<\/strong>.<\/p>\n<h4>\u0421\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h4>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e <strong>\u0441\u0442\u043e\u0445\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0441\u0442\u0438<\/strong> (\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0441\u0442\u0438), \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0436\u0430\u0434\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0430 \u0438\u0437 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439:<\/p>\n<pre><code class=\"python\">inputs = [1, 0, 2, 4] # \"not\" \"all\" \"heroes\" \"wear\" output = gpt(inputs) np.random.choice(np.arange(vocab_size), p=output[-1]) # capes np.random.choice(np.arange(vocab_size), p=output[-1]) # hats np.random.choice(np.arange(vocab_size), p=output[-1]) # capes np.random.choice(np.arange(vocab_size), p=output[-1]) # capes np.random.choice(np.arange(vocab_size), p=output[-1]) # pants<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0432\u0432\u043e\u0434\u0443, \u043d\u043e \u0438 \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0436\u0430\u0434\u043d\u044b\u043c \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0442\u0435\u0445\u043d\u0438\u043a\u0438 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 <a href=\"https:\/\/docs.cohere.ai\/docs\/controlling-generation-with-top-k-top-p#2-pick-from-amongst-the-top-tokens-top-k\" rel=\"nofollow noopener noreferrer\"><strong>top-k<\/strong><\/a>, <a href=\"https:\/\/docs.cohere.ai\/docs\/controlling-generation-with-top-k-top-p#3-pick-from-amongst-the-top-tokens-whose-probabilities-add-up-to-15-top-p\" rel=\"nofollow noopener noreferrer\"><strong>top-p<\/strong><\/a> \u0438 <a href=\"https:\/\/docs.cohere.ai\/docs\/temperature\" rel=\"nofollow noopener noreferrer\"><strong>temperature<\/strong><\/a> \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u0435\u0440\u0435\u0434 \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e. \u042d\u0442\u043e \u0435\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0439 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0435 \u00ab\u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b\u00bb \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0440\u0438\u0441\u043a\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438, \u0434\u0435\u043b\u0430\u044f \u0435\u0451 \u0431\u043e\u043b\u0435\u0435 \u00ab\u0442\u0432\u043e\u0440\u0447\u0435\u0441\u043a\u043e\u0439\u00bb).<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0435\u0445\u043d\u0438\u043a \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e <a href=\"https:\/\/lilianweng.github.io\/posts\/2021-01-02-controllable-text-generation\/\" rel=\"nofollow noopener noreferrer\">Controllable Neural Text Generation<\/a> \u041b\u0438\u043b\u043b\u0438\u0430\u043d \u0412\u0435\u043d\u0433.<\/p>\n<h3>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u041c\u044b \u043e\u0431\u0443\u0447\u0430\u0435\u043c GPT \u043a\u0430\u043a \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u2014 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gradient_descent\" rel=\"nofollow noopener noreferrer\"><strong>\u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430<\/strong><\/a> \u0441 \u0443\u0447\u0451\u0442\u043e\u043c \u043d\u0435\u043a\u043e\u0435\u0439 <strong>\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0442\u0435\u0440\u044c<\/strong>. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 GPT \u043c\u044b \u0431\u0435\u0440\u0451\u043c <a href=\"https:\/\/www.youtube.com\/watch?v=ErfnhcEV1O8\" rel=\"nofollow noopener noreferrer\"><strong>\u043f\u043e\u0442\u0435\u0440\u044e \u043f\u0435\u0440\u0435\u043a\u0440\u0451\u0441\u0442\u043d\u043e\u0439 \u044d\u043d\u0442\u0440\u043e\u043f\u0438\u0438<\/strong><\/a> \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">def lm_loss(inputs: list[int], params) -> float:     # \u043c\u0435\u0442\u043a\u0438 y - \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e input, \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 \u043d\u0430 1 \u0432\u043b\u0435\u0432\u043e     #     # inputs = [not,     all,   heros,   wear,   capes]     #      x = [not,     all,   heroes,  wear]     #      y = [all,  heroes,     wear,  capes]     #      # \u0440\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043c\u0435\u0442\u043a\u0438 \u0434\u043b\u044f inputs[-1], \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0435\u0451 \u0438\u0437 x     #     # \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f N \u0432\u0432\u043e\u0434\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 N - 1 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043f\u0430\u0440 \u0434\u043b\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f     x, y = inputs[:-1], inputs[1:]          # \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434     # \u0432\u0441\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439 \u0441\u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438     output = gpt(x, params)          # \u043f\u043e\u0442\u0435\u0440\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u0451\u0441\u0442\u043d\u043e\u0439 \u044d\u043d\u0442\u0440\u043e\u043f\u0438\u0438     # \u043c\u044b \u0431\u0435\u0440\u0451\u043c \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u043f\u043e \u0432\u0441\u0435\u043c N-1 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c     loss = np.mean(-np.log(output[y]))      return loss  def train(texts: list[list[str]], params) -> float:     for text in texts:         inputs = tokenizer.encode(text)         loss = lm_loss(inputs, params)         gradients = compute_gradients_via_backpropagation(loss, params)         params = gradient_descent_update_step(gradients, params)     return params<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>params<\/code> \u043a\u043e \u0432\u0432\u043e\u0434\u0443 <code>gpt<\/code>. \u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u044d\u0442\u0430\u043f \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438, \u0434\u0435\u043b\u0430\u044f \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0441\u0451 \u043b\u0443\u0447\u0448\u0435 \u0438 \u043b\u0443\u0447\u0448\u0435 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u044f\u0437\u044b\u043a\u0430 \u0441 \u043a\u0430\u0436\u0434\u044b\u043c \u043d\u043e\u0432\u044b\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u043c \u0442\u0435\u043a\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043d\u0430 \u0432\u0438\u0434\u0438\u0442. \u042d\u0442\u043e \u043a\u0440\u0430\u0439\u043d\u0435 \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043e\u0434\u043d\u0430\u043a\u043e \u043e\u043d\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432 \u0446\u0435\u043b\u043e\u043c.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u044b input\/label \u043c\u043e\u0436\u043d\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437 \u0441\u044b\u0440\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430. \u042d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Self-supervised_learning\" rel=\"nofollow noopener noreferrer\">self-supervised learning<\/a><\/strong> (\u0441\u0430\u043c\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c).<\/p>\n<p>  \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u044b\u0440\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, GPT-3 \u0431\u044b\u043b\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0430 \u043d\u0430 <strong>300 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/strong> \u0442\u0435\u043a\u0441\u0442\u0430 \u0438\u0437 \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u0438 \u043a\u043d\u0438\u0433:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/dr\/j-\/3h\/drj-3hjjvksa2awpm60kc8_qi3a.png\" data-src=\"https:\/\/habrastorage.org\/webt\/dr\/j-\/3h\/drj-3hjjvksa2awpm60kc8_qi3a.png\"\/><\/div>\n<p>  <i>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 2.2 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e GPT-3<\/i><\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0431\u041e\u043b\u044c\u0448\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 GPT-3 \u0438\u043c\u0435\u0435\u0442 <strong>175 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432<\/strong>, \u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0442\u0440\u0430\u0442\u044b \u043d\u0430 \u0435\u0451 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 <a href=\"https:\/\/twitter.com\/eturner303\/status\/1266264358771757057\" rel=\"nofollow noopener noreferrer\">$1-10 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432<\/a>. [\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e <a href=\"https:\/\/arxiv.org\/pdf\/2210.11416.pdf\" rel=\"nofollow noopener noreferrer\">InstructGPT<\/a> \u0438 <a href=\"https:\/\/arxiv.org\/pdf\/2203.15556.pdf\" rel=\"nofollow noopener noreferrer\">Chinchilla<\/a> \u043c\u044b \u043e\u0441\u043e\u0437\u043d\u0430\u043b\u0438, \u0447\u0442\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0430\u0442\u044c \u0441\u0442\u043e\u043b\u044c \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. \u041e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u0438 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 GPT \u0441 1,3 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0432\u0437\u043e\u0439\u0442\u0438 GPT-3 \u0441 175 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.]<\/p>\n<p>  \u042d\u0442\u043e\u0442 \u044d\u0442\u0430\u043f \u0441\u0430\u043c\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>pre-training<\/strong> (\u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0441\u0430 \u00ab\u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u0445\u00bb \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0441\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u0442\u0432\u0438\u0442\u0430.<\/p>\n<p>  \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u0443\u0433\u043b\u0443\u0431\u043b\u0451\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>fine-tuning<\/strong> (\u043f\u043e\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0435\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0443\u0436\u0435 \u0431\u044b\u043b\u0438 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u044f\u0437\u044b\u043a\u0430 \u0438 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0434\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0434 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>  \u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u00ab\u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0431\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 + \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u00bb \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transfer_learning\" rel=\"nofollow noopener noreferrer\">\u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u044b\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c<\/a>.<\/p>\n<h3>\u041f\u0440\u043e\u043c\u043f\u0442\u0438\u043d\u0433<\/h3>\n<p>  \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f <a href=\"https:\/\/s3-us-west-2.amazonaws.com\/openai-assets\/research-covers\/language-unsupervised\/language_understanding_paper.pdf\" rel=\"nofollow noopener noreferrer\">GPT<\/a> \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <a href=\"https:\/\/arxiv.org\/pdf\/1810.04805.pdf\" rel=\"nofollow noopener noreferrer\">BERT<\/a>.<\/p>\n<p>  \u0418 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0443\u0447\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439 \u043e <a href=\"https:\/\/d4mucfpksywv.cloudfront.net\/better-language-models\/language_models_are_unsupervised_multitask_learners.pdf\" rel=\"nofollow noopener noreferrer\">GPT-2<\/a> and <a href=\"https:\/\/arxiv.org\/abs\/2005.14165\" rel=\"nofollow noopener noreferrer\">GPT-3<\/a> \u043c\u044b \u043e\u0441\u043e\u0437\u043d\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c GPT \u0441\u0430\u043c\u0430 \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u043f\u0442\u0430 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0433\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u0439\u043a\u0438. \u042d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>in-context learning<\/strong> (\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u0440\u043e\u043c\u043f\u0442\u0430. \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u0435\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 (zero shot), \u0441 \u043e\u0434\u043d\u0438\u043c (one shot) \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 (few shot) \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/u8\/d6\/6e\/u8d66erpfwuwxuldc7gajqcrkjk.png\" data-src=\"https:\/\/habrastorage.org\/webt\/u8\/d6\/6e\/u8d66erpfwuwxuldc7gajqcrkjk.png\"\/><\/div>\n<p>  <i>\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u044f 2.1 \u0438\u0437 \u043d\u0430\u0443\u0447\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e GPT-3<\/i><\/p>\n<p>  \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/openai.com\/blog\/chatgpt\/\" rel=\"nofollow noopener noreferrer\">GPT \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0447\u0430\u0442-\u0431\u043e\u0442\u0430<\/a>, \u0430 \u043d\u0435 \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0435\u0451 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u00ab\u0437\u0430\u0434\u0430\u0447\u0438\u00bb. \u0418\u0441\u0442\u043e\u0440\u0438\u044f \u0431\u0435\u0441\u0435\u0434\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u043c\u043f\u0442\u0430, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0441 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u00ab\u0422\u044b \u0447\u0430\u0442-\u0431\u043e\u0442, \u0432\u0435\u0434\u0438 \u0441\u0435\u0431\u044f \u0445\u043e\u0440\u043e\u0448\u043e\u00bb. \u0415\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u043c\u043f\u0442, \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0434\u0430\u0442\u044c <a href=\"https:\/\/imgur.com\/a\/AbDFcgk\" rel=\"nofollow noopener noreferrer\">\u0447\u0430\u0442-\u0431\u043e\u0442\u0443 \u0447\u0435\u0440\u0442\u044b \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438<\/a>.<\/p>\n<p>  \u0420\u0430\u0437\u043e\u0431\u0440\u0430\u0432\u0448\u0438\u0441\u044c \u0441\u043e \u0432\u0441\u0435\u043c \u044d\u0442\u0438\u043c, \u043c\u043e\u0436\u043d\u043e, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438!<\/p>\n<h2>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h2>\n<p>  \u041a\u043b\u043e\u043d\u0438\u0440\u0443\u0439\u0442\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430:<\/p>\n<pre><code class=\"bash\">git clone https:\/\/github.com\/jaymody\/picoGPT cd picoGPT<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"bash\">pip install -r requirements.txt<\/code><\/pre>\n<p>  \u0423\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u043d\u0430 Macbook \u0441 M1, \u0442\u043e \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c <code>pip install<\/code>, \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432 <code>requirements.txt<\/code> <code>tensorflow<\/code> \u043d\u0430 <code>tensorflow-macos<\/code>. \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u043d\u0430 <code>Python 3.9.10<\/code>.<\/p>\n<p>  \u041a\u0440\u0430\u0442\u043a\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432:<\/p>\n<ul>\n<li><strong><code>encoder.py<\/code><\/strong> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u043e\u0434 BPE Tokenizer \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 OpenAI, \u0432\u0437\u044f\u0442\u044b\u0439 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0437 \u0435\u0451 <a href=\"https:\/\/github.com\/openai\/gpt-2\/blob\/master\/src\/encoder.py\" rel=\"nofollow noopener noreferrer\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f gpt-2<\/a>.<\/li>\n<li><strong><code>utils.py<\/code><\/strong> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u043e\u0434 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u0435\u0441\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 GPT-2, \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430 \u0438 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.<\/li>\n<li><strong><code>gpt2.py<\/code><\/strong> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0430\u043c\u0443 \u043c\u043e\u0434\u0435\u043b\u044c GPT \u0438 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 Python.<\/li>\n<li><strong><code>gpt2_pico.py<\/code><\/strong> \u2014 \u044d\u0442\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 <code>gpt2.py<\/code>, \u043d\u043e \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0441\u0442\u0440\u043e\u043a. \u0417\u0430\u0447\u0435\u043c? \u0410 \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438 \u043d\u0435\u0442.<\/li>\n<\/ul>\n<p>  \u041c\u044b \u0437\u0430\u043d\u043e\u0432\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c <code>gpt2.py<\/code> \u0441 \u043d\u0443\u043b\u044f, \u0442\u0430\u043a \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043c \u0435\u0433\u043e \u0438 \u0432\u043e\u0441\u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u043e\u0439 \u0444\u0430\u0439\u043b:<\/p>\n<pre><code class=\"bash\">rm gpt2.py touch gpt2.py<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0441\u0442\u0430\u0432\u0438\u043c \u0432 <code>gpt2.py<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">import numpy as np   def gpt2(inputs, wte, wpe, blocks, ln_f, n_head):     pass # TODO: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e   def generate(inputs, params, n_head, n_tokens_to_generate):     from tqdm import tqdm      for _ in tqdm(range(n_tokens_to_generate), \"generating\"):  # \u0446\u0438\u043a\u043b \u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f         logits = gpt2(inputs, **params, n_head=n_head)  # \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438         next_id = np.argmax(logits[-1])  # \u0436\u0430\u0434\u043d\u043e\u0435 \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435         inputs = np.append(inputs, [next_id])  # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0433\u043d\u043e\u0437 \u043a \u0432\u0432\u043e\u0434\u0443      return list(inputs[len(inputs) - n_tokens_to_generate :])  # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 id   def main(prompt: str, n_tokens_to_generate: int = 40, model_size: str = \"124M\", models_dir: str = \"models\"):     from utils import load_encoder_hparams_and_params      # \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c encoder, hparams, \u0438 params \u0438\u0437 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0445 open-ai \u0444\u0430\u0439\u043b\u043e\u0432 gpt-2     encoder, hparams, params = load_encoder_hparams_and_params(model_size, models_dir)      # \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432\u0432\u043e\u0434\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 BPE tokenizer     input_ids = encoder.encode(prompt)      # \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u043d\u0435 \u0432\u044b\u0448\u043b\u0438 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438     assert len(input_ids) + n_tokens_to_generate &lt; hparams[\"n_ctx\"]      # \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c id \u0432\u044b\u0432\u043e\u0434\u0430     output_ids = generate(input_ids, params, hparams[\"n_head\"], n_tokens_to_generate)      # \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c id \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0443     output_text = encoder.decode(output_ids)      return output_text   if __name__ == \"__main__\":     import fire      fire.Fire(main)<\/code><\/pre>\n<p>  \u041e\u043f\u0438\u0448\u0435\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u0447\u0435\u0442\u044b\u0440\u0451\u0445 \u0447\u0430\u0441\u0442\u0435\u0439:<\/p>\n<ol>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>gpt2<\/code> \u2014 \u044d\u0442\u043e \u0441\u0430\u043c \u043a\u043e\u0434 GPT, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0440\u044f\u0434\u0443 \u0441 <code>inputs<\/code> \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<br \/> \n<ul>\n<li><code>wte<\/code>, <code>wpe<\/code>, <code>blocks<\/code> \u0438 <code>ln_f<\/code> \u2014 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0434\u0435\u043b\u0438.<\/li>\n<li><code>n_head<\/code> \u2014 \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 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0430.<\/li>\n<\/ul>\n<\/li>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>generate<\/code> \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0430\u0432\u0442\u043e\u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0432\u0438\u0434\u0435\u043b\u0438 \u0440\u0430\u043d\u0435\u0435. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043c\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0436\u0430\u0434\u043d\u044b\u043c \u0441\u044d\u043c\u043f\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c. <a href=\"https:\/\/www.google.com\/search?q=tqdm\" rel=\"nofollow noopener noreferrer\"><code>tqdm<\/code><\/a> \u2014 \u044d\u0442\u043e \u0448\u043a\u0430\u043b\u0430 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0430, \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0449\u0430\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043e\u0434\u0438\u043d \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u044b.<\/li>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>main<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<br \/> \n<ol>\n<li>\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440 (<code>encoder<\/code>), \u0432\u0435\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 (<code>params<\/code>) \u0438 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b (<code>hparams<\/code>)<\/li>\n<li>\u041a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u043e\u043c\u043f\u0442 \u0432\u0432\u043e\u0434\u0430 \u0432 ID \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430<\/li>\n<li>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e generate<\/li>\n<li>\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 ID \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443<\/li>\n<\/ol>\n<p>  <\/li>\n<li><a href=\"https:\/\/github.com\/google\/python-fire\" rel=\"nofollow noopener noreferrer\"><code>fire.Fire(main)<\/code><\/a> \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0448 \u0444\u0430\u0439\u043b \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 CLI, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: <code>python gpt2.py \"\u0437\u0434\u0435\u0441\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u043c\u043f\u0442\"<\/code><\/li>\n<\/ol>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u0441\u043c\u043e\u0442\u0440\u0438\u043c\u0441\u044f \u043a <code>encoder<\/code>, <code>hparams<\/code> \u0438 <code>params<\/code>. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432 \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438 Python \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">from utils import load_encoder_hparams_and_params encoder, hparams, params = load_encoder_hparams_and_params(\"124M\", \"models\")<\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 <code>models\/124M<\/code> <a href=\"https:\/\/github.com\/jaymody\/picoGPT\/blob\/a750c145ba4d09d5764806a6c78c71ffaff88e64\/utils.py#L13-L40\" rel=\"nofollow noopener noreferrer\">\u0441\u043a\u0430\u0447\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430<\/a>, \u0430 \u0432 \u043d\u0430\u0448 \u043a\u043e\u0434 <a href=\"https:\/\/github.com\/jaymody\/picoGPT\/blob\/a750c145ba4d09d5764806a6c78c71ffaff88e64\/utils.py#L68-L82\" rel=\"nofollow noopener noreferrer\"><code>\u0437\u0430\u0433\u0440\u0443\u0437\u044f\u0442\u0441\u044f encoder<\/code>, <code>hparams<\/code> \u0438 <code>params<\/code><\/a>.<\/p>\n<h3>Encoder<\/h3>\n<p>  <code>encoder<\/code> \u2014 \u044d\u0442\u043e BPE tokenizer, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 GPT-2:<\/p>\n<pre><code class=\"python\">>>> ids = encoder.encode(\"Not all heroes wear capes.\") >>> ids [3673, 477, 10281, 5806, 1451, 274, 13]  >>> encoder.decode(ids) \"Not all heroes wear capes.\"<\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430 (\u0445\u0440\u0430\u043d\u044f\u0449\u0435\u0433\u043e\u0441\u044f \u0432 <code>encoder.decoder<\/code>) \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b:<\/p>\n<pre><code class=\"python\">>>> [encoder.decoder[i] for i in ids] ['Not', '\u0120all', '\u0120heroes', '\u0120wear', '\u0120cap', 'es', '.']<\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0442\u043e\u043a\u0435\u043d\u044b \u2014 \u044d\u0442\u043e \u0441\u043b\u043e\u0432\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>Not<\/code>), \u0438\u043d\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0441\u043b\u043e\u0432\u0430, \u043d\u043e \u0441 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u043c \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>\u0120all<\/code> (<a href=\"https:\/\/github.com\/karpathy\/minGPT\/blob\/37baab71b9abea1b76ab957409a1cc2fbfba8a26\/mingpt\/bpe.py#L22-L33\" rel=\"nofollow noopener noreferrer\"><code>\u0120<\/code> \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u0435\u043b<\/a>), \u0438\u043d\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u0441\u043b\u043e\u0432\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0432\u043e capes \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043e \u043d\u0430 <code>\u0120cap<\/code> \u0438 <code>es<\/code>), \u0430 \u0438\u043d\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0437\u043d\u0430\u043a\u0438 \u043f\u0440\u0435\u043f\u0438\u043d\u0430\u043d\u0438\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>.<\/code>).<\/p>\n<p>  BPE \u0443\u0434\u043e\u0431\u0435\u043d \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443. \u0415\u0441\u043b\u0438 \u043e\u043d \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442 \u0442\u043e, \u0447\u0435\u0433\u043e \u043d\u0435\u0442 \u0432 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0438, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u043e \u043d\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442:<\/p>\n<pre><code class=\"python\">>>> [encoder.decoder[i] for i in encoder.encode(\"zjqfl\")] ['z', 'j', 'q', 'fl']<\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u044f:<\/p>\n<pre><code class=\"python\">>>> len(encoder.decoder) 50257<\/code><\/pre>\n<p>  \u0412\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0445 \u043f\u0430\u0440, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f <em>\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c<\/em> \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u0430. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440, \u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0443\u0436\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0439 \u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0445 \u043f\u0430\u0440 \u0438\u0437 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0444\u0430\u0439\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0441\u043a\u0430\u0447\u0430\u043d\u044b \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 <code>load_encoder_hparams_and_params<\/code>. \u0421\u043c. <code>models\/124M\/encoder.json<\/code> (\u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0439) \u0438 <code>models\/124M\/vocab.bpe<\/code> (\u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0445 \u043f\u0430\u0440).<\/p>\n<h3>\u0413\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/h3>\n<p>  <code>hparams<\/code> \u2014 \u044d\u0442\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0433\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">>>> hparams {   \"n_vocab\": 50257, # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0432 \u0432\u043e\u043a\u0430\u0431\u0443\u043b\u044f\u0440\u0438\u0438   \"n_ctx\": 1024, # \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u0432\u043e\u0434\u0430   \"n_embd\": 768, # \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 (\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \"\u0448\u0438\u0440\u0438\u043d\u0443\" \u0441\u0435\u0442\u0438)   \"n_head\": 12, # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u043b\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (n_embd \u0434\u043e\u043b\u0436\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043d\u0430 n_head)   \"n_layer\": 12 # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0451\u0432 (\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \"\u0433\u043b\u0443\u0431\u0438\u043d\u0443\" \u0441\u0435\u0442\u0438) }<\/code><\/pre>\n<p>  \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a \u043a\u043e\u0434\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044e\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443. \u0422\u0430\u043a\u0436\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>n_seq<\/code> \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u0432\u043e\u0434\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>n_seq = len(inputs)<\/code>).<\/p>\n<h3>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b<\/h3>\n<p>  <code>params<\/code> \u2014 \u044d\u0442\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 json-\u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438. \u0423\u0437\u043b\u044b \u043b\u0438\u0441\u0442\u044c\u0435\u0432 json \u2014 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u044b NumPy. \u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u044b\u0432\u0435\u0434\u0435\u043c <code>params<\/code>, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043d\u0430 \u0438\u0445 shape, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">>>> import numpy as np >>> def shape_tree(d): >>>     if isinstance(d, np.ndarray): >>>         return list(d.shape) >>>     elif isinstance(d, list): >>>         return [shape_tree(v) for v in d] >>>     elif isinstance(d, dict): >>>         return {k: shape_tree(v) for k, v in d.items()} >>>     else: >>>         ValueError(\"uh oh\") >>>  >>> print(shape_tree(params)) {     \"wpe\": [1024, 768],     \"wte\": [50257, 768],         \"ln_f\": {\"b\": [768], \"g\": [768]},     \"blocks\": [         {             \"attn\": {                 \"c_attn\": {\"b\": [2304], \"w\": [768, 2304]},                 \"c_proj\": {\"b\": [768], \"w\": [768, 768]},             },             \"ln_1\": {\"b\": [768], \"g\": [768]},             \"ln_2\": {\"b\": [768], \"g\": [768]},             \"mlp\": {                 \"c_fc\": {\"b\": [3072], \"w\": [768, 3072]},                 \"c_proj\": {\"b\": [768], \"w\": [3072, 768]},             },         },         ... # \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0434\u043b\u044f n_layers     ] }<\/code><\/pre>\n<p>  \u0412\u0441\u0451 \u044d\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043a\u043f\u043e\u0438\u043d\u0442\u0430 tensorflow \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 OpenAI:<\/p>\n<pre><code class=\"python\">>>> import tensorflow as tf >>> tf_ckpt_path = tf.train.latest_checkpoint(\"models\/124M\") >>> for name, _ in tf.train.list_variables(tf_ckpt_path): >>>     arr = tf.train.load_variable(tf_ckpt_path, name).squeeze() >>>     print(f\"{name}: {arr.shape}\") model\/h0\/attn\/c_attn\/b: (2304,) model\/h0\/attn\/c_attn\/w: (768, 2304) model\/h0\/attn\/c_proj\/b: (768,) model\/h0\/attn\/c_proj\/w: (768, 768) model\/h0\/ln_1\/b: (768,) model\/h0\/ln_1\/g: (768,) model\/h0\/ln_2\/b: (768,) model\/h0\/ln_2\/g: (768,) model\/h0\/mlp\/c_fc\/b: (3072,) model\/h0\/mlp\/c_fc\/w: (768, 3072) model\/h0\/mlp\/c_proj\/b: (768,) model\/h0\/mlp\/c_proj\/w: (3072, 768) model\/h1\/attn\/c_attn\/b: (2304,) model\/h1\/attn\/c_attn\/w: (768, 2304) ... model\/h9\/mlp\/c_proj\/b: (768,) model\/h9\/mlp\/c_proj\/w: (3072, 768) model\/ln_f\/b: (768,) model\/ln_f\/g: (768,) model\/wpe: (1024, 768) model\/wte: (50257, 768)<\/code><\/pre>\n<p>  <a href=\"https:\/\/github.com\/jaymody\/picoGPT\/blob\/29e78cc52b58ed2c1c483ffea2eb46ff6bdec785\/utils.py#L43-L65\" rel=\"nofollow noopener noreferrer\">\u041f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u043d\u0438\u0436\u0435 \u043a\u043e\u0434<\/a> \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 tensorflow \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c <code>params<\/code>.<\/p>\n<p>  \u0414\u043b\u044f \u0441\u043f\u0440\u0430\u0432\u043a\u0438 \u0432\u043e\u0442 shape <code>params<\/code>, \u0433\u0434\u0435 \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u043d\u0430 <code>hparams<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442:<\/p>\n<pre><code class=\"python\">{     \"wpe\": [n_ctx, n_embd],     \"wte\": [n_vocab, n_embd],         \"ln_f\": {\"b\": [n_embd], \"g\": [n_embd]},     \"blocks\": [         {             \"attn\": {                 \"c_attn\": {\"b\": [3*n_embd], \"w\": [n_embd, 3*n_embd]},                 \"c_proj\": {\"b\": [n_embd], \"w\": [n_embd, n_embd]},             },             \"ln_1\": {\"b\": [n_embd], \"g\": [n_embd]},             \"ln_2\": {\"b\": [n_embd], \"g\": [n_embd]},             \"mlp\": {                 \"c_fc\": {\"b\": [4*n_embd], \"w\": [n_embd, 4*n_embd]},                 \"c_proj\": {\"b\": [n_embd], \"w\": [4*n_embd, n_embd]},             },         },         ... # \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0434\u043b\u044f n_layers     ] }<\/code><\/pre>\n<p>  \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0430\u0440\u044e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c shape \u0432\u0435\u0441\u043e\u0432 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0435\u0439 GPT. \u0414\u043b\u044f \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u0438 \u0438\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0430\u043c \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f.<\/p>\n<h2>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u043e\u0438<\/h2>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043c\u044b \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0441\u0430\u043c\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 GPT, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0447\u0430\u0441\u0442\u044c \u0441\u0430\u043c\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0441\u043b\u043e\u0451\u0432 \u0441\u0435\u0442\u0438, \u043d\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0445 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 GPT.<\/p>\n<h3>GELU<\/h3>\n<p>  \u0414\u043b\u044f \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0441\u0442\u0438 (<strong>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438<\/strong>) \u0432\u044b\u0431\u043e\u0440\u0430 GPT-2 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/arxiv.org\/pdf\/1606.08415.pdf\" rel=\"nofollow noopener noreferrer\">GELU (Gaussian Error Linear Units)<\/a>, \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 ReLU:<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/sx\/tp\/4h\/sxtp4h-zbrh3xgbx1luzkgltepa.png\" data-src=\"https:\/\/habrastorage.org\/webt\/sx\/tp\/4h\/sxtp4h-zbrh3xgbx1luzkgltepa.png\"\/><\/div>\n<p>  <i>\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1 \u0438\u0437 \u043d\u0430\u0443\u0447\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e GELU<\/i><\/p>\n<p>  \u041e\u043d\u0430 \u0430\u043f\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"python\">def gelu(x):     return 0.5 * x * (1 + np.tanh(np.sqrt(2 \/ np.pi) * (x + 0.044715 * x**3)))<\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0438 ReLU, GELU \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u0432\u043e\u0434 \u043f\u043e\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043d\u043e:<\/p>\n<pre><code class=\"python\">>>> gelu(np.array([[1, 2], [-2, 0.5]])) array([[ 0.84119,  1.9546 ],        [-0.0454 ,  0.34571]])<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 GELU \u0432 \u043c\u043e\u0434\u0435\u043b\u044f\u0445-\u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0430 <a href=\"https:\/\/arxiv.org\/pdf\/1810.04805.pdf\" rel=\"nofollow noopener noreferrer\">BERT<\/a>, \u0438 \u044f \u0434\u0443\u043c\u0430\u044e, \u0435\u0451 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0449\u0451 \u0434\u043e\u043b\u0433\u043e.<\/p>\n<h3>Softmax<\/h3>\n<p>  \u0421\u0442\u0430\u0440\u044b\u0439 \u0434\u043e\u0431\u0440\u044b\u0439 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Softmax_function\" rel=\"nofollow noopener noreferrer\">softmax<\/a>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/7c3\/d92\/ac3\/7c3d92ac3c08cdb5a29ae9f22fbbc226.svg\" alt=\"$\\text{softmax}(x)_i = \\frac{e^{x_i}}{\\sum_j e^{x_j}}$\" data-tex=\"display\"\/><\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def softmax(x):     exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))     return exp_x \/ np.sum(exp_x, axis=-1, keepdims=True)<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/jaykmody.com\/blog\/stable-softmax\/\" rel=\"nofollow noopener noreferrer\"><code>\u0442\u0440\u044e\u043a \u0441 max(x)<\/code><\/a>.<\/p>\n<p>  Softmax \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b (\u043e\u0442 <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/e14\/250\/606\/e142506069924ab8e43f137e4f74835f.svg\" alt=\"$-\\infty$\" data-tex=\"inline\"\/> \u0434\u043e <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/593\/801\/555\/593801555e4ca493976f80bc4e9b5daa.svg\" alt=\"$\\infty$\" data-tex=\"inline\"\/>) \u0432 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 (\u043e\u0442 0 \u0434\u043e 1, \u0433\u0434\u0435 \u0441\u0443\u043c\u043c\u0430 \u0432\u0441\u0435\u0445 \u0447\u0438\u0441\u0435\u043b \u0440\u0430\u0432\u043d\u0430 1). \u041c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c <code>softmax<\/code> \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043e\u0441\u0438 \u0432\u0432\u043e\u0434\u0430.<\/p>\n<pre><code class=\"python\">>>> x = softmax(np.array([[2, 100], [-5, 0]])) >>> x array([[0.00034, 0.99966],        [0.26894, 0.73106]]) >>> x.sum(axis=-1) array([1., 1.])<\/code><\/pre>\n<p>  <\/p>\n<h3>\u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0451\u0432<\/h3>\n<p>  <a href=\"https:\/\/arxiv.org\/pdf\/1607.06450.pdf\" rel=\"nofollow noopener noreferrer\">\u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0451\u0432<\/a> \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0438\u043c\u0435\u043b\u0438 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 0 \u0438 \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044e 1:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/8aa\/93d\/854\/8aa93d8540458f1954289537f3e31274.svg\" alt=\"$\\text{LayerNorm}(x) = \\gamma \\cdot \\frac{x - \\mu}{\\sqrt{\\sigma^2}} + \\beta$\" data-tex=\"display\"\/><\/p>\n<p>  \u0433\u0434\u0435 <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/658\/3bf\/7e6\/6583bf7e62f62291a44864774bbafe66.svg\" alt=\"$\\mu$\" data-tex=\"inline\"\/> \u2014 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/4cc\/fd4\/32e\/4ccfd432ea4f2a64f3a5c8c7378517af.svg\" alt=\"$x$\" data-tex=\"inline\"\/>, <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/dff\/37f\/4b1\/dff37f4b1259e9dd611dbe8abb952cc1.svg\" alt=\"$\\sigma^2$\" data-tex=\"inline\"\/> \u2014 \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/4cc\/fd4\/32e\/4ccfd432ea4f2a64f3a5c8c7378517af.svg\" alt=\"$x$\" data-tex=\"inline\"\/>, \u0430 <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/e70\/a4d\/c8c\/e70a4dc8cfb514fee86d8651daf8c7eb.svg\" alt=\"$\\gamma$\" data-tex=\"inline\"\/> \u0438 <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/dde\/35b\/631\/dde35b63133e1e49275c05ed00f4cd5d.svg\" alt=\"$\\beta$\" data-tex=\"inline\"\/> \u2014 \u0438\u0437\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<pre><code class=\"python\">def layer_norm(x, g, b, eps: float = 1e-5):     mean = np.mean(x, axis=-1, keepdims=True)     variance = np.var(x, axis=-1, keepdims=True)     x = (x - mean) \/ np.sqrt(variance + eps)  # \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0443\u0435\u043c x, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c mean=0 \u0438 var=1 \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043e\u0441\u0438     return g * x + b  # \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c \u0438 \u0441\u043c\u0435\u0449\u0430\u0435\u043c \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 gamma\/beta<\/code><\/pre>\n<p>  \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0451\u0432 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0432\u0432\u043e\u0434\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435, \u0447\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0438 \u0441\u0442\u0430\u0431\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. \u041a\u0430\u043a \u0438 \u043f\u0440\u0438 <a href=\"https:\/\/arxiv.org\/pdf\/1502.03167.pdf\" rel=\"nofollow noopener noreferrer\">Batch Normalization<\/a>, \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0437\u0430\u0442\u0435\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0441\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0434\u0432\u0443\u0445 \u0438\u0437\u0443\u0447\u0430\u0435\u043c\u044b\u0445 \u0432\u0435\u043a\u0442\u043e\u0440\u043e\u0432 gamma \u0438 beta. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0447\u043b\u0435\u043d epsilon \u0432 \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u043d\u043e\u043b\u044c.<\/p>\n<p>  \u041f\u043e <a href=\"https:\/\/stats.stackexchange.com\/questions\/474440\/why-do-transformers-use-layer-norm-instead-of-batch-norm\" rel=\"nofollow noopener noreferrer\">\u0440\u0430\u0437\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c<\/a> \u0432 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435 batch norm, \u0430 \u043d\u043e\u0440\u043c\u0430 \u0441\u043b\u043e\u0451\u0432. \u0420\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0430\u043c\u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 <a href=\"https:\/\/tungmphung.com\/deep-learning-normalization-methods\/\" rel=\"nofollow noopener noreferrer\">\u044d\u0442\u043e\u043c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0441\u0442\u0435<\/a>.<\/p>\n<p>  \u041c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u0451\u0432 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043e\u0441\u0438 \u0432\u0432\u043e\u0434\u0430.<\/p>\n<pre><code class=\"python\">>>> x = np.array([[2, 2, 3], [-5, 0, 1]]) >>> x = layer_norm(x, g=np.ones(x.shape[-1]), b=np.zeros(x.shape[-1])) >>> x array([[-0.70709, -0.70709,  1.41418],        [-1.397  ,  0.508  ,  0.889  ]]) >>> x.var(axis=-1) array([0.99996, 1.     ]) # \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 >>> x.mean(axis=-1) array([-0., -0.])<\/code><\/pre>\n<p>  <\/p>\n<h3>\u041b\u0438\u043d\u0435\u0439\u043d\u043e\u0441\u0442\u044c<\/h3>\n<p>  \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0435 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 + \u043f\u0435\u0440\u0435\u043a\u043e\u0441:<\/p>\n<pre><code class=\"python\">def linear(x, w, b):  # [m, in], [in, out], [out] -> [m, out]     return x @ w + b<\/code><\/pre>\n<p>  \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u043b\u043e\u0438 \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <strong>\u043f\u0440\u043e\u0435\u043a\u0446\u0438\u044f\u043c\u0438<\/strong> (\u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0438 \u043f\u0440\u043e\u0435\u0446\u0438\u0440\u0443\u044e\u0442 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0435).<\/p>\n<pre><code class=\"python\">>>> x = np.random.normal(size=(64, 784)) # input dim = 784, batch\/sequence dim = 64 >>> w = np.random.normal(size=(784, 10)) # output dim = 10 >>> b = np.random.normal(size=(10,)) >>> x.shape # shape \u0434\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (64, 784) >>> linear(x, w, b).shape # shape \u043f\u043e\u0441\u043b\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (64, 10)<\/code><\/pre>\n<p>  <i>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442.<\/i><\/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\/post\/716902\/\"> https:\/\/habr.com\/ru\/post\/716902\/<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/webt\/w1\/br\/oi\/w1broiflq1cuye8nzwg4ptlaeoc.jpeg\" data-src=\"https:\/\/habrastorage.org\/webt\/w1\/br\/oi\/w1broiflq1cuye8nzwg4ptlaeoc.jpeg\" data-blurred=\"true\"\/><\/div>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441 \u043d\u0443\u043b\u044f GPT \u0432\u0441\u0435\u0433\u043e \u0432 <a href=\"https:\/\/github.com\/jaymody\/picoGPT\/blob\/29e78cc52b58ed2c1c483ffea2eb46ff6bdec785\/gpt2_pico.py#L3-L58\" rel=\"nofollow noopener noreferrer\"><code>60 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 numpy<\/code><\/a>. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0432 \u043d\u0430\u0448\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 OpenAI \u0432\u0435\u0441\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 GPT-2 \u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u043a\u0441\u0442.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345503","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345503","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=345503"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345503\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}