{"id":128,"date":"2023-07-27T11:38:41","date_gmt":"2023-07-27T03:38:41","guid":{"rendered":"http:\/\/lingbo.online\/?p=128"},"modified":"2023-07-28T17:59:19","modified_gmt":"2023-07-28T09:59:19","slug":"lstm-pytorch%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/algorithm_learning\/lstm-pytorch%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"LSTM-Pytorch\u5b9e\u73b0"},"content":{"rendered":"<h3>\u6982\u8ff0<\/h3>\n<p>\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc LSTM\uff08long short-term memory\uff09\u662f RNN \u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u5176\u6838\u5fc3\u6982\u5ff5\u5728\u4e8e\u7ec6\u80de\u72b6\u6001\u4ee5\u53ca\u201c\u95e8\u201d\u7ed3\u6784\u3002\u7ec6\u80de\u72b6\u6001\u76f8\u5f53\u4e8e\u4fe1\u606f\u4f20\u8f93\u7684\u8def\u5f84\uff0c\u8ba9\u4fe1\u606f\u80fd\u5728\u5e8f\u5217\u8fde\u4e2d\u4f20\u9012\u4e0b\u53bb\u3002\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u4f5c\u7f51\u7edc\u7684\u201c\u8bb0\u5fc6\u201d\u3002\u7406\u8bba\u4e0a\u8bb2\uff0c\u7ec6\u80de\u72b6\u6001\u80fd\u591f\u5c06\u5e8f\u5217\u5904\u7406\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u4fe1\u606f\u4e00\u76f4\u4f20\u9012\u4e0b\u53bb\u3002\u56e0\u6b64\uff0c\u5373\u4f7f\u662f\u8f83\u65e9\u65f6\u95f4\u6b65\u957f\u7684\u4fe1\u606f\u4e5f\u80fd\u643a\u5e26\u5230\u8f83\u540e\u65f6\u95f4\u6b65\u957f\u7684\u7ec6\u80de\u4e2d\u6765\uff0c\u8fd9\u514b\u670d\u4e86\u77ed\u65f6\u8bb0\u5fc6\u7684\u5f71\u54cd\u3002\u4fe1\u606f\u7684\u6dfb\u52a0\u548c\u79fb\u9664\u6211\u4eec\u901a\u8fc7\u201c\u95e8\u201d\u7ed3\u6784\u6765\u5b9e\u73b0\uff0c\u201c\u95e8\u201d\u7ed3\u6784\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4f1a\u53bb\u5b66\u4e60\u8be5\u4fdd\u5b58\u6216\u9057\u5fd8\u54ea\u4e9b\u4fe1\u606f\u3002<br \/>\n<figure id=\"attachment_132\" aria-describedby=\"caption-attachment-132\" style=\"width: 1575px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/07\/36-7976-1.jpg\" alt=\"\" width=\"1575\" height=\"967\" class=\"size-full wp-image-132\" srcset=\"http:\/\/youpaiyun.lingbo.online\/2023\/07\/36-7976-1.jpg 1575w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/36-7976-1-300x184.jpg 300w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/36-7976-1-1024x629.jpg 1024w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/36-7976-1-768x472.jpg 768w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/36-7976-1-1536x943.jpg 1536w\" sizes=\"auto, (max-width: 1575px) 100vw, 1575px\" \/><figcaption id=\"caption-attachment-132\" class=\"wp-caption-text\">LSTM\u95e8\u7ed3\u6784<\/figcaption><\/figure><\/p>\n<h3>nn.LSTM<\/h3>\n<h4>__init__<\/h4>\n<ul>\n<li>input_size\uff1a\u8f93\u5165\u7279\u5f81x\u7684\u7ef4\u6570\uff0c\u5373\u6bcf\u4e00\u884c\u8f93\u5165\u5143\u7d20\u7684\u4e2a\u6570\u3002<\/li>\n<li>hidden_size\uff1a\u9690\u85cf\u5c42\u72b6\u6001h\u7684\u7ef4\u6570\uff0c\u5373\u9690\u85cf\u5c42\u8282\u70b9\u7684\u4e2a\u6570\u3002\u9690\u85cf\u5c42\u7684\u7ef4\u6570\u503c\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u548c\u8f93\u5165\u7684\u7ef4\u5ea6\u6ca1\u6709\u5173\u7cfb\u3002<\/li>\n<li>num_layers\uff1aLSTM\u5806\u53e0\u7684\u5c42\u6570\uff0c\u9ed8\u8ba4\u503c\u662f1\u5c42\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3a2\uff0c\u5c06\u88ab\u5904\u7406\u4e3a\u4e00\u4e2a<code>stacked LSTM<\/code>\uff0c\u5373\u7b2c\u4e8c\u4e2aLSTM\u63a5\u6536\u7b2c\u4e00\u4e2aLSTM\u7684\u8ba1\u7b97\u7ed3\u679c\uff0c\u5982\uff0c\u7b2c\u4e00\u5c42\u8f93\u5165<span class=\"katex-eq\" data-katex-display=\"false\">[X_0,X_1,X_2,...,X_t]<\/span>\uff0c\u8ba1\u7b97\u51fa<span class=\"katex-eq\" data-katex-display=\"false\">[h_0,h_1,h_2,...,h_t]<\/span>\uff0c\u7b2c\u4e8c\u5c42\u5c06 <span class=\"katex-eq\" data-katex-display=\"false\">[h_0,h_1,h_2,...,h_t]<\/span>\uff0c\u4f5c\u4e3a<span class=\"katex-eq\" data-katex-display=\"false\">[X_0,X_1,X_2,...,X_t]<\/span>\u8f93\u5165\u518d\u6b21\u8ba1\u7b97\uff0c\u8f93\u51fa\u6700\u540e\u7684<span class=\"katex-eq\" data-katex-display=\"false\">[h_0,h_1,h_2,...,h_t]<\/span>\u3002\u9ed8\u8ba4\u503c\u4e3a1\u3002<\/li>\n<\/ul>\n<h4>LSTM.forward()<\/h4>\n<p>\u4e0d\u540c\u4e8eRNN\uff0cLSTM\u5728\u524d\u5411\u4f20\u64ad\u65f6\u8fd4\u56de\u4e24\u4e2a\u4e2d\u95f4\u53d8\u91cf<span class=\"katex-eq\" data-katex-display=\"false\">h_t,c_t<\/span><\/p>\n<pre><code class=\"language-python\">out,(ht,ct)=lstm(x,[ht_1,ct_1])\n\n#\u6848\u4f8b\uff1a\u5efa\u7acb\u4e00\u4e2aLSTM\uff0c\u8f93\u5165\u7ef4\u6570\u4e3a100\uff0c\u9690\u85cf\u5c42\u4e3a20,LSTM\u5c42\u6570\u4e3a4\nlstm=nn.LSTM(input_size=100,hidden_size=20,num_layers=4)\nprint(lstm)\nx=torch.randn(10,3,100) # x:[seq,b,vec],\u53ef\u4ee5\u7406\u89e3\u4e3a3\u4e2a\u53e5\u5b50\uff0c\u6bcf\u4e2a\u53e5\u5b50\u6709\u5341\u4e2a\u5355\u8bcd\uff0c\u6bcf\u4e2a\u5355\u8bcdencoding\u6210\u4e3a\u957f\u5ea6\u4e3a100\u7684vector\nout,(h,c)=lstm(x) #[h_0,c_0]\nprint(out.shape,h.shape,c.shape)\n\n#torch.Size([10,3,20]) out:[seq,b,h]\n#torch.Size([4,3,20]) h\/c:[num_layer,b,h]\n#torch.Size([4,3,20]) h\/c:[num_layer,b,h]<\/code><\/pre>\n<h3>nn.LSTMCell<\/h3>\n<h4>__init__<\/h4>\n<ul>\n<li>input_size\uff1a\u8f93\u5165\u7ef4\u5ea6x<\/li>\n<li>hidden_size\uff1a\u9690\u85cf\u5c42h<\/li>\n<li>num_layers\uff1aLSTM\u5806\u53e0\u7684\u5c42\u6570<\/li>\n<\/ul>\n<h4>LSTMCell.forward()<\/h4>\n<pre><code class=\"language-python\">ht,ct=lstmcell(xt,[ht_1,ct_1])\n\n#Single layer\nprint(&#039;one layer lstm&#039;)\ncell=nn.LSTMCell(input_size=100,hidden_size=20)\nh=torch.zeros(3,20)\nc=torch.zeros(3,20) #1\u5c42\uff0c3\u53e5\u8bdd\uff0chidden=20\nfor xt in x:\n    h,c=cell(xt,[h,c]) #xt:[b,vec]\nprint(h.shape,c.shape)\n\ntorch.Size([3,20])\ntorch.Size([3,20])#ht\/ct:[b,h]\n\n#Two Layers\nprint(&#039;two layer lstm&#039;)\ncell1=nn.LSTMCell(input_size=100,hidden_size=30)\ncell2=nn.LSTMCell(input_size=30,hidden_size=20)\nh1=torch.zeros(3,30)\nc1=torch.zeros(3,30)\nh2=torch.zeros(3,20)\nc2=torch.zeros(3,20)\nfor xt in x:\n    h1,c1=cell1(xt,[ht,c1])\n    h2,c2=cell2(h1,[h2,c2])\n\nprint(h2.shape,c2.shape)\ntorch.Size(3,20)\ntorch.Size(3,20)<\/code><\/pre>\n<h3>\u6848\u4f8b\u5206\u6790<\/h3>\n<h4>\u80cc\u666f\u4ecb\u7ecd<\/h4>\n<p>\u672c\u6587\u4f7f\u7528\u4e00\u4e2a\u4ea4\u901a\u6d41\u91cf\u9884\u6d4b\u6848\u4f8b\u6765\u8bf4\u660ePytorch\u4e2dLSTM\u6a21\u578b\u7684\u5efa\u6a21\u65b9\u6cd5\u3002\u8be5\u6570\u636e\u6765\u6e90\u4e8e<a href=\"https:\/\/webtris.highwaysengland.co.uk\/\" title=\"\u82f1\u56fd\u9ad8\u901f\uff08\u8fc7\u9053\uff09\" target=\"_blank\"  rel=\"nofollow\" >\u82f1\u56fd\u9ad8\u901f\uff08\u56fd\u9053\uff09<\/a>\u5404\u4e2a\u76d1\u6d4b\u70b9\u7684\u6bcf\u65e5\u4ea4\u901a\u6d41\u91cf\u6570\u636e\uff0c\u5176\u4e2d\u6570\u636e\u6807\u51c6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<figure id=\"attachment_167\" aria-describedby=\"caption-attachment-167\" style=\"width: 1785px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2023\/07\/\u6570\u636e\u683c\u5f0f.png\" alt=\"\" width=\"1785\" height=\"418\" class=\"size-full wp-image-167\" srcset=\"http:\/\/youpaiyun.lingbo.online\/2023\/07\/\u6570\u636e\u683c\u5f0f.png 1785w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/\u6570\u636e\u683c\u5f0f-300x70.png 300w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/\u6570\u636e\u683c\u5f0f-1024x240.png 1024w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/\u6570\u636e\u683c\u5f0f-768x180.png 768w, http:\/\/youpaiyun.lingbo.online\/2023\/07\/\u6570\u636e\u683c\u5f0f-1536x360.png 1536w\" sizes=\"auto, (max-width: 1785px) 100vw, 1785px\" \/><figcaption id=\"caption-attachment-167\" class=\"wp-caption-text\">\u82f1\u56fd\u9ad8\u901f\u516c\u8def\u4ea4\u901a\u68c0\u6d4b\u6d41\u91cf\u6570\u636e\u57fa\u672c\u683c\u5f0f<\/figcaption><\/figure>\n<p>\u6807\u51c6\u6570\u636e\u7684\u91c7\u6837\u9891\u7387\u4e3a15\u5206\u949f\uff0c\u5b57\u6bb5\u5305\u62ec\u76d1\u6d4b\u70b9id\u3001\u65e5\u671f\u3001\u65f6\u95f4\u3001\u65f6\u95f4\u95f4\u9694\u3001\u8f66\u8f86\u957f\u5ea6\uff08\u7528\u4e8e\u533a\u522b\u4e0d\u540c\u7684\u8f66\u8f86\u7c7b\u578b\uff09\u3001\u5e73\u5747\u901f\u5ea6\uff08\u82f1\u91cc\/\u5c0f\u65f6\uff09\u548c\u76d1\u6d4b\u6d41\u91cf\u3002\u6848\u4f8b\u4e2d\u7684\u9884\u6d4b\u6a21\u578b\u4ec5\u4f7f\u7528\u8f66\u6d41\u91cf\u548c\u5e73\u5747\u901f\u5ea6\u4f5c\u4e3a\u6a21\u578b\u8f93\u5165\u7684\u7279\u5f81\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 \u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc LSTM\uff08long short-term memory\uff09\u662f RNN \u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u5176\u6838\u5fc3\u6982\u5ff5\u5728\u4e8e\u7ec6\u80de\u72b6\u6001\u4ee5\u53ca\u201c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-algorithm_learning"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":8,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}