{"id":459524,"date":"2025-05-13T21:00:17","date_gmt":"2025-05-13T21:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=459524"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=459524","title":{"rendered":"<span>\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439: \u0447. 2, \u043e\u0442 ChatGPT \u0434\u043e \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0435\u0436\u0438\u043c\u0430<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eb2\/e34\/5e1\/eb2e345e188adf831492ef34025f55fb.jpg\" width=\"1536\" height=\"1024\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/eb2\/e34\/5e1\/eb2e345e188adf831492ef34025f55fb.jpg 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eb2\/e34\/5e1\/eb2e345e188adf831492ef34025f55fb.jpg 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p><strong>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u00ab\u0425\u0430\u0431\u0440\u00bb!<\/strong><\/p>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043e\u0442 \u0440\u043e\u0431\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432 \u041c\u0430\u0440\u043a\u043e\u0432\u0430 \u0434\u043e \u0434\u043e\u043b\u0433\u043e\u0439 \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0440\u043e\u0447\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c, \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u043b\u0435\u0442 \u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0438 \u0434\u0430\u0436\u0435 \u0441\u043f\u043e\u0440\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<p><strong>\u041f\u0440\u0438\u0441\u0442\u0435\u0433\u043d\u0438\u0442\u0435 \u0442\u043e\u043a\u0435\u043d\u044b \u2014 \u0432\u0445\u043e\u0434 \u0432 \u0437\u043e\u043d\u0443 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0439!<\/strong><\/p>\n<hr\/>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/bothub\/articles\/909092\/\">\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439: \u0447. 1, \u043e\u0442 \u0446\u0435\u043f\u0435\u0439 \u041c\u0430\u0440\u043a\u043e\u0432\u0430 \u0434\u043e ELIZA\u00a0<\/a><\/p>\n<p><strong>\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439: \u0447. 2, \u043e\u0442 ChatGPT \u0434\u043e \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0435\u0436\u0438\u043c\u0430<\/strong> \u2190 \u0432\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0435\u0441\u044c \u0442\u0443\u0442.<\/p>\n<hr\/>\n<h2>2000-\u0435 \u0433\u043e\u0434\u044b: \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0438<\/h2>\n<p>\u0412\u0430\u0436\u043d\u044b\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435\u043c \u0432 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432 2000-\u0445 \u0433\u043e\u0434\u0430\u0445 \u0441\u0442\u0430\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D1%82%D0%B0%D1%82%D0%B8%D1%81%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B8%D0%B9_%D0%BC%D0%B0%D1%88%D0%B8%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BF%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%D0%B4\">\u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u0432<\/a>, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a Google Translate. \u041e\u043d\u0438 \u043d\u0430\u0447\u0430\u043b\u0438 \u0448\u0438\u0440\u043e\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438. \u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0440\u044b\u0432 \u043e\u0442\u043a\u0440\u044b\u043b \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043b\u044e\u0434\u0435\u0439 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d \u0438 \u043a\u0443\u043b\u044c\u0442\u0443\u0440, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u0432 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0441\u0442\u044c NLP-\u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.\u00a0<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u0432 \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0437\u044b\u043a\u0430\u043c\u0438.<\/p>\n<hr\/>\n<h2>2013 \u0433\u043e\u0434: word2vec<\/h2>\n<p><a href=\"https:\/\/ru.wikipedia.org\/wiki\/Word2vec\"><strong>Word2vec<\/strong><\/a><strong> <\/strong>&#8212; \u043e\u0431\u0449\u0435\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u0432 \u043d\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041c\u043e\u0434\u0435\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0430\u043d\u0430\u043b\u0438\u0437 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438 (\u0440\u0430\u0437\u0434\u0435\u043b \u043b\u0438\u043d\u0433\u0432\u0438\u0441\u0442\u0438\u043a\u0438, \u0438\u0437\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u0441\u043c\u044b\u0441\u043b\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0435\u0434\u0438\u043d\u0438\u0446 \u044f\u0437\u044b\u043a\u0430) \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0434\u0438\u0441\u0442\u0440\u0438\u0431\u0443\u0442\u0438\u0432\u043d\u043e\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435, \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u043b\u043e\u0432.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6f8\/cea\/91a\/6f8cea91ae6fbe19da1fb4b1fb8d7d27.png\" alt=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 word2vec: \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0432, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u0421\u043f\u0440\u0430\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u043b\u043e\u0432\u0430, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u00abfrance\u00bb, \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430  \" title=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 word2vec: \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0432, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u0421\u043f\u0440\u0430\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u043b\u043e\u0432\u0430, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u00abfrance\u00bb, \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430  \" width=\"957\" height=\"545\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/6f8\/cea\/91a\/6f8cea91ae6fbe19da1fb4b1fb8d7d27.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6f8\/cea\/91a\/6f8cea91ae6fbe19da1fb4b1fb8d7d27.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 word2vec: \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0432, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u0421\u043f\u0440\u0430\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u043b\u043e\u0432\u0430, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u00abfrance\u00bb, \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430  <\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c word2vec \u0431\u044b\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u0433\u0440\u0443\u043f\u043f\u043e\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 Google \u0432 <strong>2013 \u0433\u043e\u0434\u0443<\/strong>. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e-\u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0431\u044b\u043b\u0438 \u0438 \u0440\u0430\u043d\u0435\u0435, \u043e\u0434\u043d\u0430\u043a\u043e word2vec \u0441\u0442\u0430\u043b \u043f\u0435\u0440\u0432\u043e\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439:<strong> \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0437-\u0437\u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b.\u00a0<\/strong><\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c word2vec \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0440\u043f\u0443\u0441 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0442\u0440\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u0438\u0437 \u043a\u043e\u0440\u043f\u0443\u0441\u0430 \u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0441\u0442\u0440\u043e\u0438\u0442 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u041a\u0430\u0436\u0434\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0443 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440 \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 &#8212; \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0443\u0436\u043d\u043e\u0435 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 word2vec \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.nn.functional as F  class SkipGramWithNegSampling(nn.Module):     def __init__(self, vocab_size, embed_size, num_neg_samples=5):         super().__init__()         self.target_embeddings = nn.Embedding(vocab_size, embed_size)  # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432         self.context_embeddings = nn.Embedding(vocab_size, embed_size)  # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0432          # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u0435\u0441\u043e\u0432 (\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u0434\u043b\u044f Word2Vec)         nn.init.xavier_uniform_(self.target_embeddings.weight)         nn.init.xavier_uniform_(self.context_embeddings.weight)          self.num_neg_samples = num_neg_samples  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043d\u0430 1 \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439      def forward(self, target_words, context_words):         \"\"\"         target_words: \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432 (\u0444\u043e\u0440\u043c\u0430 [batch_size])         context_words: \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0432 (\u0444\u043e\u0440\u043c\u0430 [batch_size])         \"\"\"         # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438         target_emb = self.target_embeddings(target_words)  # [batch_size, embed_size]         context_emb = self.context_embeddings(context_words)  # [batch_size, embed_size]          # \u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c score \u0434\u043b\u044f \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0430\u0440         positive_scores = torch.sum(target_emb * context_emb, dim=1)  # [batch_size]          # \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b (\u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)         batch_size = target_words.size(0)         negative_words = torch.randint(0, len(self.target_embeddings.weight),                                      (batch_size, self.num_neg_samples))  # [batch_size, num_neg]         negative_emb = self.context_embeddings(negative_words)  # [batch_size, num_neg, embed_size]          # \u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c score \u0434\u043b\u044f \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0430\u0440         negative_scores = torch.bmm(negative_emb, target_emb.unsqueeze(2)).squeeze()  # [batch_size, num_neg]          return positive_scores, negative_scores  # \u0413\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b (\u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430) VOCAB_SIZE = 10000  # \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f EMBED_SIZE = 300     # \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 BATCH_SIZE = 1024    # \u0420\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0442\u0447\u0430 NUM_NEG = 5          # \u041d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043d\u0430 1 \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 model = SkipGramWithNegSampling(VOCAB_SIZE, EMBED_SIZE, NUM_NEG)  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043e\u0431\u044b\u0447\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u0443) # \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u0430\u0442\u0447 \u043f\u0430\u0440 (\u0446\u0435\u043b\u0435\u0432\u043e\u0435_\u0441\u043b\u043e\u0432\u043e, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0435_\u0441\u043b\u043e\u0432\u043e) target_indices = torch.randint(0, VOCAB_SIZE, (BATCH_SIZE,))  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b context_indices = torch.randint(0, VOCAB_SIZE, (BATCH_SIZE,))  # \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 - \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430  # \u041f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 pos_scores, neg_scores = model(target_indices, context_indices)  # \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u0435\u0440\u044c (Max-margin loss) positive_loss = F.logsigmoid(pos_scores).mean() negative_loss = F.logsigmoid(-neg_scores).mean() loss = -(positive_loss + negative_loss)  # \u0423\u0441\u0440\u0435\u0434\u043d\u0435\u043d\u0438\u0435 \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u0432 negative_loss  # \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 (\u043e\u0434\u0438\u043d \u0448\u0430\u0433) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) optimizer.zero_grad() loss.backward() optimizer.step()<\/code><\/pre>\n<hr\/>\n<h2>2017 \u0433\u043e\u0434: \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440<\/h2>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/486358\/\"><strong>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440<\/strong><\/a><strong> <\/strong>&#8212; \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0432 2017 \u0433\u043e\u0434\u0443 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0438\u0437 Google Brain.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/688\/87a\/46c\/68887a46ce1bb6003e0a70b944033edd.png\" alt=\"\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0430\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u043b\u0435\u0432\u0430 \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043b\u043e\u0438. \u0421\u043f\u0440\u0430\u0432\u0430 \u2014 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a, \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0440\u043e\u0441\u0441-\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0432\u044b\u0445\u043e\u0434\u0430\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430  \" title=\"\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0430\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u043b\u0435\u0432\u0430 \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043b\u043e\u0438. \u0421\u043f\u0440\u0430\u0432\u0430 \u2014 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a, \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0440\u043e\u0441\u0441-\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0432\u044b\u0445\u043e\u0434\u0430\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430  \" width=\"1280\" height=\"1015\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/688\/87a\/46c\/68887a46ce1bb6003e0a70b944033edd.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/688\/87a\/46c\/68887a46ce1bb6003e0a70b944033edd.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0430\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u043b\u0435\u0432\u0430 \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043b\u043e\u0438. \u0421\u043f\u0440\u0430\u0432\u0430 \u2014 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a, \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0440\u043e\u0441\u0441-\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0432\u044b\u0445\u043e\u0434\u0430\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430  <\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u044b\u043c\u0438 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0442\u0435\u043a\u0441\u0442 \u043d\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435, \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0432\u0440\u043e\u0434\u0435 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0435\u0444\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u043d\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u0442\u0435\u043a\u0441\u0442, \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0430 \u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0437\u0430\u0438\u043c\u043e\u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0438.<\/p>\n<p>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430. \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d0b\/c2b\/23a\/d0bc2b23a92572730390adce7b3725b1.png\" alt=\"\u041a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439\" title=\"\u041a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439\" width=\"675\" height=\"1600\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/d0b\/c2b\/23a\/d0bc2b23a92572730390adce7b3725b1.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d0b\/c2b\/23a\/d0bc2b23a92572730390adce7b3725b1.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430: \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0445\u043e\u0434 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f; \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043a \u0432\u044b\u0445\u043e\u0434\u0443 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0442\u0430\u043a \u0438 \u0432\u044b\u0445\u043e\u0434 \u043a\u043e\u0434\u0435\u0440\u0430; \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0449\u0443\u044e \u043d\u0430 \u0432\u0445\u043e\u0434 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c67\/1ce\/94b\/c671ce94b913bfa3aa841180c46b5ae2.png\" alt=\"\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439\" title=\"\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439\" width=\"744\" height=\"1600\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/c67\/1ce\/94b\/c671ce94b913bfa3aa841180c46b5ae2.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c67\/1ce\/94b\/c671ce94b913bfa3aa841180c46b5ae2.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<strong> \u043c\u043d\u043e\u0433\u043e\u0433\u043e\u043b\u043e\u0432\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 (multi-head attention, MHA)<\/strong>, \u0433\u0434\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438. \u0422\u043e \u0435\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u0432\u0430\u0436\u043d\u044b \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0441 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0437\u0440\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b, \u043a\u0430\u043a \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439, \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043d\u0430 Python. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.nn.functional as F  class TransformerEncoderLayer(nn.Module):     \"\"\"     \u041e\u0434\u0438\u043d \u0441\u043b\u043e\u0439 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 (Post-LN \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0438\u0437 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438).      \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b:         d_model (int): \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 512)         nhead (int): \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u043b\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 8)         dim_feedforward (int): \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c FFN (\u043e\u0431\u044b\u0447\u043d\u043e 4*d_model)         dropout (float): \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0440\u043e\u043f\u0430\u0443\u0442\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 0.1)     \"\"\"     def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1):         super().__init__()          # 1. \u0421\u043b\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0433\u043e\u043b\u043e\u0432\u043e\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f         self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)          # 2. \u0414\u0432\u0443\u0445\u0441\u043b\u043e\u0439\u043d\u0430\u044f FFN \u0441\u0435\u0442\u044c         self.linear1 = nn.Linear(d_model, dim_feedforward)         self.linear2 = nn.Linear(dim_feedforward, d_model)          # 3. \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0434\u0440\u043e\u043f\u0430\u0443\u0442\u044b         self.norm1 = nn.LayerNorm(d_model)         self.norm2 = nn.LayerNorm(d_model)         self.dropout = nn.Dropout(dropout)         self.dropout1 = nn.Dropout(dropout)         self.dropout2 = nn.Dropout(dropout)          # 4. \u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0434\u043b\u044f FFN (\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435 ReLU)         self.activation = F.relu      def forward(self, src, src_mask=None, src_key_padding_mask=None):         \"\"\"         \u0412\u0445\u043e\u0434:             src: (S, B, D) - \u0432\u0445\u043e\u0434\u043d\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c (\u0434\u043b\u0438\u043d\u0430, \u0431\u0430\u0442\u0447, \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c)             src_mask: (S, S) - \u043c\u0430\u0441\u043a\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e)             src_key_padding_mask: (B, S) - \u043c\u0430\u0441\u043a\u0430 \u0431\u0430\u0442\u0447\u0430 (\u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e)         \"\"\"         # \u0428\u0430\u0433 1: \u0421\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441 residual connection         attn_output, _ = self.self_attn(             src, src, src,             attn_mask=src_mask,             key_padding_mask=src_key_padding_mask         )         src = src + self.dropout1(attn_output)         src = self.norm1(src)  # Post-LN: \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u041f\u041e\u0421\u041b\u0415 residual          # \u0428\u0430\u0433 2: FFN \u0441 residual connection         ff_output = self.linear1(src)         ff_output = self.activation(ff_output)         ff_output = self.dropout(ff_output)         ff_output = self.linear2(ff_output)         src = src + self.dropout2(ff_output)         src = self.norm2(src)          return src  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u0431\u0435\u0437 \u0432\u044b\u0432\u043e\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0445) if __name__ == \"__main__\":     # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0434\u0435\u043b\u0438     d_model = 512    # \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432     nhead = 8        # 8 \u0433\u043e\u043b\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f     seq_len = 100    # \u0414\u043b\u0438\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438     batch_size = 32  # \u0420\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0442\u0447\u0430      # \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043b\u043e\u0439 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430     encoder_layer = TransformerEncoderLayer(d_model, nhead)      # \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:     # (S, B, D) = (\u0434\u043b\u0438\u043d\u0430_\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0431\u0430\u0442\u0447, \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c)     test_input = torch.rand(seq_len, batch_size, d_model)      # \u0412\u044b\u0437\u043e\u0432 \u0441\u043b\u043e\u044f (\u0431\u0435\u0437 \u043c\u0430\u0441\u043e\u043a)     output = encoder_layer(test_input)      # \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d:     # 1. \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u044d\u043d\u043a\u043e\u0434\u0438\u043d\u0433\u0438 \u043a test_input     # 2. \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0451\u0432 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430     # 3. \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438<\/code><\/pre>\n<hr\/>\n<h2>\u0422\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/599673\/\"><strong>\u0422\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/a><strong> <\/strong>&#8212; \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0435 \u0435\u0434\u0438\u043d\u0438\u0446\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 \u0441\u043b\u043e\u0432\u0430, \u0447\u0430\u0441\u0442\u0438 \u0441\u043b\u043e\u0432\u0430 \u0438\u043b\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4b8\/1a5\/a86\/4b81a5a863f2b245fa786a38e391bfe1.png\" alt=\"\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438\" title=\"\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438\" width=\"1140\" height=\"447\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/4b8\/1a5\/a86\/4b81a5a863f2b245fa786a38e391bfe1.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4b8\/1a5\/a86\/4b81a5a863f2b245fa786a38e391bfe1.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0418\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u0438 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0430 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c: \u0441\u043b\u043e\u0432\u0430, \u043f\u043e\u0434\u0441\u043b\u043e\u0432\u0430, \u0437\u043d\u0430\u043a\u0438 \u043f\u0440\u0435\u043f\u0438\u043d\u0430\u043d\u0438\u044f, \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043e\u0442\u043c\u0435\u0442\u043a\u0438(\u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043c\u0430\u0440\u043a\u0435\u0440\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0438 \u043c\u0430\u0440\u043a\u0435\u0440\u044b \u0434\u043b\u044f \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0432).<\/p>\n<p>\u0422\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u044f <strong>\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443<\/strong>.<br \/>\u041c\u043e\u0434\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u043b\u043e\u0432\u0430 \u0438\u043b\u0438 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u0431\u0443\u043a\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u0430\u043c. \u041e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435 \u0444\u0440\u0430\u0437\u0430, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u2014 \u0432\u0441\u0451 \u0438\u0437-\u0437\u0430 \u043e\u0442\u043b\u0438\u0447\u0438\u0439 \u0432 \u0441\u043f\u043e\u0441\u043e\u0431\u0435 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<hr\/>\n<h2>2018 \u0433\u043e\u0434: BERT<\/h2>\n<p>\u0412 \u043e\u043a\u0442\u044f\u0431\u0440\u0435 2018 \u0433\u043e\u0434\u0430 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 Google \u0431\u044b\u043b\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u044f\u0437\u044b\u043a\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c <a href=\"https:\/\/habr.com\/ru\/companies\/skillfactory\/articles\/862130\/\"><strong>BERT<\/strong><\/a><strong> <\/strong>&#8212; \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/035\/d7b\/bfd\/035d7bbfd8f6a2c1425369a472e9d480.png\" alt=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 BERT. \u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0435\u043a\u0441\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u0430\u0441\u043a\u0438\u0440\u0443\u0435\u0442 15% \u0442\u043e\u043a\u0435\u043d\u043e\u0432. \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043b\u043e\u0432\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0412\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c (FFNN) \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 softmax \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u043e\u0432\" title=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 BERT. \u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0435\u043a\u0441\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u0430\u0441\u043a\u0438\u0440\u0443\u0435\u0442 15% \u0442\u043e\u043a\u0435\u043d\u043e\u0432. \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043b\u043e\u0432\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0412\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c (FFNN) \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 softmax \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u043e\u0432\" width=\"1325\" height=\"925\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/035\/d7b\/bfd\/035d7bbfd8f6a2c1425369a472e9d480.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/035\/d7b\/bfd\/035d7bbfd8f6a2c1425369a472e9d480.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 BERT. \u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u0435\u043a\u0441\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u0430\u0441\u043a\u0438\u0440\u0443\u0435\u0442 15% \u0442\u043e\u043a\u0435\u043d\u043e\u0432. \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043b\u043e\u0432\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0412\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c (FFNN) \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 softmax \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u043e\u0432<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430 BERT<\/strong> &#8212; \u043f\u043e\u043c\u043e\u0447\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442.\u00a0<\/p>\n<p><strong>\u041e\u0434\u043d\u0430 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 BERT<\/strong> &#8212; \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u044c. \u0421\u043b\u043e\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0451 \u0442\u0430\u043a \u0436\u0435 \u0432 \u043e\u0434\u043d\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0441\u043b\u0435\u0432\u0430 (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0441\u043b\u0435\u0432\u0430, \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u0442\u0435\u043a\u0441\u0442\u0430), \u043d\u043e \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u0440\u0430\u0432\u0430 (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0442\u0435\u043a\u0441\u0442\u0430).\u00a0<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn  class BERT(nn.Module):     \"\"\"     \u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b BERT \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u0435\u0439.      \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438:     - \u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b \u0441 \u043f\u0440\u0435\u0434\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 (\u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e PyTorch Transformer)     - \u0421\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430     - \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0434\u0432\u0443\u0445 \u0437\u0430\u0434\u0430\u0447: Masked Language Model (MLM) \u0438 Next Sentence Prediction (NSP)      Args:         vocab_size (int): \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f         d_model (int): \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0441\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0441\u043b\u043e\u044f (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 768)         nhead (int): \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u043b\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 12)         dim_feedforward (int): \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c FFN \u0441\u043b\u043e\u044f (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 3072)         n_layers (int): \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0451\u0432 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 12)     \"\"\"     def __init__(self, vocab_size, d_model=768, nhead=12, dim_feedforward=3072, n_layers=12):         super().__init__()          # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 (\u0442\u043e\u043a\u0435\u043d\u044b + \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b + \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435)         self.embedding = BERTEmbedding(vocab_size, d_model)          # \u0421\u043b\u043e\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0441 \u043f\u0440\u0435\u0434\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439         self.encoder_layers = nn.ModuleList([             PreNormTransformerLayer(d_model, nhead, dim_feedforward)             for _ in range(n_layers)         ])          # \u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 NSP (Next Sentence Prediction)         self.nsp_classifier = nn.Sequential(             nn.Linear(d_model, d_model),             nn.Tanh(),             nn.Linear(d_model, 2)         )          # \u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 MLM \u0441 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u044b\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438         self.mlm_decoder = nn.Linear(d_model, vocab_size)         self.mlm_decoder.weight = self.embedding.token_embed.weight  # Weight tying         self.mlm_bias = nn.Parameter(torch.zeros(vocab_size))      def forward(self, input_ids, segment_ids, attention_mask=None):         \"\"\"         Args:             input_ids (Tensor): \u0418\u043d\u0434\u0435\u043a\u0441\u044b \u0442\u043e\u043a\u0435\u043d\u043e\u0432 [batch_size, seq_len]             segment_ids (Tensor): \u0418\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432 [batch_size, seq_len]             attention_mask (Tensor): \u041c\u0430\u0441\u043a\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f [batch_size, seq_len]          Returns:             tuple: (\u043b\u043e\u0433\u0438\u0442\u044b MLM, \u043b\u043e\u0433\u0438\u0442\u044b NSP) \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e NSP \u043b\u043e\u0433\u0438\u0442\u044b         \"\"\"         # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435         x = self.embedding(input_ids, segment_ids)         for layer in self.encoder_layers:             x = layer(x, attention_mask)          # \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u0442\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0435\u0438\u0445 \u0437\u0430\u0434\u0430\u0447         nsp_logits = self.nsp_classifier(x[:, 0])  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c [CLS] \u0442\u043e\u043a\u0435\u043d          # \u0414\u043b\u044f MLM \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043b\u043e\u0433\u0438\u0442\u044b \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439         mlm_logits = self.mlm_decoder(x) + self.mlm_bias          return mlm_logits, nsp_logits  class PreNormTransformerLayer(nn.Module):     \"\"\"\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0441\u043b\u043e\u0439 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0441 \u043f\u0440\u0435\u0434\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 (\u043a\u0430\u043a \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c BERT)\"\"\"     def __init__(self, d_model, nhead, dim_feedforward):         super().__init__()         self.norm1 = nn.LayerNorm(d_model)         self.self_attn = nn.MultiheadAttention(d_model, nhead)         self.norm2 = nn.LayerNorm(d_model)         self.ffn = nn.Sequential(             nn.Linear(d_model, dim_feedforward),             nn.GELU(),             nn.Linear(dim_feedforward, d_model)         )      def forward(self, src, mask=None):         # Self-attention \u0441 \u043f\u0440\u0435\u0434\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439         src = self.norm1(src)         src = src + self.self_attn(src, src, src, key_padding_mask=mask)[0]          # FFN \u0441 \u043f\u0440\u0435\u0434\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439         src = self.norm2(src)         src = src + self.ffn(src)         return src  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 if __name__ == \"__main__\":     # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0435 BERT-base     bert = BERT(vocab_size=30000)      # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 (batch_size=2, seq_len=512)     input_ids = torch.randint(0, 30000, (2, 512))     segment_ids = torch.cat([torch.zeros(2, 256), torch.ones(2, 256)], dim=1)     attention_mask = torch.ones(2, 512)  # \u0420\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u0441\u043a\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 0\/1      # \u041f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434     mlm_logits, nsp_logits = bert(input_ids, segment_ids, attention_mask)      # \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435:     # - MLM \u043b\u043e\u0433\u0438\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432     # - NSP \u043b\u043e\u0433\u0438\u0442\u044b \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438<\/code><\/pre>\n<hr\/>\n<h2>2018\u20132025 \u0433\u043e\u0434\u044b: ChatGPT<\/h2>\n<p>\u0412 2018 \u0433\u043e\u0434\u0443 \u043c\u0438\u0440 \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0441\u044f \u0441 <a href=\"https:\/\/habr.com\/ru\/companies\/bothub\/articles\/887988\/\"><strong>GPT-1<\/strong><\/a> \u2014 \u0441\u043a\u0440\u043e\u043c\u043d\u043e\u0439 \u043f\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043c\u0435\u0440\u043a\u0430\u043c, \u043d\u043e \u0440\u0435\u0432\u043e\u043b\u044e\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u043e\u0442 OpenAI. \u041e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u043e\u0432, \u043e\u043d\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043b\u0430 117 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u043e\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438 \u043c\u043e\u0433\u043b\u0430 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0442\u0435\u043a\u0441\u0442\u044b, \u0445\u043e\u0442\u044f \u0435\u0451 \u043e\u0434\u043d\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0441\u0442\u044c (\u0430\u043d\u0430\u043b\u0438\u0437 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e) \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0432 512 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043d\u0438\u0436\u0430\u043b\u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430. \u041c\u043e\u0434\u0435\u043b\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0430 \u0441\u043b\u0430\u0431\u0443\u044e \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u0432 \u0434\u0438\u0430\u043b\u043e\u0433\u0430\u0445.<\/p>\n<p>\u0412\u0441\u0435\u0433\u043e \u0433\u043e\u0434 \u0441\u043f\u0443\u0441\u0442\u044f OpenAI \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043b\u0430 <strong>GPT-2<\/strong>, \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043a\u0430\u0447\u043e\u043a \u0432 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438. \u041d\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0441 1,5 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043a <strong>zero-shot learning<\/strong> \u2014 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u0447 \u0431\u0435\u0437 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445. \u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043e 1024 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b GPT-2 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0435 \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0442\u0435\u043a\u0441\u0442\u044b, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043d\u043e\u0432\u043e\u0441\u0442\u043d\u044b\u0435 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 \u0438 \u043b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u043d\u044b\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0441\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u041d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u043f\u0440\u043e\u0440\u044b\u0432 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u0451\u043b \u0432 2020 \u0433\u043e\u0434\u0443 \u0441 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c GPT-3. \u0415\u0451 175 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043e\u0442\u043a\u0440\u044b\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a few-shot learning (\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 1\u20133 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445) \u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u042d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u043b\u043e \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e ChatGPT \u2014 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043b\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0418\u0418. \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441, \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u043b\u0430 \u0433\u0430\u043b\u043b\u044e\u0446\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0438\u0441\u043f\u044b\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0446\u0435\u043f\u043e\u0447\u0435\u043a.<\/p>\n<p>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u044d\u0442\u0430\u043f \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d <strong>GPT-4 <\/strong>(2023 \u0433\u043e\u0434), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u043b\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e, \u043d\u043e \u0438 \u043d\u0430\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439. \u0421\u043d\u0438\u0436\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u043d\u0430 40%, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a\u0438 \u0438 \u0441\u0445\u0435\u043c\u044b, \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u0432\u0435\u0442\u044b \u2014 \u0432\u0441\u0451 \u0438\u0437 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0443\u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0441\u043b\u043e\u0432\u043e\u043c \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0441\u0442\u0430\u043b\u0438<strong> \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u044e\u0449\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438<\/strong> (<strong>large reasoning models, LRM)<\/strong>, \u043d\u0430\u0447\u0430\u0432\u0448\u0438\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0435 2024 \u0433\u043e\u0434\u0430. \u042d\u0442\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u0441\u0440\u0435\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u2014 \u043b\u0438\u043d\u0435\u0439\u043a\u0430 o1\u2013o4 \u043e\u0442 OpenAI, DeepSeek R1, Claude 3.7 Sonnet Thinking \u0438 Grok 3, \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043a \u0434\u0435\u0434\u0443\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f\u043c. \u041e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0432\u043d\u0435\u0448\u043d\u044e\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u043d\u044b\u0435 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u2014 \u0432\u043f\u043b\u043e\u0442\u044c \u0434\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0433\u0438\u043f\u043e\u0442\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f. \u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043d\u0430\u043b\u0438\u0437 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0439 \u0441 \u0432\u044b\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0445 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0439.\u00a0<\/p>\n<p>\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430 \u0441\u0430\u0439\u0442\u0430\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438\u043b\u0438 \u043d\u0430 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430\u0445 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 BotHub. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0439\u0441\u044f \u043f\u043e <a href=\"https:\/\/bothub.chat\/?invitedBy=m_aGCkuyTgqllHCK0dUc7\">\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0435<\/a> \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c 100 000 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043b\u044e\u0431\u044b\u043c \u043c\u043e\u0434\u0435\u043b\u044f\u043c (\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u0412\u041f\u041d).<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c51\/973\/8ed\/c519738ed57f409d1f684fc05452c619.png\" alt=\"\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u0438 GPT \u0438 LRM\" title=\"\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u0438 GPT \u0438 LRM\" width=\"602\" height=\"740\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/c51\/973\/8ed\/c519738ed57f409d1f684fc05452c619.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c51\/973\/8ed\/c519738ed57f409d1f684fc05452c619.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u0438 GPT \u0438 LRM<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 GPT \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn  class GPTBlock(nn.Module):     \"\"\"\u041e\u0434\u0438\u043d \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440-\u0431\u043b\u043e\u043a GPT (\u0434\u0435\u043a\u043e\u0434\u0435\u0440) \u0441 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f\"\"\"     def __init__(self, d_model, nhead, dim_feedforward):         super().__init__()         # \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0434 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c (Pre-LN) \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f         self.ln1 = nn.LayerNorm(d_model)         # \u041c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043c\u043d\u043e\u0433\u043e\u0433\u043e\u043b\u043e\u0432\u043e\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f         self.attn = nn.MultiheadAttention(d_model, nhead, batch_first=True)          self.ln2 = nn.LayerNorm(d_model)         # MLP \u0441 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 (\u0442\u0438\u043f\u0438\u0447\u043d\u043e \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u043e\u0432)         self.mlp = nn.Sequential(             nn.Linear(d_model, dim_feedforward),             nn.GELU(),  # \u0410\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 GPT \u0432\u043c\u0435\u0441\u0442\u043e ReLU             nn.Linear(dim_feedforward, d_model)         )      def forward(self, x, mask=None):         # \u041e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 + \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435         attn_output, _ = self.attn(             self.ln1(x),  # \u0417\u0430\u043f\u0440\u043e\u0441             self.ln1(x),  # \u041a\u043b\u044e\u0447             self.ln1(x),  # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435             attn_mask=mask  # \u041c\u0430\u0441\u043a\u0430 \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432         )         x = x + attn_output          # \u041e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 + MLP         x = x + self.mlp(self.ln2(x))         return x  class GPT(nn.Module):     \"\"\"\u0423\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f GPT-like-\u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b\"\"\"     def __init__(self, vocab_size, d_model=768, nhead=12,                  dim_feedforward=3072, n_layers=12):         super().__init__()          # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u0439         self.token_embed = nn.Embedding(vocab_size, d_model)         self.pos_embed = nn.Parameter(torch.zeros(1, 1024, d_model))  # \u041e\u0431\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438          # \u0421\u0442\u0435\u043a \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440-\u0431\u043b\u043e\u043a\u043e\u0432         self.blocks = nn.ModuleList([             GPTBlock(d_model, nhead, dim_feedforward)             for _ in range(n_layers)         ])          # \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043b\u043e\u0438         self.ln_f = nn.LayerNorm(d_model)         self.head = nn.Linear(d_model, vocab_size)  # \u041b\u043e\u0433\u0438\u0442\u044b \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430      def forward(self, x):         # x: (batch_size, sequence_length)         batch_size, seq_len = x.size()          # \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438         tok_emb = self.token_embed(x)  # (B, T, D)         pos_emb = self.pos_embed[:, :seq_len, :]  # \u041e\u0431\u0440\u0435\u0437\u0430\u0435\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u043f\u043e \u0434\u043b\u0438\u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438         x = tok_emb + pos_emb          # \u041c\u0430\u0441\u043a\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430 (\u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u0435\u0442 looking into the future)         mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1).bool().to(x.device)          # \u041f\u0440\u043e\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u0432\u0441\u0435 \u0431\u043b\u043e\u043a\u0438         for block in self.blocks:             x = block(x, mask)          # \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435         x = self.ln_f(x)         return self.head(x)  # (B, T, vocab_size)  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f if __name__ == \"__main__\":     # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u043a \u0432 GPT-2 Small (\u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430)     vocab_size = 50257  # \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f GPT-2     model = GPT(         vocab_size=vocab_size,         d_model=768,         nhead=12,         dim_feedforward=3072,         n_layers=12     )      # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445: \u0431\u0430\u0442\u0447 \u0438\u0437 2 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043f\u043e 16 \u0442\u043e\u043a\u0435\u043d\u043e\u0432     input_ids = torch.randint(0, vocab_size, (2, 16))      # \u041f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 (\u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435)     logits = model(input_ids)  # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043b\u043e\u0433\u0438\u0442\u044b \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430      # \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0434\u0435\u0441\u044c \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438:     # 1) \u0412\u044b\u0431\u043e\u0440\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, top-k sampling)     # 2) \u0418\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e (autoregressive loop)     # 3) \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 softmax \u043a \u043b\u043e\u0433\u0438\u0442\u0430\u043c<\/code><\/pre>\n<hr\/>\n<h2>2020 \u0433\u043e\u0434: \u04225<\/h2>\n<p><a href=\"https:\/\/arxiv.org\/abs\/1910.10683\"><strong>T5 (Text-to-Text Transfer Transformer)<\/strong><\/a> &#8212; \u044f\u0437\u044b\u043a\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u0430\u044f \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 Google \u0432 2020 \u0433\u043e\u0434\u0443. T5 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u043c \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u0438 BERT \u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0448\u0438\u0440\u043e\u043a\u043e\u0433\u043e \u0441\u043f\u0435\u043a\u0442\u0440\u0430 \u0437\u0430\u0434\u0430\u0447 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430.<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/434\/524\/f29\/434524f2993b09a8a80f33c764ece53f.png\" width=\"1600\" height=\"605\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/434\/524\/f29\/434524f2993b09a8a80f33c764ece53f.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/434\/524\/f29\/434524f2993b09a8a80f33c764ece53f.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u041f\u043e\u0434\u043e\u0431\u043d\u043e GPT, \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 T5 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0435\u0434\u0438\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430, \u0441\u0432\u043e\u0434\u044f \u0438\u0445 \u043a \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0444\u043e\u0440\u043c\u0430\u0442\u0443 \u00ab\u0442\u0435\u043a\u0441\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u2014 \u0442\u0435\u043a\u0441\u0442 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435\u00bb. \u0422\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c, \u0441\u0443\u043c\u043c\u0430\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0438 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 NLP-\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430.<\/p>\n<p><strong>\u04225 <\/strong>\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0432 \u043e\u0431\u043e\u0438\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0435\u0439 \u0444\u043e\u043a\u0443\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u0435.\u00a0<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 T5:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn  class PositionalEncoding(nn.Module):     \"\"\"\u0423\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 (\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435 T5 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438)\"\"\"     def __init__(self, d_model, max_len=512):         super().__init__()         self.pe = nn.Embedding(max_len, d_model)      def forward(self, x):         positions = torch.arange(x.size(1), device=x.device).unsqueeze(0)         return x + self.pe(positions)  class T5EncoderBlock(nn.Module):     \"\"\"\u0411\u043b\u043e\u043a \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 T5 (\u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435\u043c cross-attention)\"\"\"     def __init__(self, d_model, nhead, dim_feedforward):         super().__init__()         self.self_attn = nn.MultiheadAttention(d_model, nhead)         self.ffn = nn.Sequential(             nn.Linear(d_model, dim_feedforward),             nn.ReLU(),             nn.Linear(dim_feedforward, d_model)         )         self.norm1 = nn.LayerNorm(d_model)         self.norm2 = nn.LayerNorm(d_model)      def forward(self, x, mask=None):         # Self-attention \u0441 residual connection         attn_output = self.self_attn(x, x, x, attn_mask=mask)[0]         x = self.norm1(x + attn_output)          # Feed Forward Network         ffn_output = self.ffn(x)         return self.norm2(x + ffn_output)  class T5DecoderBlock(nn.Module):     \"\"\"\u0411\u043b\u043e\u043a \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430 T5 \u0441 self-attention, cross-attention \u0438 FFN\"\"\"     def __init__(self, d_model, nhead, dim_feedforward):         super().__init__()         self.self_attn = nn.MultiheadAttention(d_model, nhead)         self.cross_attn = nn.MultiheadAttention(d_model, nhead)         self.ffn = nn.Sequential(             nn.Linear(d_model, dim_feedforward),             nn.ReLU(),             nn.Linear(dim_feedforward, d_model)         )         self.norm1 = nn.LayerNorm(d_model)         self.norm2 = nn.LayerNorm(d_model)         self.norm3 = nn.LayerNorm(d_model)      def forward(self, x, encoder_output, src_mask=None, tgt_mask=None):         # Self-attention (\u043c\u0430\u0441\u043a\u0430 tgt_mask \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0443\u0442\u0435\u0447\u043a\u0443 \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e)         attn_output = self.self_attn(x, x, x, attn_mask=tgt_mask)[0]         x = self.norm1(x + attn_output)          # Cross-attention \u0441 \u0432\u044b\u0445\u043e\u0434\u043e\u043c \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430         cross_output = self.cross_attn(x, encoder_output, encoder_output, attn_mask=src_mask)[0]         x = self.norm2(x + cross_output)          # Feed Forward Network         ffn_output = self.ffn(x)         return self.norm3(x + ffn_output)  class T5(nn.Module):     \"\"\"\u0423\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f T5 \u0441 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438:     - \u041e\u0431\u0449\u0438\u0435 \u0432\u0435\u0441\u0430 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u044f     - \u041e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430     - \u041f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f     \"\"\"     def __init__(self, vocab_size, d_model=512, nhead=8, dim_feedforward=2048,                  num_encoder_layers=6, num_decoder_layers=6):         super().__init__()          # \u041e\u0431\u0449\u0438\u0435 \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0434\u043b\u044f \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430\/\u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430         self.token_embed = nn.Embedding(vocab_size, d_model)         self.pos_encoder = PositionalEncoding(d_model)          # \u0421\u043b\u043e\u0438 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0438 \u0434\u0435\u043a\u043e\u0434\u0435\u0440\u0430         self.encoder_layers = nn.ModuleList([             T5EncoderBlock(d_model, nhead, dim_feedforward)             for _ in range(num_encoder_layers)         ])         self.decoder_layers = nn.ModuleList([             T5DecoderBlock(d_model, nhead, dim_feedforward)             for _ in range(num_decoder_layers)         ])          # \u0412\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u043e\u0439 \u0441 \u043e\u0431\u0449\u0438\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438         self.generator = nn.Linear(d_model, vocab_size)         self.generator.weight = self.token_embed.weight  # Weight tying      def encode(self, src, src_mask=None):         x = self.token_embed(src)         x = self.pos_encoder(x)         for layer in self.encoder_layers:             x = layer(x, src_mask)         return x      def decode(self, tgt, memory, tgt_mask=None, src_mask=None):         x = self.token_embed(tgt)         x = self.pos_encoder(x)         for layer in self.decoder_layers:             x = layer(x, memory, src_mask, tgt_mask)         return x      def forward(self, src, tgt, src_mask=None, tgt_mask=None):         memory = self.encode(src, src_mask)         output = self.decode(tgt, memory, tgt_mask, src_mask)         return self.generator(output)  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 if __name__ == \"__main__\":     # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438     vocab_size = 32000  # \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f T5     batch_size = 2     seq_len = 32      # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438     model = T5(         vocab_size=vocab_size,         d_model=256,  # \u0423\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u043e \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438         nhead=4,         dim_feedforward=512,         num_encoder_layers=3,         num_decoder_layers=3     )      # \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445     src = torch.randint(0, vocab_size, (batch_size, seq_len))  # \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b     tgt = torch.randint(0, vocab_size, (batch_size, seq_len))  # \u0426\u0435\u043b\u0435\u0432\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b      # \u041f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434     outputs = model(src, tgt)      # outputs.shape = [batch_size, seq_len, vocab_size]<\/code><\/pre>\n<hr\/>\n<h2>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438<\/h2>\n<p>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u043d\u0435\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0430\u0440\u0441\u0435\u043d\u0430\u043b\u0443 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0445 \u0442\u0435\u0445\u043d\u0438\u043a \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.\u00a0<\/p>\n<p>\u041e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0439 \u0441\u0442\u0430\u043b\u043e <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Generative_pre-trained_transformer\"><strong>\u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 (pre-training)<\/strong><\/a>, \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0440\u0430\u0437\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0441\u0432\u0430\u0438\u0432\u0430\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044f \u0437\u043d\u0430\u043d\u0438\u044f \u043e \u043c\u0438\u0440\u0435. \u042d\u0442\u0430\u043f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u043b\u043e\u0441\u0441\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u0443 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0430\u0434\u0430\u043f\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u044d\u0442\u0430\u043f<strong> <\/strong><a href=\"https:\/\/habr.com\/ru\/companies\/raft\/articles\/785616\/\"><strong>\u0434\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f (fine-tuning)<\/strong><\/a>, \u0433\u0434\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0439 \u0447\u0430\u0442-\u0431\u043e\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0431\u0449\u0438\u0435 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0435 \u043d\u0430\u0432\u044b\u043a\u0438 \u043f\u0440\u0438 \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0434\u0438\u0446\u0438\u043d\u0441\u043a\u0438\u0445 \u0442\u0435\u043a\u0441\u0442\u0430\u0445 \u0438 \u0434\u0438\u0430\u043b\u043e\u0433\u0430\u0445. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b \u043c\u0435\u0442\u043e\u0434\u044b <a href=\"https:\/\/habr.com\/ru\/articles\/830102\/\"><strong>\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c (reinforcement learning from human feedback, RLHF)<\/strong><\/a>, \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u0443\u0435\u0442 \u0441\u0432\u043e\u0438 \u043e\u0442\u0432\u0435\u0442\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0435\u043d\u0438\u0439, \u2014 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u043c\u043e\u0433\u043b\u043e \u0441\u0442\u0430\u0442\u044c ChatGPT \u0441\u0442\u043e\u043b\u044c \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0432 \u0434\u0438\u0430\u043b\u043e\u0433\u0430\u0445.<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u044b \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. <a href=\"https:\/\/habr.com\/ru\/companies\/yandex\/articles\/800945\/\"><strong>\u041a\u0432\u0430\u043d\u0442\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/a><strong> <\/strong>\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u043e\u0434\u0435\u043b\u0438 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441 32-\u0431\u0438\u0442\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0434\u043e 8-\u0431\u0438\u0442\u043d\u044b\u0445), \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u043e\u0433\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430. <a href=\"https:\/\/education.yandex.ru\/handbook\/ml\/article\/distillyaciya-znanij\"><strong>\u0414\u0438\u0441\u0442\u0438\u043b\u043b\u044f\u0446\u0438\u044f \u0437\u043d\u0430\u043d\u0438\u0439 (knowledge distillation)<\/strong><\/a> \u2014 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0438\u0437\u044f\u0449\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u0430\u044f \u00ab\u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0430\u044f\u00bb \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u00ab\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439\u00bb \u043c\u043e\u0434\u0435\u043b\u0438, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u043f\u0440\u0438 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0442\u0440\u0430\u0442\u0430\u0445.<\/p>\n<p>\u041e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/879494\/\">mixture of experts (MoE)<\/a> \u0438 <a href=\"https:\/\/www.ig1.com\/mixture-of-agents\/\">mixture of agents (MoA)<\/a>. \u0412 MoE \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439-\u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0435\u0448\u0430\u0435\u0442, \u043a\u0430\u043a\u043e\u0439 \u044d\u043a\u0441\u043f\u0435\u0440\u0442 \u043b\u0443\u0447\u0448\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0441 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0434\u0438\u043d \u044d\u043a\u0441\u043f\u0435\u0440\u0442 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0412 MoA \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u0433\u0435\u043d\u0442\u044b \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u0435\u0434\u0438\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u0432\u043e\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0434\u0438\u043d \u0430\u0433\u0435\u043d\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0435, \u0434\u0440\u0443\u0433\u043e\u0439 \u043d\u0430 \u043b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u043d\u043e\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u0435.\u00a0<\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0432\u0440\u043e\u0434\u0435 ChatGPT o3, Claude 3.7 Sonnet Thinking \u0438 DeepSeek R1 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u044b\u0434\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043d\u043e \u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u044b\u043c\u0438 \u0432 \u0436\u0438\u0437\u043d\u0438 \u2014 \u043e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0434\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.\u00a0<\/p>\n<hr\/>\n<p><strong>\u0412\u043e\u0442 \u0438 \u043f\u043e\u0434\u043e\u0448\u043b\u0430 \u043a \u043a\u043e\u043d\u0446\u0443 \u043d\u0430\u0448\u0430 \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439<\/strong> \u2014 \u0445\u043e\u0442\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0430\u043b\u043e. \u0421 \u043a\u0430\u0436\u0434\u044b\u043c \u0433\u043e\u0434\u043e\u043c \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044f \u043d\u043e\u0432\u044b\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c.\u00a0<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u0430\u043c \u0431\u044b\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/909100\/\"> https:\/\/habr.com\/ru\/articles\/909100\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><strong>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u00ab\u0425\u0430\u0431\u0440\u00bb!<\/strong><\/p>\n<p>\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043e\u0442 \u0440\u043e\u0431\u043a\u0438\u0445 \u0448\u0430\u0433\u043e\u0432 \u041c\u0430\u0440\u043a\u043e\u0432\u0430 \u0434\u043e \u0434\u043e\u043b\u0433\u043e\u0439 \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0440\u043e\u0447\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c, \u043f\u0440\u043e\u0439\u0434\u0435\u043c\u0441\u044f \u043f\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u043b\u0435\u0442 \u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c, \u043a\u0430\u043a \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0438 \u0434\u0430\u0436\u0435 \u0441\u043f\u043e\u0440\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<p><strong>\u041f\u0440\u0438\u0441\u0442\u0435\u0433\u043d\u0438\u0442\u0435 \u0442\u043e\u043a\u0435\u043d\u044b \u2014 \u0432\u0445\u043e\u0434 \u0432 \u0437\u043e\u043d\u0443 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0439!<\/strong><\/p>\n<hr\/>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/bothub\/articles\/909092\/\">\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439: \u0447. 1, \u043e\u0442 \u0446\u0435\u043f\u0435\u0439 \u041c\u0430\u0440\u043a\u043e\u0432\u0430 \u0434\u043e ELIZA\u00a0<\/a><\/p>\n<p><strong>\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439: \u0447. 2, \u043e\u0442 ChatGPT \u0434\u043e \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0435\u0436\u0438\u043c\u0430<\/strong> \u2190 \u0432\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0435\u0441\u044c \u0442\u0443\u0442.<\/p>\n<hr\/>\n<h2>2000-\u0435 \u0433\u043e\u0434\u044b: \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0438<\/h2>\n<p>\u0412\u0430\u0436\u043d\u044b\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435\u043c \u0432 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432 2000-\u0445 \u0433\u043e\u0434\u0430\u0445 \u0441\u0442\u0430\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D1%82%D0%B0%D1%82%D0%B8%D1%81%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B8%D0%B9_%D0%BC%D0%B0%D1%88%D0%B8%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BF%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%D0%B4\">\u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u0432<\/a>, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a Google Translate. \u041e\u043d\u0438 \u043d\u0430\u0447\u0430\u043b\u0438 \u0448\u0438\u0440\u043e\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438. \u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0440\u044b\u0432 \u043e\u0442\u043a\u0440\u044b\u043b \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043b\u044e\u0434\u0435\u0439 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d \u0438 \u043a\u0443\u043b\u044c\u0442\u0443\u0440, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u0432 \u0437\u043d\u0430\u0447\u0438\u043c\u043e\u0441\u0442\u044c NLP-\u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u0432 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435.\u00a0<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u043e\u0432 \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0437\u044b\u043a\u0430\u043c\u0438.<\/p>\n<hr\/>\n<h2>2013 \u0433\u043e\u0434: word2vec<\/h2>\n<p><a href=\"https:\/\/ru.wikipedia.org\/wiki\/Word2vec\"><strong>Word2vec<\/strong><\/a><strong> <\/strong>&#8212; \u043e\u0431\u0449\u0435\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0441\u043b\u043e\u0432 \u043d\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. \u041c\u043e\u0434\u0435\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0430\u043d\u0430\u043b\u0438\u0437 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438 (\u0440\u0430\u0437\u0434\u0435\u043b \u043b\u0438\u043d\u0433\u0432\u0438\u0441\u0442\u0438\u043a\u0438, \u0438\u0437\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u0441\u043c\u044b\u0441\u043b\u043e\u0432\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0435\u0434\u0438\u043d\u0438\u0446 \u044f\u0437\u044b\u043a\u0430) \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0434\u0438\u0441\u0442\u0440\u0438\u0431\u0443\u0442\u0438\u0432\u043d\u043e\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435, \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u0438 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u043b\u043e\u0432.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 word2vec: \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0432, \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u0421\u043f\u0440\u0430\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u043b\u043e\u0432\u0430, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u00abfrance\u00bb, \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043d\u043e\u0433\u043e \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430  <\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c word2vec \u0431\u044b\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u0433\u0440\u0443\u043f\u043f\u043e\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 Google \u0432 <strong>2013 \u0433\u043e\u0434\u0443<\/strong>. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e-\u0441\u0435\u043c\u0430\u043d\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0431\u044b\u043b\u0438 \u0438 \u0440\u0430\u043d\u0435\u0435, \u043e\u0434\u043d\u0430\u043a\u043e word2vec \u0441\u0442\u0430\u043b \u043f\u0435\u0440\u0432\u043e\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439:<strong> \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0437-\u0437\u0430 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b.\u00a0<\/strong><\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c word2vec \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0440\u043f\u0443\u0441 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0442\u0440\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u0438\u0437 \u043a\u043e\u0440\u043f\u0443\u0441\u0430 \u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0441\u0442\u0440\u043e\u0438\u0442 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u041a\u0430\u0436\u0434\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0443 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u043a\u0442\u043e\u0440 \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 &#8212; \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0443\u0436\u043d\u043e\u0435 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 word2vec \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.nn.functional as F  class SkipGramWithNegSampling(nn.Module):     def __init__(self, vocab_size, embed_size, num_neg_samples=5):         super().__init__()         self.target_embeddings = nn.Embedding(vocab_size, embed_size)  # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432         self.context_embeddings = nn.Embedding(vocab_size, embed_size)  # \u042d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0432          # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u0435\u0441\u043e\u0432 (\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u0434\u043b\u044f Word2Vec)         nn.init.xavier_uniform_(self.target_embeddings.weight)         nn.init.xavier_uniform_(self.context_embeddings.weight)          self.num_neg_samples = num_neg_samples  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043d\u0430 1 \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439      def forward(self, target_words, context_words):         \"\"\"         target_words: \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0446\u0435\u043b\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432 (\u0444\u043e\u0440\u043c\u0430 [batch_size])         context_words: \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0445 \u0441\u043b\u043e\u0432 (\u0444\u043e\u0440\u043c\u0430 [batch_size])         \"\"\"         # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u0438         target_emb = self.target_embeddings(target_words)  # [batch_size, embed_size]         context_emb = self.context_embeddings(context_words)  # [batch_size, embed_size]          # \u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c score \u0434\u043b\u044f \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0430\u0440         positive_scores = torch.sum(target_emb * context_emb, dim=1)  # [batch_size]          # \u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b (\u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)         batch_size = target_words.size(0)         negative_words = torch.randint(0, len(self.target_embeddings.weight),                                      (batch_size, self.num_neg_samples))  # [batch_size, num_neg]         negative_emb = self.context_embeddings(negative_words)  # [batch_size, num_neg, embed_size]          # \u0412\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c score \u0434\u043b\u044f \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0430\u0440         negative_scores = torch.bmm(negative_emb, target_emb.unsqueeze(2)).squeeze()  # [batch_size, num_neg]          return positive_scores, negative_scores  # \u0413\u0438\u043f\u0435\u0440\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b (\u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430) VOCAB_SIZE = 10000  # \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043b\u043e\u0432\u0430\u0440\u044f EMBED_SIZE = 300     # \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 BATCH_SIZE = 1024    # \u0420\u0430\u0437\u043c\u0435\u0440 \u0431\u0430\u0442\u0447\u0430 NUM_NEG = 5          # \u041d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043d\u0430 1 \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 model = SkipGramWithNegSampling(VOCAB_SIZE, EMBED_SIZE, NUM_NEG)  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043e\u0431\u044b\u0447\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u0443) # \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u0430\u0442\u0447 \u043f\u0430\u0440 (\u0446\u0435\u043b\u0435\u0432\u043e\u0435_\u0441\u043b\u043e\u0432\u043e, \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0435_\u0441\u043b\u043e\u0432\u043e) target_indices = torch.randint(0, VOCAB_SIZE, (BATCH_SIZE,))  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b context_indices = torch.randint(0, VOCAB_SIZE, (BATCH_SIZE,))  # \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 - \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430  # \u041f\u0440\u044f\u043c\u043e\u0439 \u043f\u0440\u043e\u0445\u043e\u0434 pos_scores, neg_scores = model(target_indices, context_indices)  # \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u0435\u0440\u044c (Max-margin loss) positive_loss = F.logsigmoid(pos_scores).mean() negative_loss = F.logsigmoid(-neg_scores).mean() loss = -(positive_loss + negative_loss)  # \u0423\u0441\u0440\u0435\u0434\u043d\u0435\u043d\u0438\u0435 \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u0432 negative_loss  # \u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 (\u043e\u0434\u0438\u043d \u0448\u0430\u0433) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) optimizer.zero_grad() loss.backward() optimizer.step()<\/code><\/pre>\n<hr\/>\n<h2>2017 \u0433\u043e\u0434: \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440<\/h2>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/486358\/\"><strong>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440<\/strong><\/a><strong> <\/strong>&#8212; \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0432 2017 \u0433\u043e\u0434\u0443 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0438\u0437 Google Brain.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0430\u0432\u0442\u043e\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0430\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u043b\u0435\u0432\u0430 \u2014 \u043c\u043e\u0434\u0443\u043b\u044c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043b\u043e\u0438. \u0421\u043f\u0440\u0430\u0432\u0430 \u2014 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a, \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u043e\u0451\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0440\u043e\u0441\u0441-\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0432\u044b\u0445\u043e\u0434\u0430\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430  <\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u044b\u043c\u0438 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044f\u043c\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0442\u0435\u043a\u0441\u0442 \u043d\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435, \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0432\u0440\u043e\u0434\u0435 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0435\u0444\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u043d\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u0442\u0435\u043a\u0441\u0442, \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0430 \u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0437\u0430\u0438\u043c\u043e\u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0438.<\/p>\n<p>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430. \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0435\u043a\u043e\u0434\u0435\u0440 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430: \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0445\u043e\u0434 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u044f; \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043a \u0432\u044b\u0445\u043e\u0434\u0443 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0442\u0430\u043a \u0438 \u0432\u044b\u0445\u043e\u0434 \u043a\u043e\u0434\u0435\u0440\u0430; \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u0443\u044e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0443\u044e \u0441\u0435\u0442\u044c, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0449\u0443\u044e \u043d\u0430 \u0432\u0445\u043e\u0434 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043b\u043e\u0439<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<strong> \u043c\u043d\u043e\u0433\u043e\u0433\u043e\u043b\u043e\u0432\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 (multi-head attention, MHA)<\/strong>, \u0433\u0434\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438. \u0422\u043e \u0435\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 \u0432\u0430\u0436\u043d\u044b \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0441 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0437\u0440\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b, \u043a\u0430\u043a \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439, \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043d\u0430 Python. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">import torch import torch.nn as nn import torch.nn.functional as F  class TransformerEncoderLayer(nn.Module):     \"\"\"     \u041e\u0434\u0438\u043d \u0441\u043b\u043e\u0439 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 (Post-LN \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0438\u0437 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438).      \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b:         d_model (int): \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0431\u0435\u0434\u0434\u0438\u043d\u0433\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 512)         nhead (int): \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u043b\u043e\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 8)         dim_feedforward (int): \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c FFN (\u043e\u0431\u044b\u0447\u043d\u043e 4*d_model)         dropout (float): \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0440\u043e\u043f\u0430\u0443\u0442\u0430 (\u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 0.1)     \"\"\"     def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1):         super().__init__()          # 1. \u0421\u043b\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0433\u043e\u043b\u043e\u0432\u043e\u0433\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f         self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)          # 2. \u0414\u0432\u0443\u0445\u0441\u043b\u043e\u0439\u043d\u0430\u044f FFN \u0441\u0435\u0442\u044c         self.linear1 = nn.Linear(d_model, dim_feedforward)         self.linear2 = nn.Linear(dim_feedforward, d_model)          # 3. \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0434\u0440\u043e\u043f\u0430\u0443\u0442\u044b         self.norm1 = nn.LayerNorm(d_model)         self.norm2 = nn.LayerNorm(d_model)         self.dropout = nn.Dropout(dropout)         self.dropout1 = nn.Dropout(dropout)         self.dropout2 = nn.Dropout(dropout)          # 4. \u0410\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u044f \u0434\u043b\u044f FFN (\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435 ReLU)         self.activation = F.relu      def forward(self, src, src_mask=None, src_key_padding_mask=None):         \"\"\"         \u0412\u0445\u043e\u0434:             src: (S, B, D) - \u0432\u0445\u043e\u0434\u043d\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c (\u0434\u043b\u0438\u043d\u0430, \u0431\u0430\u0442\u0447, \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c)             src_mask: (S, S) - \u043c\u0430\u0441\u043a\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f (\u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e)             src_key_padding_mask: (B, S) - \u043c\u0430\u0441\u043a\u0430 \u0431\u0430\u0442\u0447\u0430 (\u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e)         \"\"\"         # \u0428\u0430\u0433 1: \u0421\u0430\u043c\u043e\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0441 residual connection         attn_output, _ = self.self_attn(             src, src, src,             attn_mask=src_mask,             key_padding_mask=src_key_padding_mask         )         src = src + self.dropout1(attn_output)         src = self.norm1(src)  # Post-LN: \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u041f\u041e\u0421\u041b\u0415 residual          # \u0428\u0430\u0433 2: FFN \u0441 residual connection         ff_output = self.linear1(src)         ff_output = self.activation(ff_output)         ff_output = self.dropout(ff_output)         ff_output = self.linear2(ff_output)         src = src + self.dropout2(ff_output)         src = self.norm2(src)          return src  # \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u0431\u0435\u0437 \u0432\u044b\u0432\u043e\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0445) if<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-459524","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/459524","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=459524"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/459524\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}