{"id":52836,"date":"2025-08-11T20:09:36","date_gmt":"2025-08-11T12:09:36","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/52836.html"},"modified":"2025-08-11T20:09:36","modified_gmt":"2025-08-11T12:09:36","slug":"%e8%a1%80%e4%b9%a6%e4%b8%a4%e4%b8%87%e5%ad%97%e5%b8%a6%e4%bd%a0%e5%81%9arnn%e6%a1%88%e4%be%8b-%e4%ba%ba%e5%90%8d%e5%88%86%e7%b1%bb%e5%99%a8%e9%99%84%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/52836.html","title":{"rendered":"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b--\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!)"},"content":{"rendered":"<p>\u5404\u4f4d\u89c2\u4f17\u8001\u7237&#xff0c;\u5927\u5bb6\u597d&#xff0c;\u6211\u662f<span style=\"color:#ed7976\">\u8bd7\u4eba\u554a_<\/span>&#xff0c;\u4eca\u5929\u548c\u5404\u4f4d\u5206\u4eab\u4e00\u4e2aRNN\u6848\u4f8b &#8212; \u4eba\u540d\u5206\u7c7b\u5668,\u4e00\u6587\u901f\u901a~<\/p>\n<p>&#xff08;\u5c4f\u5e55\u524d\u7684\u4f60&#xff0c;\u5e05\u6c14\u4f4e\u8c03\u6709\u5185\u6db5&#xff0c;\u7f8e\u4e3d\u5927\u65b9\u5f88\u4f18\u96c5\u2026\u6240\u4ee5&#xff0c;\u6c42\u4e2a\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u5173\u6ce8\u5457&#xff5e;&#xff09;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p style=\"text-align:center\">!!!!!!\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(PS:<span style=\"color:#faa572\">\u9700\u8981\u8bad\u7ec3\u6570\u636e\u7684\u4f19\u4f34, \u8bf7\u7559\u8a00<\/span> ,\u6211\u4f1a\u9010\u4e00\u53d1\u9001)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0!!!!!!<\/p>\n<p>\u672c\u6587\u5c06\u4ece\u5934\u5230\u5c3e&#xff0c;\u7528PyTorch\u4eb2\u624b\u642d\u5efaRNN\u3001LSTM\u3001GRU\u4e09\u79cd\u7ecf\u5178\u6a21\u578b&#xff0c;\u4e00\u6b65\u6b65\u5b9e\u73b0&#034;\u8f93\u5165\u4eba\u540d\u2192\u9884\u6d4b\u56fd\u7c4d&#034;\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<p>\u65e0\u8bba\u4f60\u662f\u521a\u8e0f\u5165\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u7684\u65b0\u624b&#xff0c;\u8fd8\u662f\u60f3\u5de9\u56fa\u5e8f\u5217\u6a21\u578b\u57fa\u7840\u7684\u5f00\u53d1\u8005&#xff0c;\u8ddf\u7740\u8fd9\u4e2a\u6848\u4f8b\u5b9e\u64cd&#xff0c;\u4e0d\u4ec5\u80fd\u5403\u900f\u6587\u672c\u5206\u7c7b\u7684\u6838\u5fc3\u6280\u6cd5&#xff0c;\u66f4\u80fd\u76f4\u89c2\u89e6\u6478\u5230\u4e0d\u540c\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u7684&#034;\u601d\u7ef4\u5dee\u5f02&#034;~<\/p>\n<h2>\u5b66\u4e60\u76ee\u6807<\/h2>\n<ul>\n<li>\u4e86\u89e3\u6709\u5173\u4eba\u540d\u5206\u7c7b\u95ee\u9898\u548c\u6709\u5173\u6570\u636e.<\/li>\n<li>\u638c\u63e1\u4f7f\u7528RNN\u6784\u5efa\u4eba\u540d\u5206\u7c7b\u5668\u5b9e\u73b0\u8fc7\u7a0b.<\/li>\n<\/ul>\n<h3 id=\"1\">1 \u6848\u4f8b\u4ecb\u7ecd<\/h3>\n<ul>\n<li>\n<p>\u5173\u4e8e\u4eba\u540d\u5206\u7c7b\u95ee\u9898:<\/p>\n<p>\u4ee5\u4e00\u4e2a\u4eba\u540d\u4e3a\u8f93\u5165, \u4f7f\u7528\u6a21\u578b\u5e2e\u52a9\u6211\u4eec\u5224\u65ad\u5b83\u6700\u6709\u53ef\u80fd\u662f\u6765\u81ea\u54ea\u4e00\u4e2a\u56fd\u5bb6\u7684\u4eba\u540d, \u8fd9\u5728\u67d0\u4e9b\u56fd\u9645\u5316\u516c\u53f8\u7684\u4e1a\u52a1\u4e2d\u5177\u6709\u91cd\u8981\u610f\u4e49, \u5728\u7528\u6237\u6ce8\u518c\u8fc7\u7a0b\u4e2d, \u4f1a\u6839\u636e\u7528\u6237\u586b\u5199\u7684\u540d\u5b57\u76f4\u63a5\u7ed9\u4ed6\u5206\u914d\u53ef\u80fd\u7684\u56fd\u5bb6\u6216\u5730\u533a\u9009\u9879, \u4ee5\u53ca\u8be5\u56fd\u5bb6\u6216\u5730\u533a\u7684\u56fd\u65d7, \u9650\u5236\u624b\u673a\u53f7\u7801\u4f4d\u6570\u7b49\u7b49.<\/p>\n<\/li>\n<li>\n<p>\u4eba\u540d\u5206\u7c7b\u6570\u636e\u9884\u89c8<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5b58\u653e\u8def\u5f84&#xff1a;$(home)\/data\/name_classfication.txt<\/p>\n<\/li>\n<li>\u6570\u636e\u683c\u5f0f\u8bf4\u660e \u6bcf\u4e00\u884c\u7b2c\u4e00\u4e2a\u5355\u8bcd\u4e3a\u4eba\u540d&#xff0c;\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u4e3a\u56fd\u5bb6\u540d\u3002\u4e2d\u95f4\u7528\u5236\u8868\u7b26tab\u5206\u5272<\/li>\n<\/ul>\n<p>Huffmann    German<br \/>\nHummel  German<br \/>\nHummel  German<br \/>\nHutmacher   German<br \/>\nIngersleben German<br \/>\nJaeger  German<br \/>\nJager   German<br \/>\nDeng    Chinese<br \/>\nDing    Chinese<br \/>\nDong    Chinese<br \/>\nDou Chinese<br \/>\nDuan    Chinese<br \/>\nEng Chinese<br \/>\nFan Chinese<br \/>\nFei Chinese<br \/>\nAbaimov Russian<br \/>\nAbakeliya   Russian<br \/>\nAbakovsky   Russian<br \/>\nAbakshin    Russian<br \/>\nAbakumoff   Russian<br \/>\nAbakumov    Russian<br \/>\nAbakumtsev  Russian<br \/>\nAbakushin   Russian<br \/>\nAbalakin    Russian<\/p>\n<h3 id=\"2\">2 \u6848\u4f8b\u6b65\u9aa4<\/h3>\n<p>\u6574\u4e2a\u6848\u4f8b\u7684\u5b9e\u73b0\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e94\u4e2a\u6b65\u9aa4<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b65\u5bfc\u5165\u5fc5\u5907\u7684\u5de5\u5177\u5305<\/li>\n<li>\u7b2c\u4e8c\u6b65\u5bf9data\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406&#xff0c;\u6ee1\u8db3\u8bad\u7ec3\u8981\u6c42<\/li>\n<li>\u7b2c\u4e09\u6b65\u6784\u5efaRNN\u6a21\u578b(\u5305\u62ec\u4f20\u7edfRNN, LSTM\u4ee5\u53caGRU)<\/li>\n<li>\u7b2c\u56db\u6b65\u6784\u5efa\u8bad\u7ec3\u51fd\u6570\u5e76\u8fdb\u884c\u8bad\u7ec3<\/li>\n<li>\u7b2c\u4e94\u6b65\u6784\u5efa\u9884\u6d4b\u51fd\u6570\u5e76\u8fdb\u884c\u9884\u6d4b<\/li>\n<\/ul>\n<h4 id=\"21\">2.1 \u5bfc\u5165\u5fc5\u5907\u7684\u5de5\u5177\u5305<\/h4>\n<p># \u5bfc\u5165torch\u5de5\u5177<br \/>\nimport torch<br \/>\n# \u5bfc\u5165nn\u51c6\u5907\u6784\u5efa\u6a21\u578b<br \/>\nimport torch.nn as nn<br \/>\nimport torch.nn.functional as F<br \/>\nimport torch.optim as optim<br \/>\n# \u5bfc\u5165torch\u7684\u6570\u636e\u6e90 \u6570\u636e\u8fed\u4ee3\u5668\u5de5\u5177\u5305<br \/>\nfrom  torch.utils.data import Dataset, DataLoader<br \/>\n# \u7528\u4e8e\u83b7\u5f97\u5e38\u89c1\u5b57\u6bcd\u53ca\u5b57\u7b26\u89c4\u8303\u5316<br \/>\nimport string<br \/>\n# \u5bfc\u5165\u65f6\u95f4\u5de5\u5177\u5305<br \/>\nimport time<br \/>\n# \u5f15\u5165\u5236\u56fe\u5de5\u5177\u5305<br \/>\nimport matplotlib.pyplot as plt<br \/>\n# \u4eceio\u4e2d\u5bfc\u5165\u6587\u4ef6\u6253\u5f00\u65b9\u6cd5<br \/>\nfrom io import open<\/p>\n<h4 id=\"22\">2.2 \u6570\u636e\u9884\u5904\u7406<\/h4>\n<p>\u8fd9\u91cc\u9700\u8981\u5bf9data\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406&#xff0c;\u6ee1\u8db3\u8bad\u7ec3\u8981\u6c42\u3002<\/p>\n<h5 id=\"1_1\">1 \u83b7\u53d6\u5e38\u7528\u7684\u5b57\u7b26\u6570\u91cf<\/h5>\n<p># \u83b7\u53d6\u6240\u6709\u5e38\u7528\u5b57\u7b26\u5305\u62ec\u5b57\u6bcd\u548c\u5e38\u7528\u6807\u70b9<br \/>\nall_letters &#061; string.ascii_letters &#043; &#034; .,;&#039;&#034;<\/p>\n<p># \u83b7\u53d6\u5e38\u7528\u5b57\u7b26\u6570\u91cf<br \/>\nn_letters &#061; len(all_letters)<\/p>\n<p>print(&#034;n_letter:&#034;, n_letters)<\/p>\n<ul>\n<li>\u8f93\u51fa\u6548\u679c:<\/li>\n<\/ul>\n<p>n_letter: 57<\/p>\n<h5 id=\"2_1\">2 \u56fd\u5bb6\u540d\u79cd\u7c7b\u6570\u548c\u4e2a\u6570<\/h5>\n<p># \u56fd\u5bb6\u540d \u79cd\u7c7b\u6570<br \/>\ncategorys &#061; [&#039;Italian&#039;, &#039;English&#039;, &#039;Arabic&#039;, &#039;Spanish&#039;, &#039;Scottish&#039;, &#039;Irish&#039;, &#039;Chinese&#039;, &#039;Vietnamese&#039;, &#039;Japanese&#039;,<br \/>\n             &#039;French&#039;, &#039;Greek&#039;, &#039;Dutch&#039;, &#039;Korean&#039;, &#039;Polish&#039;, &#039;Portuguese&#039;, &#039;Russian&#039;, &#039;Czech&#039;, &#039;German&#039;]<br \/>\n# \u56fd\u5bb6\u540d \u4e2a\u6570<br \/>\ncategorynum &#061; len(categorys)<br \/>\nprint(&#039;categorys&#8212;&gt;&#039;, categorys)<\/p>\n<ul>\n<li>\u8f93\u51fa\u6548\u679c:<\/li>\n<\/ul>\n<p>categorys&#8212;&gt; [&#039;Italian&#039;, &#039;English&#039;, &#039;Arabic&#039;, &#039;Spanish&#039;, &#039;Scottish&#039;, &#039;Irish&#039;, &#039;Chinese&#039;, &#039;Vietnamese&#039;, &#039;Japanese&#039;, &#039;French&#039;, &#039;Greek&#039;, &#039;Dutch&#039;, &#039;Korean&#039;, &#039;Polish&#039;, &#039;Portuguese&#039;, &#039;Russian&#039;, &#039;Czech&#039;, &#039;German&#039;]<br \/>\ncategorynum&#8212;&gt; 18<\/p>\n<h5 id=\"3\">3 \u8bfb\u6570\u636e\u5230\u5185\u5b58<\/h5>\n<p># \u601d\u8def\u5206\u6790<br \/>\n# 1 \u6253\u5f00\u6570\u636e\u6587\u4ef6 open(filename, mode&#061;&#039;r&#039;, encoding&#061;&#039;utf-8&#039;)<br \/>\n# 2 \u6309\u884c\u8bfb\u6587\u4ef6\u3001\u63d0\u53d6\u6837\u672cx \u6837\u672cy line.strip().split(&#039;\\\\t&#039;)<br \/>\n# 3 \u8fd4\u56de\u6837\u672cx\u7684\u5217\u8868\u3001\u6837\u672cy\u7684\u5217\u8868 my_list_x, my_list_y<br \/>\ndef read_data(filename):<br \/>\n    my_list_x, my_list_y&#061; [], []<br \/>\n    # \u6253\u5f00\u6587\u4ef6<br \/>\n    with  open(filename, mode&#061;&#039;r&#039;, encoding&#061;&#039;utf-8&#039;) as f:<br \/>\n        # \u6309\u7167\u884c\u8bfb\u6570\u636e<br \/>\n        for line in f.readlines():<br \/>\n            if len(line) &lt;&#061; 5:<br \/>\n                continue<br \/>\n            # \u6309\u7167\u884c\u63d0\u53d6\u6837\u672cx \u6837\u672cy<br \/>\n            (x, y) &#061; line.strip().split(&#039;\\\\t&#039;)<br \/>\n            my_list_x.append(x)<br \/>\n            my_list_y.append(y)<\/p>\n<p>    # \u6253\u5370\u6837\u672c\u7684\u6570\u91cf<br \/>\n    print(&#039;my_list_x-&gt;&#039;, len(my_list_x))<br \/>\n    print(&#039;my_list_y-&gt;&#039;, len(my_list_y))<\/p>\n<p>    # \u8fd4\u56de\u6837\u672cx\u7684\u5217\u8868\u3001\u6837\u672cy\u7684\u5217\u8868<br \/>\n    return my_list_x, my_list_y<\/p>\n<h5 id=\"4-nameclassdataset\">4 \u6784\u5efa\u6570\u636e\u6e90NameClassDataset<\/h5>\n<p># \u539f\u59cb\u6570\u636e -&gt; \u6570\u636e\u6e90NameClassDataset &#8211;&gt; \u6570\u636e\u8fed\u4ee3\u5668DataLoader<br \/>\n# \u6784\u9020\u6570\u636e\u6e90 NameClassDataset&#xff0c;\u628a\u8bed\u6599\u8f6c\u6362\u6210x y<br \/>\n# 1 init\u51fd\u6570 \u8bbe\u7f6e\u6837\u672cx\u548cy self.my_list_x self.my_list_y \u6761\u76ee\u6570self.sample_len<br \/>\n# 2 __len__(self)\u51fd\u6570  \u83b7\u53d6\u6837\u672c\u6761\u6570<br \/>\n# 3 __getitem__(self, index)\u51fd\u6570 \u83b7\u53d6\u7b2c\u51e0\u6761\u6837\u672c\u6570\u636e<br \/>\n#       \u6309\u7d22\u5f15 \u83b7\u53d6\u6570\u636e\u6837\u672c x y<br \/>\n#       \u6837\u672cx one-hot\u5f20\u91cf\u5316 tensor_x[li][all_letters.find(letter)] &#061; 1<br \/>\n#       \u6837\u672cy \u5f20\u91cf\u5316 torch.tensor(categorys.index(y), dtype&#061;torch.long)<br \/>\n#       \u8fd4\u56detensor_x, tensor_y<br \/>\nclass NameClassDataset(Dataset):<br \/>\n    def __init__(self, my_list_x, my_list_y):<\/p>\n<p>        # \u6837\u672cx<br \/>\n        self.my_list_x &#061; my_list_x<\/p>\n<p>        # \u6837\u672cy<br \/>\n        self.my_list_y &#061; my_list_y<\/p>\n<p>        # \u6837\u672c\u6761\u76ee\u6570<br \/>\n        self.sample_len &#061; len(my_list_x)<\/p>\n<p>    # \u83b7\u53d6\u6837\u672c\u6761\u6570<br \/>\n    def __len__(self):<br \/>\n        return self.sample_len<\/p>\n<p>    # \u83b7\u53d6\u7b2c\u51e0\u6761 \u6837\u672c\u6570\u636e<br \/>\n    def __getitem__(self, index):<\/p>\n<p>        # \u5bf9index\u5f02\u5e38\u503c\u8fdb\u884c\u4fee\u6b63 [0, self.sample_len-1]<br \/>\n        index &#061; min(max(index, 0), self.sample_len-1)<\/p>\n<p>        # \u6309\u7d22\u5f15\u83b7\u53d6 \u6570\u636e\u6837\u672c x y<br \/>\n        x &#061; self.my_list_x[index]<br \/>\n        y &#061; self.my_list_y[index]<\/p>\n<p>        # \u6837\u672cx one-hot\u5f20\u91cf\u5316<br \/>\n        tensor_x &#061; torch.zeros(len(x), n_letters)<br \/>\n        # \u904d\u5386\u4eba\u540d \u7684 \u6bcf\u4e2a\u5b57\u6bcd \u505a\u6210one-hot\u7f16\u7801<br \/>\n        for li, letter in enumerate(x):<br \/>\n            # letter2indx \u4f7f\u7528all_letters.find(letter)\u67e5\u627e\u5b57\u6bcd\u5728all_letters\u8868\u4e2d\u7684\u4f4d\u7f6e \u7ed9one-hot\u8d4b\u503c<br \/>\n            tensor_x[li][all_letters.find(letter)] &#061; 1<\/p>\n<p>        # \u6837\u672cy \u5f20\u91cf\u5316<br \/>\n        tensor_y &#061; torch.tensor(categorys.index(y), dtype&#061;torch.long)<\/p>\n<p>        # \u8fd4\u56de\u7ed3\u679c<br \/>\n        return tensor_x, tensor_y<\/p>\n<ul>\n<li>\u5206\u6790<\/li>\n<li>\u6587\u672c\u5f20\u91cf\u5316&#xff0c;\u8fd9\u91cc\u4e5f\u5c31\u662f\u4eba\u540d\u5f20\u91cf\u5316\u662f\u901a\u8fc7one-hot\u7f16\u7801\u6765\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<p># \u5c06\u5b57\u7b26\u4e32(\u5355\u8bcd\u7c92\u5ea6)\u8f6c\u5316\u4e3a\u5f20\u91cf\u8868\u793a&#xff0c;\u5982&#xff1a;&#034;ab&#034; &#8212;&gt;<br \/>\n# tensor([[[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0.]],<\/p>\n<p>#        [[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n#          0., 0., 0., 0., 0., 0.]]])<\/p>\n<h5 id=\"5\">5 \u6784\u5efa\u8fed\u4ee3\u5668\u904d\u5386\u6570\u636e<\/h5>\n<p>def dm_test_NameClassDataset():<\/p>\n<p>    # 1 \u83b7\u53d6\u6570\u636e<br \/>\n    myfilename &#061; &#039;.\/data\/name_classfication.txt&#039;<br \/>\n    my_list_x, my_list_y &#061; read_data(myfilename)<br \/>\n    print(&#039;my_list_x length&#039;, len(my_list_x))<br \/>\n    print(&#039;my_list_y length&#039;, len(my_list_y))<\/p>\n<p>    # 2 \u5b9e\u4f8b\u5316dataset\u5bf9\u8c61<br \/>\n    nameclassdataset &#061; NameClassDataset(my_list_x, my_list_y)<\/p>\n<p>    # 3 \u5b9e\u4f8b\u5316dataloader<br \/>\n    mydataloader &#061; DataLoader(dataset&#061;nameclassdataset, batch_size&#061;1, shuffle&#061;True)<br \/>\n    for  i, (x, y) in enumerate (mydataloader):<br \/>\n        print(&#039;x.shape&#039;, x.shape, x)<br \/>\n        print(&#039;y.shape&#039;, y.shape, y)<br \/>\n        break<\/p>\n<ul>\n<li>\u8f93\u51fa\u6548\u679c:<\/li>\n<\/ul>\n<p>my_list_x length 20074<br \/>\nmy_list_y length 20074<br \/>\nx.shape torch.Size([1, 5, 57]) tensor([[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0.],<br \/>\n         [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0.],<br \/>\n         [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0.],<br \/>\n         [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0.],<br \/>\n         [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,<br \/>\n          0., 0., 0., 0., 0., 0.]]])<br \/>\ny.shape torch.Size([1]) tensor([15])<\/p>\n<h4 id=\"23-rnn\">2.3 \u6784\u5efaRNN\u6a21\u578b<\/h4>\n<h5 id=\"1-rnn\">1 \u6784\u5efaRNN\u6a21\u578b<\/h5>\n<p># RNN\u7c7b \u5b9e\u73b0\u601d\u8def\u5206\u6790&#xff1a;<br \/>\n# 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n#    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1)<\/p>\n<p># 2 forward(input, hidden)\u51fd\u6570<br \/>\n#   \u8ba9\u6570\u636e\u7ecf\u8fc7\u4e09\u4e2a\u5c42 \u8fd4\u56desoftmax\u7ed3\u679c\u548chn<br \/>\n#   \u5f62\u72b6\u53d8\u5316 [seqlen,1,57],[1,1,128]) -&gt; [seqlen,1,128],[1,1,128]<\/p>\n<p># 3 \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n#   \u5f62\u72b6[self.num_layers, 1, self.hidden_size]<br \/>\nclass RNN(nn.Module):<\/p>\n<p>    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1):<br \/>\n        super(RNN, self).__init__()<br \/>\n        # 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n        self.input_size &#061; input_size<br \/>\n        self.hidden_size &#061; hidden_size<br \/>\n        self.output_size &#061; output_size<br \/>\n        self.num_layers &#061; num_layers<\/p>\n<p>        # \u5b9a\u4e49rnn\u5c42<br \/>\n        self.rnn &#061; nn.RNN(self.input_size, self.hidden_size, self.num_layers)<\/p>\n<p>        # \u5b9a\u4e49linear\u5c42<br \/>\n        self.linear &#061; nn.Linear(self.hidden_size, self.output_size)<\/p>\n<p>        # \u5b9a\u4e49softmax\u5c42<br \/>\n        self.softmax &#061; nn.LogSoftmax(dim&#061;-1)<\/p>\n<p>    def forward(self, input, hidden):<br \/>\n        &#039;&#039;&#039;input [6,57]-2\u7ef4\u77e9\u9635 hidden[1,1,57] &#8211; 3\u7ef4\u77e9\u9635&#039;&#039;&#039;<\/p>\n<p>        # \u6570\u636e\u5f62\u72b6 [6,57] -&gt; [6,1,57]<br \/>\n        input &#061; input.unsqueeze(1)<br \/>\n        # 1 \u6570\u636e\u7ecf\u8fc7\u6a21\u578b \u63d0\u53d6\u4e8b\u7269\u7279\u5f81<br \/>\n        # \u6570\u636e\u5f62\u72b6 [seqlen,1,57],[1,1,128]) -&gt; [seqlen,1,18],[1,1,128]<br \/>\n        rr, hn &#061; self.rnn(input, hidden)<\/p>\n<p>        # \u6570\u636e\u5f62\u72b6 [seqlen,1,128] &#8211; [1, 128]  eg:[6,1,128] &#8211;&gt; [1,128]<br \/>\n        tmprr &#061; rr[-1]<\/p>\n<p>        # 2 \u6570\u636e\u7ecf\u8fc7\u5168\u8fde\u63a5\u5c42 [1,128] &#8211;&gt;[1,18]<br \/>\n        tmprr &#061; self.linear(tmprr)<\/p>\n<p>        # 3 \u6570\u636e\u7ecf\u8fc7softmax\u5c42\u8fd4\u56de<br \/>\n        return self.softmax(tmprr), hn<\/p>\n<p>    def inithidden(self):<br \/>\n        # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n        return torch.zeros(self.num_layers, 1,self.hidden_size)<\/p>\n<ul>\n<li>torch.unsqueeze\u6f14\u793a:<\/li>\n<\/ul>\n<p>&gt;&gt;&gt; x &#061; torch.tensor([1, 2, 3, 4])<br \/>\n&gt;&gt;&gt; torch.unsqueeze(x, 0)<br \/>\ntensor([[ 1,  2,  3,  4]])<br \/>\n&gt;&gt;&gt; torch.unsqueeze(x, 1)<br \/>\ntensor([[ 1],<br \/>\n        [ 2],<br \/>\n        [ 3],<br \/>\n        [ 4]])<\/p>\n<ul>\n<li>\u8c03\u7528:<\/li>\n<\/ul>\n<p>def dm01_test_myrnn():<\/p>\n<p>    # 1 \u5b9e\u4f8b\u5316rnn\u5bf9\u8c61<br \/>\n    myrnn &#061; RNN(57, 128, 18)<br \/>\n    print(&#039;myrnn&#8212;&gt;&#039;, myrnn)<\/p>\n<p>    # 2 \u51c6\u5907\u6570\u636e<br \/>\n    input &#061; torch.randn(6, 57)<br \/>\n    print(input.shape)<br \/>\n    hidden &#061; myrnn.inithidden()<\/p>\n<p>    # 3 \u7ed9\u6a21\u578b1\u6b21\u6027\u7684\u9001\u6570\u636e<br \/>\n    # [seqlen, 57], [1, 1, 128]) -&gt; [1,18], [1,1,128]<br \/>\n    output, hidden &#061; myrnn(input, hidden)<br \/>\n    print(&#039;\u4e00\u6b21\u6027\u7684\u9001\u6570\u636e&#xff1a;output-&gt;&#039;, output.shape, output)<br \/>\n    print(&#039;hidden-&gt;&#039;, hidden.shape)<\/p>\n<p>    # 4 \u7ed9\u6a21\u578b1\u4e2a\u5b57\u7b261\u4e2a\u5b57\u7b26\u7684\u5582\u6570\u636e<br \/>\n    hidden &#061; myrnn.inithidden()<br \/>\n    for i in range(input.shape[0]):<br \/>\n        tmpinput &#061; input[i].unsqueeze(0)<br \/>\n        output, hidden &#061; myrnn(tmpinput, hidden)<\/p>\n<p>    # \u6700\u540e\u4e00\u6b21ouput<br \/>\n    print(&#039;\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u7684\u9001\u6570\u636eoutput-&gt;&#039;, output.shape, output)<\/p>\n<ul>\n<li>\u8c03\u7528\u7ed3\u679c:<\/li>\n<\/ul>\n<p>myrnn&#8212;&gt; RNN(<br \/>\n  (rnn): RNN(57, 128)<br \/>\n  (linear): Linear(in_features&#061;128, out_features&#061;18, bias&#061;True)<br \/>\n  (softmax): LogSoftmax(dim&#061;-1)<br \/>\n)<br \/>\ntorch.Size([6, 57])<br \/>\n\u4e00\u6b21\u6027\u7684\u9001\u6570\u636e&#xff1a;output-&gt; torch.Size([1, 18]) tensor([[-2.8194, -3.1730, -3.3112, -2.9715, -3.0997, -2.8097, -2.8016, -2.8738,<br \/>\n         -2.7229, -2.8181, -2.7881, -3.0218, -2.9169, -2.6193, -2.8507, -2.9684,<br \/>\n         -2.8589, -2.8273]], grad_fn&#061;&lt;LogSoftmaxBackward0&gt;)<br \/>\nhidden-&gt; torch.Size([1, 1, 128])<br \/>\n\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u7684\u9001\u6570\u636eoutput-&gt; torch.Size([1, 18]) tensor([[-2.8194, -3.1730, -3.3112, -2.9715, -3.0997, -2.8097, -2.8016, -2.8738,<br \/>\n         -2.7229, -2.8181, -2.7881, -3.0218, -2.9169, -2.6193, -2.8507, -2.9684,<br \/>\n         -2.8589, -2.8273]], grad_fn&#061;&lt;LogSoftmaxBackward0&gt;)<\/p>\n<h5 id=\"2-lstm\">2 \u6784\u5efaLSTM\u6a21\u578b<\/h5>\n<p># LSTM\u7c7b \u5b9e\u73b0\u601d\u8def\u5206\u6790&#xff1a;<br \/>\n# 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n#    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1)<\/p>\n<p># 2 forward(input, hidden)\u51fd\u6570<br \/>\n#   \u8ba9\u6570\u636e\u7ecf\u8fc7\u4e09\u4e2a\u5c42 \u8fd4\u56desoftmax\u7ed3\u679c\u548chn<br \/>\n#   \u5f62\u72b6\u53d8\u5316 [seqlen,1,57],[1,1,128]) -&gt; [seqlen,1,128],[1,1,128]<\/p>\n<p># 3 \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n#   \u5f62\u72b6[self.num_layers, 1, self.hidden_size]<br \/>\nclass LSTM(nn.Module):<\/p>\n<p>    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1):<br \/>\n        super(LSTM, self).__init__()<br \/>\n        # 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n        self.input_size &#061; input_size<br \/>\n        self.hidden_size &#061; hidden_size<br \/>\n        self.output_size &#061; output_size<br \/>\n        self.num_layers &#061; num_layers<\/p>\n<p>        # \u5b9a\u4e49rnn\u5c42<br \/>\n        self.rnn &#061; nn.LSTM(self.input_size, self.hidden_size, self.num_layers)<\/p>\n<p>        # \u5b9a\u4e49linear\u5c42<br \/>\n        self.linear &#061; nn.Linear(self.hidden_size, self.output_size)<\/p>\n<p>        # \u5b9a\u4e49softmax\u5c42<br \/>\n        self.softmax &#061; nn.LogSoftmax(dim&#061;-1)<\/p>\n<p>    def forward(self, input, hidden, c):<br \/>\n        # \u8ba9\u6570\u636e\u7ecf\u8fc7\u4e09\u4e2a\u5c42 \u8fd4\u56desoftmax\u7ed3\u679c\u548c hn c<br \/>\n        # \u6570\u636e\u5f62\u72b6 [6,57] -&gt; [6,1,52]<br \/>\n        input &#061; input.unsqueeze(1)<br \/>\n        # \u628a\u6570\u636e\u9001\u7ed9\u6a21\u578b \u63d0\u53d6\u4e8b\u7269\u7279\u5f81<br \/>\n        # \u6570\u636e\u5f62\u72b6 [seqlen,1,57],[1,1,128], [1,1,128]) -&gt; [seqlen,1,18],[1,1,128],[1,1,128]<br \/>\n        rr, (hn, cn) &#061; self.rnn(input, (hidden, c))<br \/>\n        # \u6570\u636e\u5f62\u72b6 [seqlen,1,128] &#8211; [1, 128]<br \/>\n        tmprr &#061; rr[-1]<\/p>\n<p>        tmprr &#061; self.linear(tmprr)<\/p>\n<p>        return self.softmax(tmprr), hn, cn<\/p>\n<p>    def inithidden(self):<br \/>\n        # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n        hidden &#061; c &#061; torch.zeros(self.num_layers, 1, self.hidden_size)<br \/>\n        return hidden, c<\/p>\n<h5 id=\"3-gru\">3 \u6784\u5efaGRU\u6a21\u578b<\/h5>\n<p># GRU\u7c7b \u5b9e\u73b0\u601d\u8def\u5206\u6790&#xff1a;<br \/>\n# 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n#    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1)<\/p>\n<p># 2 forward(input, hidden)\u51fd\u6570<br \/>\n#   \u8ba9\u6570\u636e\u7ecf\u8fc7\u4e09\u4e2a\u5c42 \u8fd4\u56desoftmax\u7ed3\u679c\u548chn<br \/>\n#   \u5f62\u72b6\u53d8\u5316 [seqlen,1,57],[1,1,128]) -&gt; [seqlen,1,128],[1,1,128]<\/p>\n<p># 3 \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n#   \u5f62\u72b6[self.num_layers, 1, self.hidden_size]<br \/>\nclass GRU(nn.Module):<\/p>\n<p>    def __init__(self, input_size, hidden_size, output_size, num_layers&#061;1):<br \/>\n        super(GRU, self).__init__()<br \/>\n        # 1 init\u51fd\u6570 \u51c6\u5907\u4e09\u4e2a\u5c42 self.rnn self.linear self.softmax&#061;nn.LogSoftmax(dim&#061;-1)<br \/>\n        self.input_size &#061; input_size<br \/>\n        self.hidden_size &#061; hidden_size<br \/>\n        self.output_size &#061; output_size<br \/>\n        self.num_layers &#061; num_layers<\/p>\n<p>        # \u5b9a\u4e49rnn\u5c42<br \/>\n        self.rnn &#061; nn.GRU(self.input_size, self.hidden_size, self.num_layers)<\/p>\n<p>        # \u5b9a\u4e49linear\u5c42<br \/>\n        self.linear &#061; nn.Linear(self.hidden_size, self.output_size)<\/p>\n<p>        # \u5b9a\u4e49softmax\u5c42<br \/>\n        self.softmax &#061; nn.LogSoftmax(dim&#061;-1)<\/p>\n<p>    def forward(self, input, hidden):<br \/>\n        # \u8ba9\u6570\u636e\u7ecf\u8fc7\u4e09\u4e2a\u5c42 \u8fd4\u56desoftmax\u7ed3\u679c\u548chn<br \/>\n        # \u6570\u636e\u5f62\u72b6 [6,57] -&gt; [6,1,52]<br \/>\n        input &#061; input.unsqueeze(1)<br \/>\n        # \u628a\u6570\u636e\u9001\u7ed9\u6a21\u578b \u63d0\u53d6\u4e8b\u7269\u7279\u5f81<br \/>\n        # \u6570\u636e\u5f62\u72b6 [seqlen,1,57],[1,1,128]) -&gt; [seqlen,1,18],[1,1,128]<br \/>\n        rr, hn &#061; self.rnn(input, hidden)<br \/>\n        # \u6570\u636e\u5f62\u72b6 [seqlen,1,128] &#8211; [1, 128]<br \/>\n        tmprr &#061; rr[-1]<\/p>\n<p>        tmprr &#061; self.linear(tmprr)<\/p>\n<p>        return self.softmax(tmprr), hn<\/p>\n<p>    def inithidden(self):<br \/>\n        # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u8f93\u5165\u6570\u636e inithidden()<br \/>\n        return torch.zeros(self.num_layers, 1,self.hidden_size)<\/p>\n<h5 id=\"4-rnn_lstm_gru\">4 \u6a21\u578bRNN_LSTM_GRU\u6d4b\u8bd5<\/h5>\n<p>def dm_test_rnn_lstm_gru():<\/p>\n<p>    # one-hot\u7f16\u7801\u7279\u5f8157&#xff08;n_letters&#xff09;&#xff0c;\u4e5f\u662fRNN\u7684\u8f93\u5165\u5c3a\u5bf8<br \/>\n    input_size &#061; 57<\/p>\n<p>    # \u5b9a\u4e49\u9690\u5c42\u7684\u6700\u540e\u4e00\u7ef4\u5c3a\u5bf8\u5927\u5c0f<br \/>\n    n_hidden &#061; 128<\/p>\n<p>    # \u8f93\u51fa\u5c3a\u5bf8\u4e3a\u8bed\u8a00\u7c7b\u522b\u603b\u6570n_categories # 1\u4e2a\u5b57\u7b26\u9884\u6d4b\u621018\u4e2a\u7c7b\u522b<br \/>\n    output_size &#061; 18<\/p>\n<p>    # 1 \u83b7\u53d6\u6570\u636e<br \/>\n    myfilename &#061; &#039;.\/data\/name_classfication.txt&#039;<br \/>\n    my_list_x, my_list_y  &#061; read_data(myfilename)<br \/>\n    print(&#039;categorys&#8212;&gt;&#039;, categorys)<\/p>\n<p>    # 2 \u5b9e\u4f8b\u5316dataset\u5bf9\u8c61<br \/>\n    nameclassdataset &#061; NameClassDataset(my_list_x, my_list_y)<\/p>\n<p>    # 3 \u5b9e\u4f8b\u5316dataloader<br \/>\n    mydataloader &#061; DataLoader(dataset&#061;nameclassdataset, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p>    my_rnn &#061; RNN(n_letters, n_hidden, categorynum)<br \/>\n    my_lstm &#061; LSTM(n_letters, n_hidden, categorynum)<br \/>\n    my_gru &#061; GRU(n_letters, n_hidden, categorynum)<\/p>\n<p>    print(&#039;rnn \u6a21\u578b&#039;, my_rnn)<br \/>\n    print(&#039;lstm \u6a21\u578b&#039;, my_lstm)<br \/>\n    print(&#039;gru \u6a21\u578b&#039;, my_gru)<\/p>\n<p>    for  i, (x, y) in enumerate (mydataloader):<br \/>\n        # print(&#039;x.shape&#039;, x.shape, x)<br \/>\n        # print(&#039;y.shape&#039;, y.shape, y)<br \/>\n        # \u521d\u59cb\u5316\u4e00\u4e2a\u4e09\u7ef4\u7684\u9690\u5c420\u5f20\u91cf, \u4e5f\u662f\u521d\u59cb\u7684\u7ec6\u80de\u72b6\u6001\u5f20\u91cf<br \/>\n        output, hidden &#061; my_rnn(x[0], my_rnn.inithidden())<br \/>\n        print(&#034;rnn output.shape&#8212;&gt;:&#034;, output.shape, output)<br \/>\n        if (i &#061;&#061; 0):<br \/>\n            break<\/p>\n<p>    for  i, (x, y) in enumerate (mydataloader):<br \/>\n        # print(&#039;x.shape&#039;, x.shape, x)<br \/>\n        # print(&#039;y.shape&#039;, y.shape, y)<br \/>\n        hidden, c &#061; my_lstm.inithidden()<br \/>\n        output, hidden, c &#061; my_lstm(x[0], hidden, c)<br \/>\n        print(&#034;lstm output.shape&#8212;&gt;:&#034;, output.shape, output)<br \/>\n        if (i &#061;&#061; 0):<br \/>\n            break<\/p>\n<p>    for  i, (x, y) in enumerate (mydataloader):<br \/>\n        # print(&#039;x.shape&#039;, x.shape, x)<br \/>\n        # print(&#039;y.shape&#039;, y.shape, y)<br \/>\n        output, hidden &#061; my_gru(x[0], my_gru.inithidden())<br \/>\n        print(&#034;gru output.shape&#8212;&gt;:&#034;, output.shape, output)<br \/>\n        if (i &#061;&#061; 0):<br \/>\n            break<\/p>\n<ul>\n<li>\u8f93\u51fa\u6548\u679c:<\/li>\n<\/ul>\n<p>rnn \u6a21\u578b RNN(<br \/>\n  (rnn): RNN(57, 128)<br \/>\n  (linear): Linear(in_features&#061;128, out_features&#061;18, bias&#061;True)<br \/>\n  (softmax): LogSoftmax(dim&#061;-1)<br \/>\n)<br \/>\nlstm \u6a21\u578b LSTM(<br \/>\n  (rnn): LSTM(57, 128)<br \/>\n  (linear): Linear(in_features&#061;128, out_features&#061;18, bias&#061;True)<br \/>\n  (softmax): LogSoftmax(dim&#061;-1)<br \/>\n)<br \/>\ngru \u6a21\u578b GRU(<br \/>\n  (rnn): GRU(57, 128)<br \/>\n  (linear): Linear(in_features&#061;128, out_features&#061;18, bias&#061;True)<br \/>\n  (softmax): LogSoftmax(dim&#061;-1)<br \/>\n)<br \/>\nrnn output.shape&#8212;&gt;: torch.Size([1, 18]) tensor([[-2.9552, -2.9024, -2.8828, -2.7737, -2.8387, -3.0154, -2.8587, -2.9567,<br \/>\n         -2.8406, -3.0098, -2.8152, -2.8472, -2.9561, -2.8780, -2.8332, -2.8117,<br \/>\n         -2.9560, -2.9384]], grad_fn&#061;&lt;LogSoftmaxBackward0&gt;)<br \/>\nlstm output.shape&#8212;&gt;: torch.Size([1, 18]) tensor([[-2.9283, -3.0017, -2.8902, -2.8179, -2.8484, -2.8152, -2.9654, -2.8846,<br \/>\n         -2.8642, -2.8602, -2.8860, -2.9505, -2.8806, -2.9436, -2.8388, -2.9312,<br \/>\n         -2.9241, -2.8211]], grad_fn&#061;&lt;LogSoftmaxBackward0&gt;)<br \/>\ngru output.shape&#8212;&gt;: torch.Size([1, 18]) tensor([[-2.8898, -3.0236, -2.7403, -2.8986, -2.8163, -2.9486, -2.8674, -2.9294,<br \/>\n         -2.8889, -3.0082, -2.8785, -2.8741, -2.8736, -2.7923, -2.9261, -2.8990,<br \/>\n         -2.9456, -2.8668]], grad_fn&#061;&lt;LogSoftmaxBackward0&gt;)<\/p>\n<h4 id=\"24\">2.4 \u6784\u5efa\u8bad\u7ec3\u51fd\u6570\u5e76\u8fdb\u884c\u8bad\u7ec3<\/h4>\n<h5 id=\"1-rnn_1\">1 \u6784\u5efaRNN\u8bad\u7ec3\u51fd\u6570<\/h5>\n<p># \u601d\u8def\u5206\u6790<br \/>\n# \u4ece\u6587\u4ef6\u83b7\u53d6\u6570\u636e\u3001\u5b9e\u4f8b\u5316\u6570\u636e\u6e90\u5bf9\u8c61nameclassdataset \u6570\u636e\u8fed\u4ee3\u5668\u5bf9\u8c61mydataloader<br \/>\n# \u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61my_rnn \u635f\u5931\u51fd\u6570\u5bf9\u8c61mycrossentropyloss&#061;nn.NLLLoss() \u4f18\u5316\u5668\u5bf9\u8c61myadam<br \/>\n# \u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u7684\u53c2\u6570<br \/>\n#       starttime total_iter_num total_loss  total_loss_list total_acc_num  total_acc_list<br \/>\n# \u5916\u5c42for\u5faa\u73af \u63a7\u5236\u8f6e\u6570 for epoch_idx in range(epochs)<br \/>\n# \u5185\u5c42for\u5faa\u73af \u63a7\u5236\u8fed\u4ee3\u6b21\u6570 for i, (x, y) in enumerate(mydataloader)<br \/>\n    # \u7ed9\u6a21\u578b\u5582\u6570\u636e   # \u8ba1\u7b97\u635f\u5931  # \u68af\u5ea6\u6e05\u96f6 # \u53cd\u5411\u4f20\u64ad  # \u68af\u5ea6\u66f4\u65b0<br \/>\n    # \u8ba1\u7b97\u8f85\u52a9\u4fe1\u606f   # \u7d2f\u52a0\u603b\u635f\u5931\u548c\u51c6\u786e\u6570 \u6bcf100\u6b21\u8bad\u7ec3\u8ba1\u7b97\u4e00\u4e2a\u603b\u4f53\u5e73\u5747\u635f\u5931 \u603b\u4f53\u5e73\u5747\u51c6\u786e\u7387 \u6bcf2000\u6b21\u8bad\u7ec3 \u6253\u5370\u65e5\u5fd7<br \/>\n    # \u5176\u4ed6          # \u9884\u6d4b\u5bf9\u9519 i_predit_tag &#061; (1 if torch.argmax(output).item() &#061;&#061; y.item() else 0)<\/p>\n<p># \u6a21\u578b\u4fdd\u5b58<br \/>\n    # torch.save(my_rnn.state_dict(), &#039;.\/my_rnn_model_%d.bin&#039; % (epoch_idx &#043; 1))<br \/>\n# \u8fd4\u56de \u5e73\u5747\u635f\u5931\u5217\u8868total_loss_list, \u65f6\u95f4total_time, \u5e73\u5747\u51c6\u786etotal_acc_list<\/p>\n<p># \u6a21\u578b\u8bad\u7ec3\u53c2\u6570<br \/>\nmylr &#061; 1e-3<br \/>\nepochs &#061; 1<\/p>\n<p>def my_train_rnn():<\/p>\n<p>    # \u83b7\u53d6\u6570\u636e<br \/>\n    myfilename &#061; &#039;.\/data\/name_classfication.txt&#039;<br \/>\n    my_list_x, my_list_y &#061; read_data(myfilename)<\/p>\n<p>    # \u5b9e\u4f8b\u5316dataset\u5bf9\u8c61<br \/>\n    nameclassdataset &#061; NameClassDataset(my_list_x, my_list_y)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u6a21\u578b<br \/>\n    input_size &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    output_size &#061; 18<br \/>\n    my_rnn &#061; RNN(input_size, n_hidden, output_size)<br \/>\n    print(&#039;my_rnn\u6a21\u578b&#8212;&gt;&#039;, my_rnn)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u635f\u5931\u51fd\u6570 adam\u4f18\u5316\u5668<br \/>\n    mycrossentropyloss &#061; nn.NLLLoss()<br \/>\n    myadam &#061; optim.Adam(my_rnn.parameters(), lr&#061;mylr)<\/p>\n<p>    # \u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u53c2\u6570<br \/>\n    starttime &#061; time.time()<br \/>\n    total_iter_num &#061; 0  # \u5df2\u8bad\u7ec3\u7684\u6837\u672c\u6570<br \/>\n    total_loss &#061; 0.0  # \u5df2\u8bad\u7ec3\u7684\u635f\u5931\u548c<br \/>\n    total_loss_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5f62\u6210\u635f\u5931\u5217\u8868<br \/>\n    total_acc_num &#061; 0  # \u5df2\u8bad\u7ec3\u6837\u672c\u9884\u6d4b\u51c6\u786e\u603b\u6570<br \/>\n    total_acc_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u51c6\u786e\u7387 \u5f62\u6210\u5e73\u5747\u51c6\u786e\u7387\u5217\u8868<\/p>\n<p>    # \u5916\u5c42for\u5faa\u73af \u63a7\u5236\u8f6e\u6570<br \/>\n    for epoch_idx in range(epochs):<\/p>\n<p>        # \u5b9e\u4f8b\u5316dataloader<br \/>\n        mydataloader &#061; DataLoader(dataset&#061;nameclassdataset, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p>        # \u5185\u5c42for\u5faa\u73af \u63a7\u5236\u8fed\u4ee3\u6b21\u6570<br \/>\n        for i, (x, y) in enumerate(mydataloader):<br \/>\n            # \u7ed9\u6a21\u578b\u5582\u6570\u636e<br \/>\n            output, hidden &#061; my_rnn(x[0], my_rnn.inithidden())<\/p>\n<p>            # \u8ba1\u7b97\u635f\u5931<br \/>\n            myloss &#061; mycrossentropyloss(output, y)<\/p>\n<p>            # \u68af\u5ea6\u6e05\u96f6<br \/>\n            myadam.zero_grad()<\/p>\n<p>            # \u53cd\u5411\u4f20\u64ad<br \/>\n            myloss.backward()<\/p>\n<p>            # \u68af\u5ea6\u66f4\u65b0<br \/>\n            myadam.step()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u635f\u5931<br \/>\n            total_iter_num &#061; total_iter_num &#043; 1<br \/>\n            total_loss &#061; total_loss &#043; myloss.item()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u51c6\u786e\u7387<br \/>\n            i_predit_tag &#061; (1 if torch.argmax(output).item() &#061;&#061; y.item() else 0)<br \/>\n            total_acc_num &#061; total_acc_num &#043; i_predit_tag<\/p>\n<p>            # \u6bcf100\u6b21\u8bad\u7ec3 \u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5e73\u5747\u51c6\u786e\u7387<br \/>\n            if (total_iter_num % 100 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss\/total_iter_num<br \/>\n                total_loss_list.append(tmploss)<\/p>\n<p>                tmpacc &#061; total_acc_num\/total_iter_num<br \/>\n                total_acc_list.append(tmpacc)<\/p>\n<p>            # \u6bcf2000\u6b21\u8bad\u7ec3 \u6253\u5370\u65e5\u5fd7<br \/>\n            if (total_iter_num % 2000 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss \/ total_iter_num<br \/>\n                print(&#039;\u8f6e\u6b21:%d, \u635f\u5931:%.6f, \u65f6\u95f4:%d&#xff0c;\u51c6\u786e\u7387:%.3f&#039; %(epoch_idx&#043;1, tmploss, time.time() &#8211; starttime, tmpacc))<\/p>\n<p>        # \u6bcf\u4e2a\u8f6e\u6b21\u4fdd\u5b58\u6a21\u578b<br \/>\n        torch.save(my_rnn.state_dict(), &#039;.\/my_rnn_model_%d.bin&#039; % (epoch_idx &#043; 1))<\/p>\n<p>    # \u8ba1\u7b97\u603b\u65f6\u95f4<br \/>\n    total_time &#061; int(time.time() &#8211; starttime)<\/p>\n<p>    return total_loss_list, total_time, total_acc_list<\/p>\n<h5 id=\"2-lstm_1\">2 \u6784\u5efaLSTM\u8bad\u7ec3\u51fd\u6570<\/h5>\n<p># \u601d\u8def\u5206\u6790<br \/>\n# \u540cRNN\u5b9e\u73b0\u5206\u6790<\/p>\n<p>def my_train_lstm():<\/p>\n<p>    # \u83b7\u53d6\u6570\u636e<br \/>\n    myfilename &#061; &#039;.\/data\/name_classfication.txt&#039;<br \/>\n    my_list_x, my_list_y &#061; read_data(myfilename)<\/p>\n<p>    # \u5b9e\u4f8b\u5316dataset\u5bf9\u8c61<br \/>\n    nameclassdataset &#061; NameClassDataset(my_list_x, my_list_y)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u6a21\u578b<br \/>\n    input_size &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    output_size &#061; 18<br \/>\n    my_lstm &#061; LSTM(input_size, n_hidden, output_size)<br \/>\n    print(&#039;my_lstm\u6a21\u578b&#8212;&gt;&#039;, my_lstm)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u635f\u5931\u51fd\u6570 adam\u4f18\u5316\u5668<br \/>\n    mycrossentropyloss &#061; nn.NLLLoss()<br \/>\n    myadam &#061; optim.Adam(my_lstm.parameters(), lr&#061;mylr)<\/p>\n<p>    # \u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u53c2\u6570<br \/>\n    starttime &#061; time.time()<br \/>\n    total_iter_num &#061; 0  # \u5df2\u8bad\u7ec3\u7684\u6837\u672c\u6570<br \/>\n    total_loss &#061; 0.0  # \u5df2\u8bad\u7ec3\u7684\u635f\u5931\u548c<br \/>\n    total_loss_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5f62\u6210\u635f\u5931\u5217\u8868<br \/>\n    total_acc_num &#061; 0  # \u5df2\u8bad\u7ec3\u6837\u672c\u9884\u6d4b\u51c6\u786e\u603b\u6570<br \/>\n    total_acc_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u51c6\u786e\u7387 \u5f62\u6210\u5e73\u5747\u51c6\u786e\u7387\u5217\u8868<\/p>\n<p>    # \u5916\u5c42for\u5faa\u73af \u63a7\u5236\u8f6e\u6570<br \/>\n    for epoch_idx in range(epochs):<\/p>\n<p>        # \u5b9e\u4f8b\u5316dataloader<br \/>\n        mydataloader &#061; DataLoader(dataset&#061;nameclassdataset, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p>        # \u5185\u5c42for\u5faa\u73af \u63a7\u5236\u8fed\u4ee3\u6b21\u6570<br \/>\n        for i, (x, y) in enumerate(mydataloader):<br \/>\n            # \u7ed9\u6a21\u578b\u5582\u6570\u636e<br \/>\n            hidden, c &#061; my_lstm.inithidden()<br \/>\n            output, hidden, c &#061; my_lstm(x[0], hidden, c)<\/p>\n<p>            # \u8ba1\u7b97\u635f\u5931<br \/>\n            myloss &#061; mycrossentropyloss(output, y)<\/p>\n<p>            # \u68af\u5ea6\u6e05\u96f6<br \/>\n            myadam.zero_grad()<\/p>\n<p>            # \u53cd\u5411\u4f20\u64ad<br \/>\n            myloss.backward()<\/p>\n<p>            # \u68af\u5ea6\u66f4\u65b0<br \/>\n            myadam.step()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u635f\u5931<br \/>\n            total_iter_num &#061; total_iter_num &#043; 1<br \/>\n            total_loss &#061; total_loss &#043; myloss.item()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u51c6\u786e\u7387<br \/>\n            i_predit_tag &#061; (1 if torch.argmax(output).item() &#061;&#061; y.item() else 0)<br \/>\n            total_acc_num &#061; total_acc_num &#043; i_predit_tag<\/p>\n<p>            # \u6bcf100\u6b21\u8bad\u7ec3 \u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5e73\u5747\u51c6\u786e\u7387<br \/>\n            if (total_iter_num % 100 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss\/total_iter_num<br \/>\n                total_loss_list.append(tmploss)<\/p>\n<p>                tmpacc &#061; total_acc_num\/total_iter_num<br \/>\n                total_acc_list.append(tmpacc)<\/p>\n<p>            # \u6bcf2000\u6b21\u8bad\u7ec3 \u6253\u5370\u65e5\u5fd7<br \/>\n            if (total_iter_num % 2000 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss \/ total_iter_num<br \/>\n                print(&#039;\u8f6e\u6b21:%d, \u635f\u5931:%.6f, \u65f6\u95f4:%d&#xff0c;\u51c6\u786e\u7387:%.3f&#039; %(epoch_idx&#043;1, tmploss, time.time() &#8211; starttime, tmpacc))<\/p>\n<p>        # \u6bcf\u4e2a\u8f6e\u6b21\u4fdd\u5b58\u6a21\u578b<br \/>\n        torch.save(my_lstm.state_dict(), &#039;.\/my_lstm_model_%d.bin&#039; % (epoch_idx &#043; 1))<\/p>\n<p>    # \u8ba1\u7b97\u603b\u65f6\u95f4<br \/>\n    total_time &#061; int(time.time() &#8211; starttime)<\/p>\n<p>    return total_loss_list, total_time, total_acc_list<\/p>\n<h5 id=\"3-gru_1\">3 \u6784\u5efaGRU\u8bad\u7ec3\u51fd\u6570<\/h5>\n<p># \u601d\u8def\u5206\u6790<br \/>\n# \u540cRNN\u5b9e\u73b0\u5206\u6790<\/p>\n<p>def my_train_gru():<\/p>\n<p>    # \u83b7\u53d6\u6570\u636e<br \/>\n    myfilename &#061; &#039;.\/data\/name_classfication.txt&#039;<br \/>\n    my_list_x, my_list_y &#061; read_data(myfilename)<\/p>\n<p>    # \u5b9e\u4f8b\u5316dataset\u5bf9\u8c61<br \/>\n    nameclassdataset &#061; NameClassDataset(my_list_x, my_list_y)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u6a21\u578b<br \/>\n    input_size &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    output_size &#061; 18<br \/>\n    my_gru &#061; GRU(input_size, n_hidden, output_size)<br \/>\n    print(&#039;my_gru\u6a21\u578b&#8212;&gt;&#039;, my_gru)<\/p>\n<p>    # \u5b9e\u4f8b\u5316 \u635f\u5931\u51fd\u6570 adam\u4f18\u5316\u5668<br \/>\n    mycrossentropyloss &#061; nn.NLLLoss()<br \/>\n    myadam &#061; optim.Adam(my_gru.parameters(), lr&#061;mylr)<\/p>\n<p>    # \u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u53c2\u6570<br \/>\n    starttime &#061; time.time()<br \/>\n    total_iter_num &#061; 0  # \u5df2\u8bad\u7ec3\u7684\u6837\u672c\u6570<br \/>\n    total_loss &#061; 0.0  # \u5df2\u8bad\u7ec3\u7684\u635f\u5931\u548c<br \/>\n    total_loss_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5f62\u6210\u635f\u5931\u5217\u8868<br \/>\n    total_acc_num &#061; 0  # \u5df2\u8bad\u7ec3\u6837\u672c\u9884\u6d4b\u51c6\u786e\u603b\u6570<br \/>\n    total_acc_list &#061; []  # \u6bcf100\u4e2a\u6837\u672c\u6c42\u4e00\u6b21\u5e73\u5747\u51c6\u786e\u7387 \u5f62\u6210\u5e73\u5747\u51c6\u786e\u7387\u5217\u8868<\/p>\n<p>    # \u5916\u5c42for\u5faa\u73af \u63a7\u5236\u8f6e\u6570<br \/>\n    for epoch_idx in range(epochs):<\/p>\n<p>        # \u5b9e\u4f8b\u5316dataloader<br \/>\n        mydataloader &#061; DataLoader(dataset&#061;nameclassdataset, batch_size&#061;1, shuffle&#061;True)<\/p>\n<p>        # \u5185\u5c42for\u5faa\u73af \u63a7\u5236\u8fed\u4ee3\u6b21\u6570<br \/>\n        for i, (x, y) in enumerate(mydataloader):<br \/>\n            # \u7ed9\u6a21\u578b\u5582\u6570\u636e<br \/>\n            output, hidden &#061; my_gru(x[0], my_gru.inithidden())<\/p>\n<p>            # \u8ba1\u7b97\u635f\u5931<br \/>\n            myloss &#061; mycrossentropyloss(output, y)<\/p>\n<p>            # \u68af\u5ea6\u6e05\u96f6<br \/>\n            myadam.zero_grad()<\/p>\n<p>            # \u53cd\u5411\u4f20\u64ad<br \/>\n            myloss.backward()<\/p>\n<p>            # \u68af\u5ea6\u66f4\u65b0<br \/>\n            myadam.step()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u635f\u5931<br \/>\n            total_iter_num &#061; total_iter_num &#043; 1<br \/>\n            total_loss &#061; total_loss &#043; myloss.item()<\/p>\n<p>            # \u8ba1\u7b97\u603b\u51c6\u786e\u7387<br \/>\n            i_predit_tag &#061; (1 if torch.argmax(output).item() &#061;&#061; y.item() else 0)<br \/>\n            total_acc_num &#061; total_acc_num &#043; i_predit_tag<\/p>\n<p>            # \u6bcf100\u6b21\u8bad\u7ec3 \u6c42\u4e00\u6b21\u5e73\u5747\u635f\u5931 \u5e73\u5747\u51c6\u786e\u7387<br \/>\n            if (total_iter_num % 100 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss\/total_iter_num<br \/>\n                total_loss_list.append(tmploss)<\/p>\n<p>                tmpacc &#061; total_acc_num\/total_iter_num<br \/>\n                total_acc_list.append(tmpacc)<\/p>\n<p>            # \u6bcf2000\u6b21\u8bad\u7ec3 \u6253\u5370\u65e5\u5fd7<br \/>\n            if (total_iter_num % 2000 &#061;&#061; 0):<br \/>\n                tmploss &#061; total_loss \/ total_iter_num<br \/>\n                print(&#039;\u8f6e\u6b21:%d, \u635f\u5931:%.6f, \u65f6\u95f4:%d&#xff0c;\u51c6\u786e\u7387:%.3f&#039; %(epoch_idx&#043;1, tmploss, time.time() &#8211; starttime, tmpacc))<\/p>\n<p>        # \u6bcf\u4e2a\u8f6e\u6b21\u4fdd\u5b58\u6a21\u578b<br \/>\n        torch.save(my_gru.state_dict(), &#039;.\/my_gru_model_%d.bin&#039; % (epoch_idx &#043; 1))<\/p>\n<p>    # \u8ba1\u7b97\u603b\u65f6\u95f4<br \/>\n    total_time &#061; int(time.time() &#8211; starttime)<\/p>\n<p>    return total_loss_list, total_time, total_acc_list<\/p>\n<h5 id=\"4\">4 \u6a21\u578b\u8bad\u7ec3\u5e76\u5236\u56fe<\/h5>\n<p>def dm_test_train_rnn_lstm_gru():<br \/>\n    total_loss_list_rnn, total_time_rnn, total_acc_list_rnn &#061; my_train_rnn()<\/p>\n<p>    total_loss_list_lstm, total_time_lstm, total_acc_list_lstm &#061; my_train_lstm()<\/p>\n<p>    total_loss_list_gru, total_time_gru, total_acc_list_gru &#061; my_train_gru()<\/p>\n<p>    # \u7ed8\u5236\u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf<br \/>\n    # \u521b\u5efa\u753b\u5e030<br \/>\n    plt.figure(0)<br \/>\n    # # \u7ed8\u5236\u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf<br \/>\n    plt.plot(total_loss_list_rnn, label&#061;&#034;RNN&#034;)<br \/>\n    plt.plot(total_loss_list_lstm, color&#061;&#034;red&#034;, label&#061;&#034;LSTM&#034;)<br \/>\n    plt.plot(total_loss_list_gru, color&#061;&#034;orange&#034;, label&#061;&#034;GRU&#034;)<br \/>\n    plt.legend(loc&#061;&#039;upper left&#039;)<br \/>\n    plt.savefig(&#039;.\/img\/RNN_LSTM_GRU_loss2.png&#039;)<br \/>\n    plt.show()<\/p>\n<p>    # \u7ed8\u5236\u67f1\u72b6\u56fe<br \/>\n    # \u521b\u5efa\u753b\u5e031<br \/>\n    plt.figure(1)<br \/>\n    x_data &#061; [&#034;RNN&#034;, &#034;LSTM&#034;, &#034;GRU&#034;]<br \/>\n    y_data &#061; [total_time_rnn, total_time_lstm, total_time_gru]<br \/>\n    # \u7ed8\u5236\u8bad\u7ec3\u8017\u65f6\u5bf9\u6bd4\u67f1\u72b6\u56fe<br \/>\n    plt.bar(range(len(x_data)), y_data, tick_label&#061;x_data)<br \/>\n    plt.savefig(&#039;.\/img\/RNN_LSTM_GRU_period2.png&#039;)<br \/>\n    plt.show()<\/p>\n<p>    # \u7ed8\u5236\u51c6\u786e\u7387\u5bf9\u6bd4\u66f2\u7ebf<br \/>\n    plt.figure(2)<br \/>\n    plt.plot(total_acc_list_rnn, label&#061;&#034;RNN&#034;)<br \/>\n    plt.plot(total_acc_list_lstm, color&#061;&#034;red&#034;, label&#061;&#034;LSTM&#034;)<br \/>\n    plt.plot(total_acc_list_gru, color&#061;&#034;orange&#034;, label&#061;&#034;GRU&#034;)<br \/>\n    plt.legend(loc&#061;&#039;upper left&#039;)<br \/>\n    plt.savefig(&#039;.\/img\/RNN_LSTM_GRU_acc2.png&#039;)<br \/>\n    plt.show()<\/p>\n<ul>\n<li>RNN\u6a21\u578b\u8bad\u7ec3\u65e5\u5fd7\u8f93\u51fa:<\/li>\n<\/ul>\n<p>\u8f6e\u6b21:3, \u635f\u5931:1.002102, \u65f6\u95f4:54&#xff0c;\u51c6\u786e\u7387:0.700<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.993880, \u65f6\u95f4:56&#xff0c;\u51c6\u786e\u7387:0.703<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.986200, \u65f6\u95f4:58&#xff0c;\u51c6\u786e\u7387:0.705<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.981136, \u65f6\u95f4:61&#xff0c;\u51c6\u786e\u7387:0.706<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.976931, \u65f6\u95f4:63&#xff0c;\u51c6\u786e\u7387:0.707<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.972190, \u65f6\u95f4:65&#xff0c;\u51c6\u786e\u7387:0.708<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.967081, \u65f6\u95f4:68&#xff0c;\u51c6\u786e\u7387:0.710<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.964384, \u65f6\u95f4:70&#xff0c;\u51c6\u786e\u7387:0.711<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.958782, \u65f6\u95f4:72&#xff0c;\u51c6\u786e\u7387:0.713<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.955343, \u65f6\u95f4:75&#xff0c;\u51c6\u786e\u7387:0.713<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.950741, \u65f6\u95f4:77&#xff0c;\u51c6\u786e\u7387:0.715<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.945756, \u65f6\u95f4:80&#xff0c;\u51c6\u786e\u7387:0.716<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.942663, \u65f6\u95f4:82&#xff0c;\u51c6\u786e\u7387:0.717<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.939319, \u65f6\u95f4:84&#xff0c;\u51c6\u786e\u7387:0.718<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.936169, \u65f6\u95f4:87&#xff0c;\u51c6\u786e\u7387:0.719<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.933440, \u65f6\u95f4:89&#xff0c;\u51c6\u786e\u7387:0.720<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.930918, \u65f6\u95f4:91&#xff0c;\u51c6\u786e\u7387:0.720<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.927330, \u65f6\u95f4:94&#xff0c;\u51c6\u786e\u7387:0.721<\/p>\n<ul>\n<li>LSTM\u6a21\u578b\u8bad\u7ec3\u65e5\u5fd7\u8f93\u51fa:<\/li>\n<\/ul>\n<p>\u8f6e\u6b21:3, \u635f\u5931:0.805885, \u65f6\u95f4:118&#xff0c;\u51c6\u786e\u7387:0.759<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.794148, \u65f6\u95f4:123&#xff0c;\u51c6\u786e\u7387:0.762<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.783356, \u65f6\u95f4:128&#xff0c;\u51c6\u786e\u7387:0.765<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.774931, \u65f6\u95f4:133&#xff0c;\u51c6\u786e\u7387:0.767<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.765427, \u65f6\u95f4:137&#xff0c;\u51c6\u786e\u7387:0.769<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.757254, \u65f6\u95f4:142&#xff0c;\u51c6\u786e\u7387:0.771<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.750375, \u65f6\u95f4:147&#xff0c;\u51c6\u786e\u7387:0.773<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.743092, \u65f6\u95f4:152&#xff0c;\u51c6\u786e\u7387:0.775<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.732983, \u65f6\u95f4:157&#xff0c;\u51c6\u786e\u7387:0.778<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.723816, \u65f6\u95f4:162&#xff0c;\u51c6\u786e\u7387:0.780<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.716507, \u65f6\u95f4:167&#xff0c;\u51c6\u786e\u7387:0.782<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.708377, \u65f6\u95f4:172&#xff0c;\u51c6\u786e\u7387:0.785<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.700820, \u65f6\u95f4:177&#xff0c;\u51c6\u786e\u7387:0.787<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.694714, \u65f6\u95f4:182&#xff0c;\u51c6\u786e\u7387:0.788<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.688386, \u65f6\u95f4:187&#xff0c;\u51c6\u786e\u7387:0.790<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.683056, \u65f6\u95f4:191&#xff0c;\u51c6\u786e\u7387:0.791<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.677051, \u65f6\u95f4:196&#xff0c;\u51c6\u786e\u7387:0.793<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.671668, \u65f6\u95f4:201&#xff0c;\u51c6\u786e\u7387:0.794<\/p>\n<ul>\n<li>GRU\u6a21\u578b\u8bad\u7ec3\u65e5\u5fd7\u8f93\u51fa:<\/li>\n<\/ul>\n<p>\u8f6e\u6b21:3, \u635f\u5931:0.743891, \u65f6\u95f4:106&#xff0c;\u51c6\u786e\u7387:0.772<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.733144, \u65f6\u95f4:111&#xff0c;\u51c6\u786e\u7387:0.775<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.723484, \u65f6\u95f4:116&#xff0c;\u51c6\u786e\u7387:0.777<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.714760, \u65f6\u95f4:120&#xff0c;\u51c6\u786e\u7387:0.780<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.706929, \u65f6\u95f4:125&#xff0c;\u51c6\u786e\u7387:0.782<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.698657, \u65f6\u95f4:130&#xff0c;\u51c6\u786e\u7387:0.784<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.690443, \u65f6\u95f4:134&#xff0c;\u51c6\u786e\u7387:0.787<br \/>\n\u8f6e\u6b21:3, \u635f\u5931:0.683878, \u65f6\u95f4:139&#xff0c;\u51c6\u786e\u7387:0.789<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.674766, \u65f6\u95f4:144&#xff0c;\u51c6\u786e\u7387:0.791<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.665543, \u65f6\u95f4:148&#xff0c;\u51c6\u786e\u7387:0.794<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.657179, \u65f6\u95f4:153&#xff0c;\u51c6\u786e\u7387:0.796<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.650314, \u65f6\u95f4:157&#xff0c;\u51c6\u786e\u7387:0.798<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.643698, \u65f6\u95f4:162&#xff0c;\u51c6\u786e\u7387:0.800<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.637341, \u65f6\u95f4:167&#xff0c;\u51c6\u786e\u7387:0.802<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.632063, \u65f6\u95f4:171&#xff0c;\u51c6\u786e\u7387:0.803<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.626060, \u65f6\u95f4:176&#xff0c;\u51c6\u786e\u7387:0.805<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.621460, \u65f6\u95f4:180&#xff0c;\u51c6\u786e\u7387:0.806<br \/>\n\u8f6e\u6b21:4, \u635f\u5931:0.616704, \u65f6\u95f4:185&#xff0c;\u51c6\u786e\u7387:0.808<\/p>\n<h5 id=\"5_1\">5 \u6a21\u578b\u8bad\u7ec3\u7ed3\u679c\u5206\u6790<\/h5>\n<h6 id=\"1_2\">1 \u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf\u5206\u6790<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"824\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811120921-6899dd71eb2b1.png\" width=\"2180\" \/><\/h6>\n<ul>\n<li>\u5de6\u56fe&#xff1a;1\u4e2a\u8f6e\u6b21\u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf&#xff0c;\u53f3\u56fe4\u4e2a\u8f6e\u6b21\u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf<\/li>\n<li>\u6a21\u578b\u8bad\u7ec3\u7684\u635f\u5931\u964d\u4f4e\u5feb\u6162\u4ee3\u8868\u6a21\u578b\u6536\u655b\u7a0b\u5ea6\u3002\u7531\u56fe\u53ef\u77e5, \u4f20\u7edfRNN\u7684\u6a21\u578b\u7b2c\u4e00\u4e2a\u8f6e\u6b21\u5f00\u59cb\u6536\u655b\u60c5\u51b5\u6700\u597d&#xff0c;\u7136\u540e\u662fGRU, \u6700\u540e\u662fLSTM, \u8fd9\u662f\u56e0\u4e3aRNN\u6a21\u578b\u7b80\u5355\u53c2\u6570\u5c11&#xff0c;\u89c1\u6548\u5feb\u3002\u968f\u7740\u8bad\u7ec3\u6570\u636e\u7684\u589e\u52a0&#xff0c;GRU\u6548\u679c\u6700\u597d\u3001LSTM\u6548\u679c\u6b21\u4e4b\u3001RNN\u6548\u679c\u6392\u6700\u540e\u3002<\/li>\n<li>\u6240\u4ee5\u5728\u4ee5\u540e\u7684\u6a21\u578b\u9009\u7528\u65f6&#xff0c; \u8981\u901a\u8fc7\u5bf9\u4efb\u52a1\u7684\u5206\u6790\u4ee5\u53ca\u5b9e\u9a8c\u5bf9\u6bd4, \u9009\u62e9\u6700\u9002\u5408\u7684\u6a21\u578b\u3002<\/li>\n<\/ul>\n<h6 id=\"2_2\">2 \u8bad\u7ec3\u8017\u65f6\u5206\u6790<\/h6>\n<p>\u8bad\u7ec3\u8017\u65f6\u5bf9\u6bd4\u56fe:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"480\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811120934-6899dd7e2a661.png\" width=\"640\" \/><\/p>\n<ul>\n<li>\u6a21\u578b\u8bad\u7ec3\u7684\u8017\u65f6\u957f\u77ed\u4ee3\u8868\u6a21\u578b\u7684\u8ba1\u7b97\u590d\u6742\u5ea6&#xff0c;\u7531\u56fe\u53ef\u77e5&#xff0c; \u4e5f\u6b63\u5982\u6211\u4eec\u4e4b\u524d\u7684\u7406\u8bba\u5206\u6790&#xff0c;\u4f20\u7edfRNN\u590d\u6742\u5ea6\u6700\u4f4e&#xff0c; \u8017\u65f6\u51e0\u4e4e\u53ea\u662f\u540e\u4e24\u8005\u7684\u4e00\u534a, \u7136\u540e\u662fGRU&#xff0c;\u6700\u540e\u662f\u590d\u6742\u5ea6\u6700\u9ad8\u7684LSTM\u3002<\/li>\n<\/ul>\n<h6 id=\"3_1\">3 \u8bad\u7ec3\u51c6\u786e\u7387\u5206\u6790<\/h6>\n<p>\u8bad\u7ec3\u51c6\u786e\u7387\u5bf9\u6bd4\u56fe:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"480\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811120934-6899dd7ed79f8.png\" width=\"640\" \/><\/p>\n<ul>\n<li>\u7531\u56fe\u53ef\u77e5&#xff0c; GRU\u6548\u679c\u6700\u597d\u3001LSTM\u6548\u679c\u6b21\u4e4b\u3001RNN\u6548\u679c\u6392\u6700\u540e\u3002<\/li>\n<\/ul>\n<h6 id=\"4_1\">4 \u7ed3\u8bba<\/h6>\n<p>\u6a21\u578b\u9009\u7528\u4e00\u822c\u5e94\u901a\u8fc7\u5b9e\u9a8c\u5bf9\u6bd4&#xff0c;\u5e76\u975e\u8d8a\u590d\u6742\u6216\u8d8a\u5148\u8fdb\u7684\u6a21\u578b\u8868\u73b0\u8d8a\u597d&#xff0c;\u800c\u662f\u9700\u8981\u7ed3\u5408\u81ea\u5df1\u7684\u7279\u5b9a\u4efb\u52a1&#xff0c;\u4ece\u5bf9\u6570\u636e\u7684\u5206\u6790\u548c\u5b9e\u9a8c\u7ed3\u679c\u4e2d\u83b7\u5f97\u6700\u4f73\u7b54\u6848\u3002<\/p>\n<h4 id=\"25\">2.5 \u6784\u5efa\u9884\u6d4b\u51fd\u6570\u5e76\u8fdb\u884c\u9884\u6d4b<\/h4>\n<h5 id=\"1-rnn_2\">1 \u6784\u5efaRNN\u9884\u6d4b\u51fd\u6570<\/h5>\n<p># 1 \u6784\u5efa\u4f20\u7edfRNN\u9884\u6d4b\u51fd\u6570<br \/>\nmy_path_rnn &#061; &#039;.\/model\/my_rnn_model_1.bin&#039;<br \/>\nmy_path_lstm &#061; &#039;.\/model\/my_lstm_model_1.bin&#039;<br \/>\nmy_path_gru &#061; &#039;.\/model\/my_gru_model_1.bin&#039;<\/p>\n<p># \u5c06\u4eba\u540d\u8f6c\u5316\u4e3aonehot\u5f20\u91cf<br \/>\n# eg &#039;bai&#039; &#8211;&gt; [3,57]<br \/>\ndef lineToTensor(x):<br \/>\n    # \u6587\u672c\u5f20\u91cf\u5316x<br \/>\n    tensor_x &#061; torch.zeros(len(x), n_letters)<br \/>\n    # \u904d\u5386\u8fd9\u4e2a\u4eba\u540d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u7d22\u5f15\u548c\u5b57\u7b26<br \/>\n    for li, letter in enumerate(x):<br \/>\n        # letter\u5728\u5b57\u7b26\u4e32all_letters\u4e2d\u7684\u4f4d\u7f6e \u5c31\u662fonehot\u5f20\u91cf1\u7d22\u5f15\u7684\u4f4d\u7f6e<br \/>\n        # letter\u5728\u5b57\u7b26\u4e32all_letters\u4e2d\u7684\u4f4d\u7f6e \u4f7f\u7528\u5b57\u7b26\u4e32find()\u65b9\u6cd5\u83b7\u53d6<br \/>\n        tensor_x[li][all_letters.find(letter)] &#061; 1<br \/>\n    return tensor_x<\/p>\n<p># \u601d\u8def\u5206\u6790<br \/>\n# 1 \u8f93\u5165\u6587\u672c\u6570\u636e \u5f20\u91cf\u5316one-hot<br \/>\n# 2 \u5b9e\u4f8b\u5316\u6a21\u578b \u52a0\u8f7d\u5df2\u8bad\u7ec3\u6a21\u578b\u53c2\u6570 m.load_state_dict(torch.load(my_path_rnn))<br \/>\n# 3 \u6a21\u578b\u9884\u6d4b with torch.no_grad()<br \/>\n# 4 \u4ece\u9884\u6d4b\u7ed3\u679c\u4e2d\u53d6\u51fa\u524d3\u540d,\u663e\u793a\u6253\u5370\u7ed3\u679c output.topk(3, 1, True)<br \/>\n#   category_idx &#061; topi[0][i] category &#061; categorys[category_idx]<\/p>\n<p># \u6784\u5efarnn\u9884\u6d4b\u51fd\u6570<br \/>\ndef my_predict_rnn(x):<\/p>\n<p>    n_letters &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    n_categories &#061; 18<\/p>\n<p>    # \u8f93\u5165\u6587\u672c, \u5f20\u91cf\u5316one-hot<br \/>\n    x_tensor &#061; lineToTensor(x)<\/p>\n<p>    # \u5b9e\u4f8b\u5316\u6a21\u578b \u52a0\u8f7d\u5df2\u8bad\u7ec3\u6a21\u578b\u53c2\u6570<br \/>\n    my_rnn &#061; RNN(n_letters, n_hidden, n_categories)<br \/>\n    my_rnn.load_state_dict(torch.load(my_path_rnn))<\/p>\n<p>    with torch.no_grad():<br \/>\n        # \u6a21\u578b\u9884\u6d4b<br \/>\n        output, hidden &#061; my_rnn(x_tensor, my_rnn.inithidden())<\/p>\n<p>        # \u4ece\u9884\u6d4b\u7ed3\u679c\u4e2d\u53d6\u51fa\u524d3\u540d<br \/>\n        # 3\u8868\u793a\u53d6\u524d3\u540d, 1\u8868\u793a\u8981\u6392\u5e8f\u7684\u7ef4\u5ea6, True\u8868\u793a\u662f\u5426\u8fd4\u56de\u6700\u5927\u6216\u662f\u6700\u4e0b\u7684\u5143\u7d20<br \/>\n        topv, topi &#061; output.topk(3, 1, True)<\/p>\n<p>        print(&#039;rnn &#061;&gt;&#039;, x)<br \/>\n        for i in range(3):<br \/>\n            value &#061; topv[0][i]<br \/>\n            category_idx &#061; topi[0][i]<br \/>\n            category &#061; categorys[category_idx]<br \/>\n            print(&#039;\\\\t value:%d  category:%s&#039; %(value, category))<\/p>\n<h5 id=\"2-lstm_2\">2 \u6784\u5efaLSTM\u9884\u6d4b\u51fd\u6570<\/h5>\n<p># \u6784\u5efaLSTM \u9884\u6d4b\u51fd\u6570<br \/>\ndef my_predict_lstm(x):<\/p>\n<p>    n_letters &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    n_categories &#061; 18<\/p>\n<p>    # \u8f93\u5165\u6587\u672c, \u5f20\u91cf\u5316one-hot<br \/>\n    x_tensor &#061; lineToTensor(x)<\/p>\n<p>    # \u5b9e\u4f8b\u5316\u6a21\u578b \u52a0\u8f7d\u5df2\u8bad\u7ec3\u6a21\u578b\u53c2\u6570<br \/>\n    my_lstm &#061; LSTM(n_letters, n_hidden, n_categories)<br \/>\n    my_lstm.load_state_dict(torch.load(my_path_lstm))<\/p>\n<p>    with torch.no_grad():<br \/>\n        # \u6a21\u578b\u9884\u6d4b<br \/>\n        hidden, c &#061; my_lstm.inithidden()<br \/>\n        output, hidden, c &#061; my_lstm(x_tensor, hidden, c)<\/p>\n<p>        # \u4ece\u9884\u6d4b\u7ed3\u679c\u4e2d\u53d6\u51fa\u524d3\u540d<br \/>\n        # 3\u8868\u793a\u53d6\u524d3\u540d, 1\u8868\u793a\u8981\u6392\u5e8f\u7684\u7ef4\u5ea6, True\u8868\u793a\u662f\u5426\u8fd4\u56de\u6700\u5927\u6216\u662f\u6700\u4e0b\u7684\u5143\u7d20<br \/>\n        topv, topi &#061; output.topk(3, 1, True)<\/p>\n<p>        print(&#039;rnn &#061;&gt;&#039;, x)<br \/>\n        for i in range(3):<br \/>\n            value &#061; topv[0][i]<br \/>\n            category_idx &#061; topi[0][i]<br \/>\n            category &#061; categorys[category_idx]<br \/>\n            print(&#039;\\\\t value:%d  category:%s&#039; % (value, category))<br \/>\n            print(&#039;\\\\t value:%d  category:%s&#039; % (value, category))<\/p>\n<h5 id=\"3-gru_2\">3 \u6784\u5efaGRU\u9884\u6d4b\u51fd\u6570<\/h5>\n<p># \u6784\u5efaGRU \u9884\u6d4b\u51fd\u6570<br \/>\ndef my_predict_gru(x):<\/p>\n<p>    n_letters &#061; 57<br \/>\n    n_hidden &#061; 128<br \/>\n    n_categories &#061; 18<\/p>\n<p>    # \u8f93\u5165\u6587\u672c, \u5f20\u91cf\u5316one-hot<br \/>\n    x_tensor &#061; lineToTensor(x)<\/p>\n<p>    # \u5b9e\u4f8b\u5316\u6a21\u578b \u52a0\u8f7d\u5df2\u8bad\u7ec3\u6a21\u578b\u53c2\u6570<br \/>\n    my_gru &#061; GRU(n_letters, n_hidden, n_categories)<br \/>\n    my_gru.load_state_dict(torch.load(my_path_gru))<\/p>\n<p>    with torch.no_grad():<br \/>\n        # \u6a21\u578b\u9884\u6d4b<br \/>\n        output, hidden &#061; my_gru(x_tensor, my_gru.inithidden())<\/p>\n<p>        # \u4ece\u9884\u6d4b\u7ed3\u679c\u4e2d\u53d6\u51fa\u524d3\u540d<br \/>\n        # 3\u8868\u793a\u53d6\u524d3\u540d, 1\u8868\u793a\u8981\u6392\u5e8f\u7684\u7ef4\u5ea6, True\u8868\u793a\u662f\u5426\u8fd4\u56de\u6700\u5927\u6216\u662f\u6700\u4e0b\u7684\u5143\u7d20<br \/>\n        topv, topi &#061; output.topk(3, 1, True)<\/p>\n<p>        print(&#039;rnn &#061;&gt;&#039;, x)<br \/>\n        for i in range(3):<br \/>\n            value &#061; topv[0][i]<br \/>\n            category_idx &#061; topi[0][i]<br \/>\n            category &#061; categorys[category_idx]<br \/>\n            print(&#039;\\\\t value:%d  category:%s&#039; % (value, category))<\/p>\n<h5 id=\"4-rnn_lstm_gru_1\">4 \u6784\u5efaRNN_LSTM_GRU\u9884\u6d4b\u8c03\u7528\u51fd\u6570<\/h5>\n<p>def dm_test_predic_rnn_lstm_gru():<br \/>\n        # \u628a\u4e09\u4e2a\u51fd\u6570\u7684\u5165\u53e3\u5730\u5740 \u7ec4\u6210\u5217\u8868&#xff0c;\u7edf\u4e00\u8f93\u5165\u6570\u636e\u8fdb\u884c\u6d4b\u8bd5<br \/>\n    for func in [my_predict_rnn, my_predict_lstm, my_predict_gru]:<br \/>\n        func(&#039;zhang&#039;)<\/p>\n<ul>\n<li>\u8f93\u51fa\u6548\u679c<\/li>\n<\/ul>\n<p>rnn &#061;&gt; zhang<br \/>\n     value:0  category:Russian<br \/>\n     value:0  category:Chinese<br \/>\n     value:-4  category:German<br \/>\nrnn &#061;&gt; zhang<br \/>\n     value:0  category:Chinese<br \/>\n     value:-1  category:Russian<br \/>\n     value:-1  category:German<br \/>\nrnn &#061;&gt; zhang<br \/>\n     value:0  category:Russian<br \/>\n     value:0  category:Chinese<br \/>\n     value:-2  category:Korean<\/p>\n<h3 id=\"3_2\">3 \u5c0f\u7ed3<\/h3>\n<ul>\n<li>\n<p>\u5b66\u4e60\u4e86\u5173\u4e8e\u4eba\u540d\u5206\u7c7b\u95ee\u9898: \u4ee5\u4e00\u4e2a\u4eba\u540d\u4e3a\u8f93\u5165, \u4f7f\u7528\u6a21\u578b\u5e2e\u52a9\u6211\u4eec\u5224\u65ad\u5b83\u6700\u6709\u53ef\u80fd\u662f\u6765\u81ea\u54ea\u4e00\u4e2a\u56fd\u5bb6\u7684\u4eba\u540d, \u8fd9\u5728\u67d0\u4e9b\u56fd\u9645\u5316\u516c\u53f8\u7684\u4e1a\u52a1\u4e2d\u5177\u6709\u91cd\u8981\u610f\u4e49, \u5728\u7528\u6237\u6ce8\u518c\u8fc7\u7a0b\u4e2d, \u4f1a\u6839\u636e\u7528\u6237\u586b\u5199\u7684\u540d\u5b57\u76f4\u63a5\u7ed9\u4ed6\u5206\u914d\u53ef\u80fd\u7684\u56fd\u5bb6\u6216\u5730\u533a\u9009\u9879, \u4ee5\u53ca\u8be5\u56fd\u5bb6\u6216\u5730\u533a\u7684\u56fd\u65d7, \u9650\u5236\u624b\u673a\u53f7\u7801\u4f4d\u6570\u7b49\u7b49.<\/p>\n<\/li>\n<li>\n<p>\u4eba\u540d\u5206\u7c7b\u5668\u7684\u5b9e\u73b0\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e94\u4e2a\u6b65\u9aa4:<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b65: \u5bfc\u5165\u5fc5\u5907\u7684\u5de5\u5177\u5305<\/li>\n<li>\u7b2c\u4e8c\u6b65: \u5bf9data\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406&#xff0c;\u6ee1\u8db3\u8bad\u7ec3\u8981\u6c42<\/li>\n<li>\u7b2c\u4e09\u6b65: \u6784\u5efaRNN\u6a21\u578b(\u5305\u62ec\u4f20\u7edfRNN, LSTM\u4ee5\u53caGRU)<\/li>\n<li>\u7b2c\u56db\u6b65: \u6784\u5efa\u8bad\u7ec3\u51fd\u6570\u5e76\u8fdb\u884c\u8bad\u7ec3<\/li>\n<li>\u7b2c\u4e94\u6b65: \u6784\u5efa\u8bc4\u4f30\u51fd\u6570\u5e76\u8fdb\u884c\u9884\u6d4b<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7b2c\u4e00\u6b65: \u5bfc\u5165\u5fc5\u5907\u7684\u5de5\u5177\u5305<\/p>\n<ul>\n<li>python\u7248\u672c\u4f7f\u75283.7.x, pytorch\u7248\u672c\u4f7f\u75281.6.1<\/li>\n<\/ul>\n<\/li>\n<li>\u7b2c\u4e8c\u6b65: \u5bf9data\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406&#xff0c;\u6ee1\u8db3\u8bad\u7ec3\u8981\u6c42\n<ul>\n<li>\u8bfb\u539f\u59cb\u6570\u636e\u5230\u5185\u5b58&#xff0c;\u6784\u5efa\u51fa\u6a21\u578b\u9700\u8981\u7684\u6570\u636ex&#xff0c;\u6807\u7b7ey&#xff0c;\u7136\u540e\u628a\u6570\u636e\u8f6c\u6210\u6570\u636e\u6e90&#xff0c;\u6700\u540e\u518d\u5c01\u88c5\u6210\u6570\u636e\u8fed\u4ee3\u5668<\/li>\n<li>\u4ece\u7f16\u7a0b\u5b9e\u73b0\u6765\u770b&#xff0c;\u6587\u672c\u6570\u503c\u5316&#xff0c;\u6570\u503c\u5f20\u91cf\u5316\u662f\u901a\u8fc7one-hot\u7f16\u7801\u4e00\u6b65\u5b8c\u6210\u7684<\/li>\n<\/ul>\n<\/li>\n<li>\u7b2c\u4e09\u6b65: \u6784\u5efaRNN\u6a21\u578b\n<ul>\n<li>\u6784\u5efa\u4f20\u7edf\u7684RNN\u6a21\u578b\u7684\u7c7bclass RNN.<\/li>\n<li>\u6784\u5efaLSTM\u6a21\u578b\u7684\u7c7bclass LSTM.<\/li>\n<li>\u6784\u5efaGRU\u6a21\u578b\u7684\u7c7bclass GRU.<\/li>\n<\/ul>\n<\/li>\n<li>\u7b2c\u56db\u6b65: \u6784\u5efa\u8bad\u7ec3\u51fd\u6570\u5e76\u8fdb\u884c\u8bad\u7ec3\n<ul>\n<li>\u5b9e\u4f8b\u5316\u6570\u636e\u8fed\u4ee3\u5668\u5bf9\u8c61<\/li>\n<li>\u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61\u3001\u635f\u5931\u51fd\u6570\u5bf9\u8c61\u3001\u4f18\u5316\u5668\u5bf9\u8c61<\/li>\n<li>\u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u7684\u53c2\u6570<\/li>\n<li>\u8bad\u7ec3\u6a21\u578b<\/li>\n<li>\u5916\u5c42for\u5faa\u73af \u63a7\u5236\u8f6e\u6570<\/li>\n<li>\u5185\u5c42for\u5faa\u73af \u63a7\u5236\u8fed\u4ee3\u6b21\u6570&#xff0c;\u7ed9\u6a21\u578b\u5582\u6570\u636e&#xff0c;\u8ba1\u7b97\u635f\u5931 &#xff0c;\u68af\u5ea6\u6e05\u96f6 &#xff0c;\u53cd\u5411\u4f20\u64ad &#xff0c; \u68af\u5ea6\u66f4\u65b0&#xff0c;\u6253\u5370\u65e5\u5fd7<\/li>\n<li>\u6a21\u578b\u4fdd\u5b58<\/li>\n<\/ul>\n<\/li>\n<li>\u635f\u5931\u5bf9\u6bd4\u66f2\u7ebf\u5206\u6790:\n<ul>\n<li>\u4f20\u7edfRNN\u7684\u6a21\u578b\u7b2c\u4e00\u4e2a\u8f6e\u6b21\u5f00\u59cb\u6536\u655b\u60c5\u51b5\u6700\u597d&#xff0c;\u7136\u540e\u662fGRU, \u6700\u540e\u662fLSTM, \u8fd9\u662f\u56e0\u4e3aRNN\u6a21\u578b\u7b80\u5355\u53c2\u6570\u5c11&#xff0c;\u89c1\u6548\u5feb\u3002<\/li>\n<li>\u968f\u7740\u8bad\u7ec3\u6570\u636e\u7684\u589e\u52a0&#xff0c;GRU\u6548\u679c\u6700\u597d\u3001LSTM\u6548\u679c\u6b21\u4e4b\u3001RNN\u6548\u679c\u6392\u6700\u540e<\/li>\n<\/ul>\n<\/li>\n<li>\u8bad\u7ec3\u8017\u65f6\u5bf9\u6bd4\u56fe\u5206\u6790:\n<ul>\n<li>\u6a21\u578b\u8bad\u7ec3\u7684\u8017\u65f6\u957f\u77ed\u4ee3\u8868\u6a21\u578b\u7684\u8ba1\u7b97\u590d\u6742\u5ea6&#xff0c;\u7531\u56fe\u53ef\u77e5&#xff0c;\u4e5f\u6b63\u5982\u6211\u4eec\u4e4b\u524d\u7684\u7406\u8bba\u5206\u6790&#xff0c;\u4f20\u7edfRNN\u590d\u6742\u5ea6\u6700\u4f4e&#xff0c;\u8017\u65f6\u51e0\u4e4e\u53ea\u662f\u540e\u4e24\u8005\u7684\u4e00\u534a&#xff0c;\u7136\u540e\u662fGRU&#xff0c;\u6700\u540e\u662f\u590d\u6742\u5ea6\u6700\u9ad8\u7684LSTM<\/li>\n<\/ul>\n<\/li>\n<li>\u7ed3\u8bba:\n<ul>\n<li>\u6a21\u578b\u9009\u7528\u4e00\u822c\u5e94\u901a\u8fc7\u5b9e\u9a8c\u5bf9\u6bd4&#xff0c; \u5e76\u975e\u8d8a\u590d\u6742\u6216\u8d8a\u5148\u8fdb\u7684\u6a21\u578b\u8868\u73b0\u8d8a\u597d&#xff0c; \u800c\u662f\u9700\u8981\u7ed3\u5408\u81ea\u5df1\u7684\u7279\u5b9a\u4efb\u52a1&#xff0c;\u4ece\u5bf9\u6570\u636e\u7684\u5206\u6790\u548c\u5b9e\u9a8c\u7ed3\u679c\u4e2d\u83b7\u5f97\u6700\u4f73\u7b54\u6848<\/li>\n<\/ul>\n<\/li>\n<li>\u7b2c\u4e94\u6b65: \u6784\u5efa\u9884\u6d4b\u51fd\u6570\u5e76\u8fdb\u884c\u9884\u6d4b\n<ul>\n<li>\u6784\u5efa\u4f20\u7edfRNN\u9884\u6d4b\u51fd\u6570<\/li>\n<li>\u6784\u5efaLSTM\u9884\u6d4b\u51fd\u6570<\/li>\n<li>\u6784\u5efaGRU\u9884\u6d4b\u51fd\u6570<\/li>\n<li>\u6784\u5efa\u9884\u6d4b\u51fd\u6570\u8c03\u7528\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u4f5c\u8005\u6709\u8bdd\u8bf4&#xff1a;<\/h2>\n<p>\u9996\u5148&#xff0c;\u611f\u8c22\u60a8\u770b\u5230\u4e86\u8fd9\u91cc&#xff0c;\u611f\u8c22\u652f\u6301&#xff01;\u4e0d\u77e5\u9053, \u901a\u8fc7\u6848\u4f8b, \u60a8\u662f\u5426\u5bf9 RNN \u6a21\u578b\u7684\u6267\u884c\u6d41\u7a0b\u6709\u4e86\u66f4\u4e3a\u5168\u9762\u7684\u4e86\u89e3?<\/p>\n<p>\u6211\u4e00\u76f4\u8ba4\u4e3a&#xff0c;\u5de9\u56fa\u77e5\u8bc6\u3001\u5b66\u4e60\u77e5\u8bc6\u7684\u6700\u597d\u529e\u6cd5\u5c31\u662f\u83b7\u53d6\u2192\u8f93\u51fa\u2192\u518d\u83b7\u53d6\u2192\u518d\u8f93\u51fa\u2026\u2026<\/p>\n<p>\u8fd9\u65e2\u662f\u6bdb\u6559\u5458\u7684\u300a\u5b9e\u8df5\u8bba\u300b&#xff0c;\u4e5f\u662f\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u7684\u601d\u7ef4\u3002\u843d\u5730\u6765\u8bf4&#xff0c;\u4e86\u89e3\u4e86\u67d0\u4e2a\u77e5\u8bc6\u70b9&#xff0c;\u6700\u7ec8\u6211\u4eec\u8981\u4ee5\u8bed\u8a00\u7684\u5f62\u5f0f\u8f93\u51fa\u51fa\u6765&#xff0c;\u8ba9\u522b\u4eba\u80fd\u542c\u61c2&#xff0c;\u6700\u7ec8\u76ee\u7684\u662f\u63d0\u9ad8\u4e2a\u4eba\u7684\u8bed\u8a00\u8868\u8fbe\u80fd\u529b\u3001\u9762\u8bd5\u80fd\u529b<\/p>\n<p>\u6211\u662f\u8bd7\u4eba\u554a_\u7a0b\u5e8f\u5458&#xff0c;\u81f4\u529b\u4e8e\u5206\u4eab\u4eba\u5de5\u667a\u80fd\u65b9\u9762\u7684\u77e5\u8bc6&#xff0c;\u8fd1\u671f NLP \u81ea\u7136\u8bed\u8a00\u5904\u7406\u7cfb\u5217\u6587\u7ae0\u53d1\u5e03\u4e2d&#xff0c;\u5982\u679c\u611f\u5174\u8da3&#xff0c;\u6765\u4e2a\u5173\u6ce8\u5457&#xff5e;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb947\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u3010\u6458\u8981\u3011\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u57fa\u4e8ePyTorch\u7684RNN\u4eba\u540d\u5206\u7c7b\u5668\u6848\u4f8b\uff0c\u901a\u8fc7\u5b9e\u73b0\u4f20\u7edfRNN\u3001LSTM\u548cGRU\u4e09\u79cd\u6a21\u578b\uff0c\u5b8c\u6210\u4ece\u8f93\u5165\u4eba\u540d\u9884\u6d4b\u56fd\u7c4d\u7684\u4efb\u52a1\u3002\u6848\u4f8b\u5305\u542b\u4e94\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u9884\u5904\u7406\uff08\u5305\u62ec\u5b57\u7b26one-hot\u7f16\u7801\uff09\u3001\u6a21\u578b\u6784\u5efa\uff08RNN\/LSTM\/GRU\u5b9e\u73b0\uff09\u3001\u8bad\u7ec3\u51fd\u6570\u8bbe\u8ba1\u3001\u6a21\u578b\u8bad\u7ec3\u53ca\u7ed3\u679c\u5206\u6790\uff08\u901a\u8fc7\u635f\u5931\u66f2\u7ebf\u3001\u8017\u65f6\u548c\u51c6\u786e\u7387\u5bf9\u6bd4\uff09\uff0c\u6700\u540e\u5b9e\u73b0\u9884\u6d4b\u529f\u80fd\u3002\u5b9e\u9a8c\u8868\u660e\uff0cRNN\u6536\u655b\u6700\u5feb\u4f46\u6700\u7ec8\u6548\u679c\u8f83\u5dee\uff0cGRU\u7efc\u5408\u8868\u73b0\u6700\u4f73\uff0cLSTM\u8017\u65f6\u6700\u957f\u4f46\u6548\u679c\u5c45\u4e2d\u3002\u6848\u4f8b\u5b8c\u6574\u5c55\u793a\u4e86\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u7684\u5b9e\u73b0\u6d41\u7a0b\uff0c\u4e3a\u5e8f\u5217\u6a21\u578b\u5b9e\u8df5\u63d0\u4f9b\u4e86\u5178\u578b\u8303\u4f8b\u3002<\/p>\n","protected":false},"author":2,"featured_media":52833,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[587,5323,2068,2873,50,207,86],"topic":[],"class_list":["post-52836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-587","tag-gru","tag-nlp","tag-rnn","tag-50","tag-207","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/52836.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb947\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u3010\u6458\u8981\u3011\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u57fa\u4e8ePyTorch\u7684RNN\u4eba\u540d\u5206\u7c7b\u5668\u6848\u4f8b\uff0c\u901a\u8fc7\u5b9e\u73b0\u4f20\u7edfRNN\u3001LSTM\u548cGRU\u4e09\u79cd\u6a21\u578b\uff0c\u5b8c\u6210\u4ece\u8f93\u5165\u4eba\u540d\u9884\u6d4b\u56fd\u7c4d\u7684\u4efb\u52a1\u3002\u6848\u4f8b\u5305\u542b\u4e94\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u9884\u5904\u7406\uff08\u5305\u62ec\u5b57\u7b26one-hot\u7f16\u7801\uff09\u3001\u6a21\u578b\u6784\u5efa\uff08RNN\/LSTM\/GRU\u5b9e\u73b0\uff09\u3001\u8bad\u7ec3\u51fd\u6570\u8bbe\u8ba1\u3001\u6a21\u578b\u8bad\u7ec3\u53ca\u7ed3\u679c\u5206\u6790\uff08\u901a\u8fc7\u635f\u5931\u66f2\u7ebf\u3001\u8017\u65f6\u548c\u51c6\u786e\u7387\u5bf9\u6bd4\uff09\uff0c\u6700\u540e\u5b9e\u73b0\u9884\u6d4b\u529f\u80fd\u3002\u5b9e\u9a8c\u8868\u660e\uff0cRNN\u6536\u655b\u6700\u5feb\u4f46\u6700\u7ec8\u6548\u679c\u8f83\u5dee\uff0cGRU\u7efc\u5408\u8868\u73b0\u6700\u4f73\uff0cLSTM\u8017\u65f6\u6700\u957f\u4f46\u6548\u679c\u5c45\u4e2d\u3002\u6848\u4f8b\u5b8c\u6574\u5c55\u793a\u4e86\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u7684\u5b9e\u73b0\u6d41\u7a0b\uff0c\u4e3a\u5e8f\u5217\u6a21\u578b\u5b9e\u8df5\u63d0\u4f9b\u4e86\u5178\u578b\u8303\u4f8b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/52836.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T12:09:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811120921-6899dd71eb2b1.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52836.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/52836.html\",\"name\":\"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T12:09:36+00:00\",\"dateModified\":\"2025-08-11T12:09:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/52836.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/52836.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52836.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b--\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/52836.html","og_locale":"zh_CN","og_type":"article","og_title":"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb947\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u3010\u6458\u8981\u3011\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u57fa\u4e8ePyTorch\u7684RNN\u4eba\u540d\u5206\u7c7b\u5668\u6848\u4f8b\uff0c\u901a\u8fc7\u5b9e\u73b0\u4f20\u7edfRNN\u3001LSTM\u548cGRU\u4e09\u79cd\u6a21\u578b\uff0c\u5b8c\u6210\u4ece\u8f93\u5165\u4eba\u540d\u9884\u6d4b\u56fd\u7c4d\u7684\u4efb\u52a1\u3002\u6848\u4f8b\u5305\u542b\u4e94\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u9884\u5904\u7406\uff08\u5305\u62ec\u5b57\u7b26one-hot\u7f16\u7801\uff09\u3001\u6a21\u578b\u6784\u5efa\uff08RNN\/LSTM\/GRU\u5b9e\u73b0\uff09\u3001\u8bad\u7ec3\u51fd\u6570\u8bbe\u8ba1\u3001\u6a21\u578b\u8bad\u7ec3\u53ca\u7ed3\u679c\u5206\u6790\uff08\u901a\u8fc7\u635f\u5931\u66f2\u7ebf\u3001\u8017\u65f6\u548c\u51c6\u786e\u7387\u5bf9\u6bd4\uff09\uff0c\u6700\u540e\u5b9e\u73b0\u9884\u6d4b\u529f\u80fd\u3002\u5b9e\u9a8c\u8868\u660e\uff0cRNN\u6536\u655b\u6700\u5feb\u4f46\u6700\u7ec8\u6548\u679c\u8f83\u5dee\uff0cGRU\u7efc\u5408\u8868\u73b0\u6700\u4f73\uff0cLSTM\u8017\u65f6\u6700\u957f\u4f46\u6548\u679c\u5c45\u4e2d\u3002\u6848\u4f8b\u5b8c\u6574\u5c55\u793a\u4e86\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u7684\u5b9e\u73b0\u6d41\u7a0b\uff0c\u4e3a\u5e8f\u5217\u6a21\u578b\u5b9e\u8df5\u63d0\u4f9b\u4e86\u5178\u578b\u8303\u4f8b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/52836.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T12:09:36+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811120921-6899dd71eb2b1.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/52836.html","url":"https:\/\/www.wsisp.com\/helps\/52836.html","name":"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b-\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T12:09:36+00:00","dateModified":"2025-08-11T12:09:36+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/52836.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/52836.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/52836.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"(!!\u8840\u4e66\u4e24\u4e07\u5b57!!)\u5e26\u4f60\u505aRNN\u6848\u4f8b--\u4eba\u540d\u5206\u7c7b\u5668(\u9644\u5b8c\u6574\u4ee3\u7801, \u624b\u6b8b\u515a\u72c2\u559c!)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=52836"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/52833"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=52836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=52836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=52836"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=52836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}