{"id":72574,"date":"2026-02-06T00:32:54","date_gmt":"2026-02-05T16:32:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/72574.html"},"modified":"2026-02-06T00:32:54","modified_gmt":"2026-02-05T16:32:54","slug":"lstm%e7%bd%91%e7%bb%9c%e5%ae%9e%e6%88%98%ef%bc%9a%e5%ae%9e%e7%8e%b0%e5%be%ae%e5%8d%9a%e8%af%84%e8%ae%ba%e6%83%85%e6%84%9f%e5%88%86%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/72574.html","title":{"rendered":"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E4%B8%80%E3%80%81%E4%BB%BB%E5%8A%A1%E6%98%8E%E7%A1%AE-toc\" style=\"margin-left:0px\">\u4e00\u3001\u4efb\u52a1\u660e\u786e<\/p>\n<p id=\"1.%20%E4%BB%BB%E5%8A%A1%E7%9B%AE%E6%A0%87-toc\" style=\"margin-left:40px\">1. \u4efb\u52a1\u76ee\u6807<\/p>\n<p id=\"2.%20%E6%A0%B8%E5%BF%83%E8%A6%81%E6%B1%82-toc\" style=\"margin-left:40px\">2. \u6838\u5fc3\u8981\u6c42<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E6%A0%B8%E5%BF%83%E6%80%9D%E8%B7%AF-toc\" style=\"margin-left:0px\">\u4e8c\u3001\u6838\u5fc3\u601d\u8def<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E6%B5%81%E7%A8%8B%E8%AE%BE%E8%AE%A1-toc\" style=\"margin-left:0px\">\u4e09\u3001\u6d41\u7a0b\u8bbe\u8ba1<\/p>\n<p id=\"%E9%98%B6%E6%AE%B51%EF%BC%9A%E5%8E%9F%E5%A7%8B%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E5%AD%97%E7%AC%A6%E2%86%92%E7%B4%A2%E5%BC%95%E2%86%92%E6%89%B9%E6%AC%A1%E5%BC%A0%E9%87%8F%EF%BC%8C%E9%80%82%E9%85%8D%E6%A8%A1%E5%9E%8B%E8%BE%93%E5%85%A5%EF%BC%89-toc\" style=\"margin-left:40px\">\u9636\u6bb51&#xff1a;\u539f\u59cb\u6570\u636e\u5904\u7406&#xff08;\u6838\u5fc3&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15\u2192\u6279\u6b21\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165&#xff09;<\/p>\n<p id=\"%E9%98%B6%E6%AE%B52%EF%BC%9ALSTM%E6%A8%A1%E5%9E%8B%E6%9E%84%E5%BB%BA%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E8%AF%8D%E5%90%91%E9%87%8F%E2%86%92%E6%97%B6%E5%BA%8F%E7%89%B9%E5%BE%81%E2%86%92%E6%83%85%E6%84%9F%E5%88%86%E7%B1%BB%EF%BC%89-toc\" style=\"margin-left:40px\">\u9636\u6bb52&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;\u6838\u5fc3&#xff1a;\u8bcd\u5411\u91cf\u2192\u65f6\u5e8f\u7279\u5f81\u2192\u60c5\u611f\u5206\u7c7b&#xff09;<\/p>\n<p id=\"%E9%98%B6%E6%AE%B53%EF%BC%9A%E8%AE%AD%E7%BB%83%E4%B8%8E%E6%B5%8B%E8%AF%95%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E4%BC%98%E5%8C%96%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0%EF%BC%8C%E8%AF%84%E4%BC%B0%E6%A8%A1%E5%9E%8B%E6%95%88%E6%9E%9C%EF%BC%89-toc\" style=\"margin-left:40px\">\u9636\u6bb53&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;\u6838\u5fc3&#xff1a;\u4f18\u5316\u6a21\u578b\u53c2\u6570&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff09;<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:0px\">\u56db\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<p id=\"%E6%A8%A1%E5%9D%971%EF%BC%9A%E8%AF%8D%E8%A1%A8%E6%9E%84%E5%BB%BA%EF%BC%88build_vocab.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57571&#xff1a;\u8bcd\u8868\u6784\u5efa&#xff08;build_vocab.py&#xff09;<\/p>\n<p id=\"%E6%A8%A1%E5%9D%972%EF%BC%9A%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD%E4%B8%8E%E8%BF%AD%E4%BB%A3%E5%99%A8%EF%BC%88load_dataset.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57572&#xff1a;\u6570\u636e\u52a0\u8f7d\u4e0e\u8fed\u4ee3\u5668&#xff08;load_dataset.py&#xff09;<\/p>\n<p id=\"%E6%A8%A1%E5%9D%973%EF%BC%9ALSTM%E6%A8%A1%E5%9E%8B%E6%9E%84%E5%BB%BA%EF%BC%88TextRNN.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57573&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;TextRNN.py&#xff09;<\/p>\n<p id=\"%E6%A8%A1%E5%9D%974%EF%BC%9A%E8%AE%AD%E7%BB%83%E4%B8%8E%E6%B5%8B%E8%AF%95%EF%BC%88train_eval_test.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57574&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;train_eval_test.py&#xff09;<\/p>\n<p id=\"%E6%A8%A1%E5%9D%975%EF%BC%9A%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%88main.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57575&#xff1a;\u4e3b\u7a0b\u5e8f&#xff08;main.py&#xff09;<\/p>\n<p id=\"%E6%A8%A1%E5%9D%976%EF%BC%9A%E5%8D%95%E4%B8%AA%E5%8F%A5%E5%AD%90%E9%A2%84%E6%B5%8B%EF%BC%88predict.py%EF%BC%89-toc\" style=\"margin-left:40px\">\u6a21\u57576&#xff1a;\u5355\u4e2a\u53e5\u5b50\u9884\u6d4b&#xff08;predict.py&#xff09;<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E5%85%B3%E9%94%AE%E4%BF%AE%E6%AD%A3%E4%B8%8E%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E-toc\" style=\"margin-left:0px\">\u4e94\u3001\u5173\u952e\u4fee\u6b63\u4e0e\u8865\u5145\u8bf4\u660e<\/p>\n<p id=\"1.%20%E5%8E%9F%E5%A7%8B%E6%B5%81%E7%A8%8B%E6%A0%B8%E5%BF%83%E4%BF%AE%E6%AD%A3-toc\" style=\"margin-left:40px\">1. \u539f\u59cb\u6d41\u7a0b\u6838\u5fc3\u4fee\u6b63<\/p>\n<p id=\"-toc\" style=\"margin-left:40px\">\n<p id=\"%E5%85%AD%E3%80%81%E6%80%BB%E7%BB%93%E4%B8%8E%E8%BF%9B%E9%98%B6%E6%96%B9%E5%90%91-toc\" style=\"margin-left:0px\">\u516d\u3001\u603b\u7ed3\u4e0e\u8fdb\u9636\u65b9\u5411<\/p>\n<p id=\"1.%20%E5%AE%9E%E6%88%98%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">1. \u5b9e\u6218\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<p>\u7ee7\u4e0a\u4e00\u7bc7\u5b66\u4e60\u4e86\u5faa\u73af\u795e\u7ecf\u7f51\u7edc&#xff08;RNN&#xff09;\u53ca\u6539\u8fdb\u7248\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc&#xff08;LSTM&#xff09;\u7684\u6838\u5fc3\u539f\u7406\u540e&#xff0c;\u672c\u6587\u5c06\u4ece\u5b9e\u6218\u89d2\u5ea6\u51fa\u53d1&#xff0c;\u57fa\u4e8eLSTM\u7f51\u7edc\u5b8c\u6210\u5fae\u535a\u8bc4\u8bba\u7684\u60c5\u611f\u5206\u7c7b\u4efb\u52a1\u3002\u6574\u4e2a\u8fc7\u7a0b\u6db5\u76d6\u6570\u636e\u5904\u7406\u3001\u6a21\u578b\u642d\u5efa\u3001\u8bad\u7ec3\u6d4b\u8bd5\u6574\u4e2a\u6d41\u7a0b&#xff0c;\u5305\u542b\u5b8c\u6574\u4ee3\u7801\u89e3\u6790\u3002<\/p>\n<h2 id=\"%E4%B8%80%E3%80%81%E4%BB%BB%E5%8A%A1%E6%98%8E%E7%A1%AE\">\u4e00\u3001\u4efb\u52a1\u660e\u786e<\/h2>\n<h3 id=\"1.%20%E4%BB%BB%E5%8A%A1%E7%9B%AE%E6%A0%87\">1. \u4efb\u52a1\u76ee\u6807<\/h3>\n<p>\u57fa\u4e8e\u5fae\u535a4\u7c7b\u60c5\u7eea\u6570\u636e\u96c6&#xff08;simplifyweibo_4_moods.csv&#xff09;&#xff0c;\u642d\u5efaLSTM\u6a21\u578b\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u7684\u60c5\u611f\u5206\u7c7b&#xff0c;\u5177\u4f53\u5c06\u8bc4\u8bba\u5206\u4e3a4\u7c7b\u60c5\u611f\u6807\u7b7e&#xff0c;\u8d34\u5408\u771f\u5b9e\u5fae\u535a\u573a\u666f&#xff1a;\u559c\u60a6\u3001\u6124\u6012\u3001\u538c\u6076\u3001\u4f4e\u843d\u3002\u90e8\u5206\u539f\u59cb\u6570\u636e\u5c55\u793a&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"372\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163252-6984c63423a42.png\" width=\"1603\" \/><\/p>\n<h3 id=\"2.%20%E6%A0%B8%E5%BF%83%E8%A6%81%E6%B1%82\">2. \u6838\u5fc3\u8981\u6c42<\/h3>\n<ul>\n<li>\n<p>\u6570\u636e\u5c42\u9762&#xff1a;\u91c7\u7528\u5b57\u7b26\u7ea7\u5904\u7406\u65b9\u5f0f&#xff0c;\u7edf\u4e00\u6240\u6709\u53e5\u5b50\u957f\u5ea6\u4e3a70&#xff08;\u591a\u4f59\u90e8\u5206\u622a\u65ad\u3001\u4e0d\u8db3\u90e8\u5206\u586b\u5145&#xff09;&#xff0c;\u6784\u5efa\u5b57\u7b26\u7ea7\u8bcd\u8868\u5e76\u8fc7\u6ee4\u4f4e\u9891\u8bcd&#xff0c;\u907f\u514d\u8bcd\u8868\u5197\u4f59\u3002<\/p>\n<\/li>\n<li>\n<p>\u6a21\u578b\u5c42\u9762&#xff1a;\u4f7f\u7528\u817e\u8baf\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u521d\u59cb\u5316Embedding\u5c42\u5e76\u5f00\u542f\u5fae\u8c03&#xff0c;\u5c06\u5b57\u7b26\u7d22\u5f15\u8f6c\u5316\u4e3a200\u7ef4\u8bcd\u5411\u91cf&#xff0c;\u4f20\u5165\u53cc\u5411LSTM\u63d0\u53d6\u6587\u672c\u65f6\u5e8f\u7279\u5f81&#xff0c;\u6700\u7ec8\u901a\u8fc7\u5168\u8fde\u63a5\u5c42\u5b8c\u62104\u7c7b\u60c5\u611f\u5206\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bc4\u4f30\u5c42\u9762&#xff1a;\u901a\u8fc7\u51c6\u786e\u7387&#xff08;Accuracy&#xff09;\u3001\u5206\u7c7b\u62a5\u544a&#xff08;\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u503c&#xff09;\u5168\u9762\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff0c;\u52a0\u5165\u65e9\u505c\u7b56\u7565&#xff08;Early Stopping&#xff09;\u907f\u514d\u6a21\u578b\u8fc7\u62df\u5408&#xff0c;\u63d0\u5347\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u64cd\u5c42\u9762&#xff1a;\u4ee3\u7801\u6a21\u5757\u5316\u62c6\u5206&#xff08;\u8bcd\u8868\u6784\u5efa\u3001\u6570\u636e\u52a0\u8f7d\u3001\u6a21\u578b\u5b9a\u4e49\u3001\u8bad\u7ec3\u6d4b\u8bd5&#xff09;&#xff0c;\u7ed3\u6784\u6e05\u6670&#xff0c;\u53ef\u76f4\u63a5\u590d\u5236\u590d\u7528&#xff0c;\u8865\u5145\u8fd0\u884c\u8bf4\u660e\u548c\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u65b9\u6848&#xff0c;\u964d\u4f4e\u65b0\u624b\u5b9e\u64cd\u95e8\u69db\u3002<\/p>\n<\/li>\n<\/ul>\n<h2 id=\"%E4%BA%8C%E3%80%81%E6%A0%B8%E5%BF%83%E6%80%9D%E8%B7%AF\">\u4e8c\u3001\u6838\u5fc3\u601d\u8def<\/h2>\n<p>\u6587\u672c\u60c5\u611f\u5206\u7c7b\u7684\u6838\u5fc3\u662f\u300c\u5c06\u6587\u672c\u8f6c\u5316\u4e3a\u53ef\u88ab\u6a21\u578b\u8bc6\u522b\u7684\u6570\u503c\u7279\u5f81&#xff0c;\u518d\u901a\u8fc7\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u6355\u6349\u6587\u672c\u4e2d\u7684\u60c5\u611f\u503e\u5411\u300d&#xff0c;\u7ed3\u5408LSTM\u7684\u65f6\u5e8f\u7279\u5f81\u4f18\u52bf&#xff0c;\u672c\u6b21\u5b9e\u6218\u6838\u5fc3\u601d\u8def\u5206\u4e3a3\u6b65&#xff0c;\u903b\u8f91\u8fde\u8d2f\u4e14\u8d34\u5408LSTM\u7279\u6027&#xff1a;<\/p>\n<li>\n<p>\u6570\u636e\u9884\u5904\u7406&#xff1a;\u5148\u6784\u5efa\u5b57\u7b26\u7ea7\u8bcd\u8868&#xff08;\u8fc7\u6ee4\u4f4e\u9891\u8bcd\u3001\u8865\u5145\u672a\u77e5\/\u586b\u5145\u5b57\u7b26&#xff09;&#xff0c;\u518d\u5c06\u539f\u59cb\u6587\u672c\u8f6c\u5316\u4e3a\u8bcd\u8868\u7d22\u5f15&#xff0c;\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6\u540e\u62c6\u5206\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u3001\u6d4b\u8bd5\u96c6&#xff08;8:1:1&#xff09;&#xff0c;\u6700\u540e\u6784\u5efa\u6570\u636e\u8fed\u4ee3\u5668&#xff0c;\u5c06\u6570\u636e\u8f6c\u4e3a\u5f20\u91cf\u5e76\u6309\u6279\u6b21\u52a0\u8f7d&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165\u683c\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p>\u6a21\u578b\u8bbe\u8ba1&#xff1a;\u91c7\u7528\u300cEmbedding\u2192\u53cc\u5411LSTM\u2192\u5168\u8fde\u63a5\u300d\u7684\u7ecf\u5178\u6587\u672c\u5206\u7c7b\u67b6\u6784\u2014\u2014Embedding\u5c42\u89e3\u51b3\u6587\u672c\u5411\u91cf\u5316\u95ee\u9898&#xff08;\u590d\u7528\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u63d0\u5347\u6548\u679c&#xff09;&#xff0c;\u53cc\u5411LSTM\u6355\u6349\u6587\u672c\u4e0a\u4e0b\u6587\u53cc\u5411\u65f6\u5e8f\u7279\u5f81&#xff08;\u8d34\u5408\u4e2d\u6587\u53e5\u5b50\u524d\u540e\u6587\u5173\u8054\u7684\u7279\u70b9&#xff09;&#xff0c;\u5168\u8fde\u63a5\u5c42\u5c06LSTM\u8f93\u51fa\u7684\u7279\u5f81\u5411\u91cf\u6620\u5c04\u4e3a4\u7c7b\u60c5\u611f\u7684\u9884\u6d4b\u6982\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff1a;\u4f7f\u7528Adam\u4f18\u5316\u5668\u3001\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\u8bad\u7ec3\u6a21\u578b&#xff0c;\u6bcf100\u6279\u6b21\u76d1\u63a7\u8bad\u7ec3\u96c6\u548c\u9a8c\u8bc1\u96c6\u6548\u679c&#xff0c;\u4fdd\u5b58\u9a8c\u8bc1\u96c6\u635f\u5931\u6700\u4f18\u7684\u6a21\u578b&#xff1b;\u8bad\u7ec3\u7ed3\u675f\u540e\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8bc4\u4f30\u6a21\u578b\u6027\u80fd&#xff0c;\u8f93\u51fa\u5206\u7c7b\u62a5\u544a&#xff0c;\u540c\u65f6\u52a0\u5165\u8bad\u7ec3\u53ef\u89c6\u5316&#xff0c;\u65b9\u4fbf\u76d1\u63a7\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u635f\u5931\u548c\u51c6\u786e\u7387\u53d8\u5316\u3002<\/p>\n<\/li>\n<h2 id=\"%E4%B8%89%E3%80%81%E6%B5%81%E7%A8%8B%E8%AE%BE%E8%AE%A1\">\u4e09\u3001\u6d41\u7a0b\u8bbe\u8ba1<\/h2>\n<\/p>\n<h3 id=\"%E9%98%B6%E6%AE%B51%EF%BC%9A%E5%8E%9F%E5%A7%8B%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E5%AD%97%E7%AC%A6%E2%86%92%E7%B4%A2%E5%BC%95%E2%86%92%E6%89%B9%E6%AC%A1%E5%BC%A0%E9%87%8F%EF%BC%8C%E9%80%82%E9%85%8D%E6%A8%A1%E5%9E%8B%E8%BE%93%E5%85%A5%EF%BC%89\">\u9636\u6bb51&#xff1a;\u539f\u59cb\u6570\u636e\u5904\u7406&#xff08;\u6838\u5fc3&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15\u2192\u6279\u6b21\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165&#xff09;<\/h3>\n<p>\u6570\u636e\u5904\u7406\u662f\u5b9e\u6218\u7684\u57fa\u7840&#xff0c;\u76f4\u63a5\u5f71\u54cd\u6a21\u578b\u6548\u679c&#xff0c;\u6838\u5fc3\u76ee\u6807\u662f\u300c\u5c06\u975e\u7ed3\u6784\u5316\u7684\u6587\u672c\u6570\u636e&#xff0c;\u8f6c\u5316\u4e3a\u7ed3\u6784\u5316\u7684\u6570\u503c\u5f20\u91cf\u300d&#xff0c;\u5177\u4f53\u5206\u4e3a3\u6b65&#xff1a;<\/p>\n<li>\n<p>\u6784\u5efa\u8bcd\u8868&#xff1a;<\/p>\n<li>\n<p>\u91c7\u7528\u5b57\u7b26\u7ea7\u5206\u8bcd&#xff08;\u9010\u4e2a\u62c6\u5206\u4e2d\u6587\u6c49\u5b57\/\u7b26\u53f7&#xff09;&#xff0c;\u7edf\u8ba1\u6240\u6709\u6587\u672c\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8fc7\u6ee4\u4f4e\u9891\u5b57\u7b26&#xff08;\u4f4e\u4e8e\u6700\u5c0f\u8bcd\u9891\u7684\u5b57\u7b26\u5220\u9664&#xff09;&#xff0c;\u8bbe\u7f6e\u8bcd\u8868\u6700\u5927\u5bb9\u91cf\u4e3a4762&#xff0c;\u907f\u514d\u8bcd\u8868\u8fc7\u5927\u5bfc\u81f4\u6a21\u578b\u5197\u4f59\u3002\u8fd9\u91cc4762\u662f\u56e0\u4e3a\u6211\u4e48\u4f7f\u7528\u7684\u817e\u8baf\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u77e9\u9635\u662f4762*200&#xff0c;\u6211\u4eec\u8fd8\u9700\u8981\u7559\u4e24\u4e2a\u8bcd&#xff0c;\u5206\u522b\u8868\u793a\u7a7a\u7f3a\u503c\u548c\u672a\u77e5\u503c\u3002\u56e0\u4e3a\u6211\u4eec\u8981\u4fdd\u8bc1\u8f93\u5165\u5230lstm\u7f51\u7edc\u7684\u6570\u636e\u5927\u5c0f\u662f\u7edf\u4e00\u7684&#xff0c;\u8fd9\u91cc\u6211\u4eec\u8bbe\u5b9a\u4e3a70\u4e2a\u5b57&#xff0c;\u5373\u6bcf\u6b21\u50cf\u4e00\u4e2alstm\u7f51\u7edc\u4e2d\u4f20\u516570\u4e2a\u5b57&#xff0c;\u5f53\u4e00\u6761\u8bc4\u8bba\u4e0d\u8db370\u4e2a\u5b57\u65f6&#xff0c;\u6211\u4eec\u5c31\u7528\u8868\u793a\u7a7a\u7f3a\u7684\u503c&lt;pad&gt;\u6765\u4ee3\u66ff&#xff1b;\u56e0\u4e3a\u6211\u4eec\u817e\u8baf\u7684\u78c1\u5411\u91cf\u77e9\u9635\u8f93\u5165\u53ea\u67094762&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u5728\u6587\u672c\u4e2d\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u51fa\u73b0\u9891\u6b21\u6700\u9ad8\u7684\u524d4760\u4e2a\u5b57&#xff0c;\u5176\u4ed6\u9891\u7387\u4f4e\u7684\u8bcd\u6211\u4eec\u4f7f\u7528\u672a\u77e5\u503c\u6765\u4ee3\u66ff&#xff0c;\u8bbe\u5b9a\u4e3a&lt;unk&gt;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8865\u51452\u4e2a\u7279\u6b8a\u5b57\u7b26&#xff1a;&lt;unk&gt;&#xff08;\u672a\u77e5\u5b57\u7b26&#xff0c;\u5bf9\u5e94\u8bcd\u8868\u4e2d\u672a\u6536\u5f55\u7684\u5b57\u7b26&#xff09;\u3001&lt;pad&gt;&#xff08;\u586b\u5145\u5b57\u7b26&#xff0c;\u7528\u4e8e\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5c06\u5b57\u7b26\u6620\u5c04\u4e3a\u552f\u4e00\u7d22\u5f15&#xff08;\u4ece0\u5f00\u59cb&#xff09;&#xff0c;\u4fdd\u5b58\u8bcd\u8868\u5230\u672c\u5730&#xff08;vocab.pkl&#xff09;&#xff0c;\u4f9b\u540e\u7eed\u6570\u636e\u5904\u7406\u548c\u9884\u6d4b\u65f6\u590d\u7528&#xff08;\u4ec5\u9700\u6784\u5efa\u4e00\u6b21&#xff09;\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>\u6570\u636e\u683c\u5f0f\u5316&#xff1a;<\/p>\n<li>\n<p>\u8bfb\u53d6\u539f\u59cb\u6570\u636e\u96c6&#xff0c;\u63d0\u53d6\u6bcf\u6761\u8bc4\u8bba\u7684\u300c\u60c5\u611f\u6807\u7b7e\u300d&#xff08;\u9996\u5b57\u7b26&#xff0c;0-3\u5206\u522b\u5bf9\u5e944\u7c7b\u60c5\u611f&#xff09;\u548c\u300c\u6587\u672c\u5185\u5bb9\u300d&#xff08;\u53bb\u6389\u6807\u7b7e\u540e\u7684\u8bc4\u8bba\u5185\u5bb9&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6\u4e3a70&#xff1a;\u53e5\u5b50\u957f\u5ea6\u8d85\u8fc770\u65f6&#xff0c;\u622a\u65ad\u591a\u4f59\u90e8\u5206&#xff1b;\u4e0d\u8db370\u65f6&#xff0c;\u7528&lt;pad&gt;\u5b57\u7b26\u586b\u5145\u81f370&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5c06\u6587\u672c\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26&#xff0c;\u901a\u8fc7\u8bcd\u8868\u6620\u5c04\u4e3a\u5bf9\u5e94\u7684\u7d22\u5f15&#xff08;\u672a\u77e5\u5b57\u7b26\u6620\u5c04\u4e3a&lt;unk&gt;\u7684\u7d22\u5f15&#xff09;&#xff0c;\u5b8c\u6210\u300c\u6587\u672c\u2192\u7d22\u5f15\u300d\u7684\u8f6c\u5316\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>\u6570\u636e\u6253\u5305\u4e0e\u62c6\u5206&#xff1a;<\/p>\n<li>\n<p>\u5c06\u5904\u7406\u540e\u7684\u300c\u5b57\u7b26\u7d22\u5f15\u3001\u60c5\u611f\u6807\u7b7e\u3001\u53e5\u5b50\u539f\u59cb\u957f\u5ea6\u300d\u6253\u5305\u4e3a\u5143\u7ec4&#xff0c;\u968f\u673a\u6253\u4e71\u6570\u636e\u987a\u5e8f&#xff08;\u907f\u514d\u6a21\u578b\u5b66\u4e60\u5230\u6570\u636e\u987a\u5e8f\u7684\u5197\u4f59\u7279\u5f81&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u63098:1:1\u7684\u6bd4\u4f8b\u62c6\u5206\u8bad\u7ec3\u96c6&#xff08;\u7528\u4e8e\u6a21\u578b\u8bad\u7ec3&#xff09;\u3001\u9a8c\u8bc1\u96c6&#xff08;\u7528\u4e8e\u76d1\u63a7\u6a21\u578b\u6548\u679c\u3001\u8c03\u6574\u53c2\u6570&#xff09;\u3001\u6d4b\u8bd5\u96c6&#xff08;\u7528\u4e8e\u6700\u7ec8\u8bc4\u4f30\u6a21\u578b\u6cdb\u5316\u80fd\u529b&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa\u6570\u636e\u8fed\u4ee3\u5668&#xff08;DatasetIterater&#xff09;&#xff0c;\u5c06\u6570\u636e\u6309\u6279\u6b21&#xff08;batch_size&#061;128&#xff09;\u8f6c\u4e3aPyTorch\u5f20\u91cf&#xff0c;\u5e76\u52a0\u8f7d\u5230\u6307\u5b9a\u8bbe\u5907&#xff08;CPU\/GPU&#xff09;&#xff0c;\u51cf\u5c11\u6570\u636e\u52a0\u8f7d\u7684\u65f6\u95f4\u5f00\u9500&#xff0c;\u63d0\u5347\u8bad\u7ec3\u6548\u7387\u3002<\/p>\n<\/li>\n<\/li>\n<h3 id=\"%E9%98%B6%E6%AE%B52%EF%BC%9ALSTM%E6%A8%A1%E5%9E%8B%E6%9E%84%E5%BB%BA%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E8%AF%8D%E5%90%91%E9%87%8F%E2%86%92%E6%97%B6%E5%BA%8F%E7%89%B9%E5%BE%81%E2%86%92%E6%83%85%E6%84%9F%E5%88%86%E7%B1%BB%EF%BC%89\">\u9636\u6bb52&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;\u6838\u5fc3&#xff1a;\u8bcd\u5411\u91cf\u2192\u65f6\u5e8f\u7279\u5f81\u2192\u60c5\u611f\u5206\u7c7b&#xff09;<\/h3>\n<p>\u6a21\u578b\u6784\u5efa\u8d34\u5408LSTM\u7684\u65f6\u5e8f\u4f18\u52bf&#xff0c;\u9002\u914d\u6587\u672c\u5206\u7c7b\u573a\u666f&#xff0c;\u91cd\u70b9\u4f18\u5316Embedding\u5c42\u548cLSTM\u5c42\u7684\u8bbe\u8ba1&#xff0c;\u5177\u4f53\u7ed3\u6784\u5206\u4e3a3\u5c42&#xff0c;\u5c42\u5c42\u9012\u8fdb&#xff1a;<\/p>\n<li>\n<p>Embedding\u5c42&#xff08;\u8bcd\u5411\u91cf\u5c42&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u7528\u817e\u8baf\u9884\u8bad\u7ec3200\u7ef4\u8bcd\u5411\u91cf\u521d\u59cb\u5316&#xff0c;\u907f\u514d\u968f\u673a\u521d\u59cb\u5316\u5bfc\u81f4\u7684\u8bcd\u5411\u91cf\u8bed\u4e49\u504f\u5dee&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6epadding_idx\u4e3a\u8bcd\u8868\u6700\u540e\u4e00\u4f4d&#xff08;\u5373&lt;pad&gt;\u7684\u7d22\u5f15&#xff09;&#xff0c;\u8ba9\u6a21\u578b\u5ffd\u7565\u586b\u5145\u5b57\u7b26\u7684\u5f71\u54cd&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5f00\u542f\u5fae\u8c03&#xff08;freeze&#061;False&#xff09;&#xff0c;\u8ba9\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u9002\u914d\u5f53\u524d\u5fae\u535a\u60c5\u611f\u5206\u7c7b\u4efb\u52a1&#xff0c;\u8fdb\u4e00\u6b65\u63d0\u5347\u6a21\u578b\u6548\u679c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u5c06\u5b57\u7b26\u7d22\u5f15&#xff08;\u5982[12,34,56,&#8230;]&#xff09;\u8f6c\u5316\u4e3a200\u7ef4\u8bcd\u5411\u91cf&#xff0c;\u89e3\u51b3\u300c\u6587\u672c\u65e0\u6cd5\u76f4\u63a5\u88ab\u6a21\u578b\u8bc6\u522b\u300d\u7684\u95ee\u9898&#xff08;\u8fd9\u91cc\u66f4\u8be6\u7ec6\u7684\u89e3\u91ca\u662f&#xff0c;\u6bcf\u4e00\u4e2a\u5b57\u5bf9\u5e94\u4e00\u4e2a\u7d22\u5f15\u5f20\u91cf&#xff0c;\u7d22\u5f15\u5f20\u91cf\u5728embedding\u5c42\u4e2d\u4f1a\u81ea\u52a8\u8f6c\u5316\u4e3a4762\u7ef4\u5ea6\u7684\u72ec\u70ed\u7f16\u7801&#xff0c;\u7136\u540e\u7ecf\u8fc7embedding\u5c424762*200\u7684\u77e9\u9635\u8f6c\u5316\u4e3a200\u7ef4\u5ea6\u7684\u8bcd\u5411\u91cf&#xff09;\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>LSTM\u5c42&#xff08;\u65f6\u5e8f\u7279\u5f81\u63d0\u53d6\u5c42&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u91c7\u75283\u5c42\u53cc\u5411LSTM&#xff08;BiLSTM&#xff09;&#xff0c;\u9690\u85cf\u5c42\u7ef4\u5ea6\u4e3a128&#xff0c;dropout&#061;0.3&#xff08;\u9632\u6b62\u8fc7\u62df\u5408&#xff09;&#xff0c;\u8fd9\u91ccdropout\u7684\u9632\u6b62\u8fc7\u62df\u5408\u7b56\u7565\u662f\u6240\u6709\u6a21\u578b\u901a\u7528\u7684&#xff0c;\u5728\u5176\u4ed6\u7f51\u7edc\u5982CNN\u540c\u6837\u9002\u7528\u3002\u539f\u7406\u662f\u6a21\u4eff\u4eba\u8111\u7684\u795e\u7ecf\u5143\u7ed3\u6784&#xff0c;\u56e0\u4e3a\u4eba\u8111\u4e5f\u4e0d\u662f\u6240\u6709\u795e\u7ecf\u5143\u90fd\u88ab\u540c\u65f6\u4f7f\u7528\u7684&#xff0c;\u8fd9\u91ccdropout&#061;0.3\u8868\u793a\u4ee4\u767e\u5206\u4e4b\u4e09\u5341\u7684\u795e\u7ecf\u5143\u53c2\u6570w\u4e3a0\u3002<\/p>\n<\/li>\n<li>\n<p>\u53cc\u5411LSTM\u7684\u4f18\u52bf&#xff1a;\u76f8\u6bd4\u5355\u5411LSTM&#xff0c;\u80fd\u540c\u65f6\u6355\u6349\u6587\u672c\u7684\u6b63\u5411&#xff08;\u4ece\u5de6\u5230\u53f3&#xff09;\u548c\u53cd\u5411&#xff08;\u4ece\u53f3\u5230\u5de6&#xff09;\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u6bd4\u5982\u53e5\u5b50\u300c\u8fd9\u90e8\u7535\u5f71\u5f00\u5934\u5f88\u5e73\u6de1&#xff0c;\u4f46\u7ed3\u5c3e\u5374\u975e\u5e38\u7cbe\u5f69\u300d&#xff0c;\u6b63\u5411LSTM\u6355\u6349\u5f00\u5934\u7684\u5e73\u6de1\u63cf\u8ff0&#xff0c;\u53cd\u5411LSTM\u6355\u6349\u7ed3\u5c3e\u7684\u7cbe\u5f69\u8bc4\u4ef7&#xff0c;\u4e24\u8005\u878d\u5408\u80fd\u66f4\u7cbe\u51c6\u5224\u65ad\u60c5\u611f\u503e\u5411&#xff0c;\u66f4\u9002\u5408\u60c5\u611f\u5206\u7c7b\u4efb\u52a1&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u63d0\u53d6\u6587\u672c\u7684\u65f6\u5e8f\u7279\u5f81&#xff0c;\u6355\u6349\u53e5\u5b50\u4e2d\u5b57\u7b26\u4e4b\u95f4\u7684\u8bed\u4e49\u5173\u8054&#xff08;\u5982\u300c\u5f00\u5fc3\u300d\u300c\u6109\u5feb\u300d\u7b49\u8bcd\u7684\u60c5\u611f\u5173\u8054&#xff09;&#xff0c;\u8f93\u51fa\u7ef4\u5ea6\u4e3a[batch_size, 70, 256]&#xff08;128\u00d72&#xff0c;\u53cc\u5411LSTM\u7684\u6b63\u5411\u548c\u53cd\u5411\u9690\u85cf\u5c42\u8f93\u51fa\u62fc\u63a5&#xff09;\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>\u5168\u8fde\u63a5\u5c42&#xff08;\u5206\u7c7b\u5c42&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u8f93\u5165\u7ef4\u5ea6\u4e3a256&#xff08;\u53cc\u5411LSTM\u6700\u540e\u4e00\u6b65\u7684\u8f93\u51fa&#xff0c;\u5373[batch_size, 256]&#xff09;&#xff0c;\u8f93\u51fa\u7ef4\u5ea6\u4e3a4&#xff08;\u5bf9\u5e944\u7c7b\u60c5\u611f&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u5c06LSTM\u63d0\u53d6\u7684\u65f6\u5e8f\u7279\u5f81&#xff0c;\u6620\u5c04\u4e3a4\u7c7b\u60c5\u611f\u7684\u9884\u6d4b\u6982\u7387&#xff0c;\u5b8c\u6210\u6700\u7ec8\u7684\u5206\u7c7b\u4efb\u52a1\u3002<\/p>\n<\/li>\n<\/li>\n<h3 id=\"%E9%98%B6%E6%AE%B53%EF%BC%9A%E8%AE%AD%E7%BB%83%E4%B8%8E%E6%B5%8B%E8%AF%95%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%9A%E4%BC%98%E5%8C%96%E6%A8%A1%E5%9E%8B%E5%8F%82%E6%95%B0%EF%BC%8C%E8%AF%84%E4%BC%B0%E6%A8%A1%E5%9E%8B%E6%95%88%E6%9E%9C%EF%BC%89\">\u9636\u6bb53&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;\u6838\u5fc3&#xff1a;\u4f18\u5316\u6a21\u578b\u53c2\u6570&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff09;<\/h3>\n<p>\u8bad\u7ec3\u4e0e\u6d4b\u8bd5\u7684\u6838\u5fc3\u662f\u300c\u8ba9\u6a21\u578b\u5b66\u4e60\u5230\u6587\u672c\u4e0e\u60c5\u611f\u7684\u5173\u8054&#xff0c;\u540c\u65f6\u907f\u514d\u8fc7\u62df\u5408&#xff0c;\u786e\u4fdd\u6a21\u578b\u5728\u65b0\u6570\u636e\u4e0a\u7684\u6cdb\u5316\u80fd\u529b\u300d&#xff0c;\u5177\u4f53\u5206\u4e3a2\u6b65&#xff0c;\u52a0\u5165\u5b9e\u7528\u4f18\u5316\u7b56\u7565&#xff1a;<\/p>\n<li>\n<p>\u8bad\u7ec3\u73af\u8282&#xff1a;<\/p>\n<li>\n<p>\u4f18\u5316\u5668&#xff1a;\u9009\u7528Adam\u4f18\u5316\u5668&#xff08;\u5b66\u4e60\u73870.001&#xff09;&#xff0c;\u6536\u655b\u901f\u5ea6\u5feb\u3001\u9002\u5408\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1&#xff0c;\u76f8\u6bd4SGD\u80fd\u66f4\u597d\u5730\u907f\u514d\u5c40\u90e8\u6700\u4f18&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u635f\u5931\u51fd\u6570&#xff1a;\u9009\u7528\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570&#xff08;F.cross_entropy&#xff09;&#xff0c;\u9002\u914d\u591a\u5206\u7c7b\u4efb\u52a1&#xff0c;\u80fd\u6709\u6548\u8861\u91cf\u9884\u6d4b\u6982\u7387\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u7684\u5dee\u8ddd&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u76d1\u63a7\u7b56\u7565&#xff1a;\u6bcf100\u6279\u6b21\u8f93\u51fa\u4e00\u6b21\u8bad\u7ec3\u96c6\u635f\u5931\u3001\u8bad\u7ec3\u96c6\u51c6\u786e\u7387&#xff0c;\u4ee5\u53ca\u9a8c\u8bc1\u96c6\u635f\u5931\u3001\u9a8c\u8bc1\u96c6\u51c6\u786e\u7387&#xff0c;\u76f4\u89c2\u76d1\u63a7\u6a21\u578b\u8bad\u7ec3\u6548\u679c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u65e9\u505c\u7b56\u7565&#xff1a;\u82e5\u8fde\u7eed10000\u6279\u6b21\u9a8c\u8bc1\u96c6\u635f\u5931\u65e0\u63d0\u5347&#xff0c;\u81ea\u52a8\u505c\u6b62\u8bad\u7ec3&#xff0c;\u907f\u514d\u6a21\u578b\u8fc7\u5ea6\u8bad\u7ec3\u5bfc\u81f4\u8fc7\u62df\u5408&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6a21\u578b\u4fdd\u5b58&#xff1a;\u4fdd\u5b58\u9a8c\u8bc1\u96c6\u635f\u5931\u6700\u4f18\u7684\u6a21\u578b&#xff08;TextRNN.ckpt&#xff09;&#xff0c;\u540e\u7eed\u6d4b\u8bd5\u548c\u9884\u6d4b\u76f4\u63a5\u52a0\u8f7d\u8be5\u6a21\u578b&#xff0c;\u65e0\u9700\u91cd\u65b0\u8bad\u7ec3\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bad\u7ec3\u53ef\u89c6\u5316&#xff1a;\u52a0\u5165TensorBoardX\u53ef\u89c6\u5316\u5de5\u5177&#xff0c;\u76d1\u63a7\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u635f\u5931\u548c\u51c6\u786e\u7387\u53d8\u5316&#xff0c;\u89e3\u51b3\u300c\u76f2\u8bad\u300d\u95ee\u9898&#xff0c;\u76f4\u89c2\u5224\u65ad\u6a21\u578b\u6536\u655b\u60c5\u51b5\u3002<\/p>\n<\/li>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u73af\u8282&#xff1a;<\/p>\n<li>\n<p>\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6700\u4f18\u6a21\u578b&#xff0c;\u5207\u6362\u4e3a\u8bc4\u4f30\u6a21\u5f0f&#xff08;model.eval()&#xff09;&#xff0c;\u5173\u95eddropout\u548c\u68af\u5ea6\u8ba1\u7b97&#xff0c;\u8282\u7701\u5185\u5b58&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fd0\u884c\u6a21\u578b&#xff0c;\u8ba1\u7b97\u6d4b\u8bd5\u96c6\u635f\u5931\u3001\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f93\u51fa\u5206\u7c7b\u62a5\u544a&#xff0c;\u5305\u542b4\u7c7b\u60c5\u611f\u7684\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u503c&#xff0c;\u5168\u9762\u8bc4\u4f30\u6a21\u578b\u5728\u6bcf\u7c7b\u60c5\u611f\u4e0a\u7684\u5206\u7c7b\u6548\u679c&#xff08;\u6bd4\u5982\u6a21\u578b\u5bf9\u300c\u559c\u60a6\u300d\u7c7b\u8bc4\u8bba\u7684\u8bc6\u522b\u51c6\u786e\u7387&#xff0c;\u5bf9\u300c\u4f4e\u843d\u300d\u7c7b\u8bc4\u8bba\u7684\u53ec\u56de\u7387\u7b49&#xff09;\u3002<\/p>\n<\/li>\n<\/li>\n<h2 id=\"%E5%9B%9B%E3%80%81%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0\">\u56db\u3001\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p>\u4ee3\u7801\u6309\u300c\u529f\u80fd\u6a21\u5757\u5316\u300d\u62c6\u5206&#xff0c;\u6bcf\u4e2a\u6a21\u5757\u5355\u72ec\u4e00\u4e2a\u6587\u4ef6&#xff0c;\u7ed3\u6784\u6e05\u6670\u3001\u4fbf\u4e8e\u7ef4\u62a4\u548c\u590d\u7528&#xff0c;\u540c\u65f6\u6dfb\u52a0\u8be6\u7ec6\u6ce8\u91ca&#xff0c;\u65b0\u624b\u53ef\u9010\u6a21\u5757\u7406\u89e3&#xff0c;\u4e5f\u53ef\u76f4\u63a5\u590d\u5236\u5230\u5bf9\u5e94\u6587\u4ef6\u4e2d\u8fd0\u884c\u3002\u6240\u6709\u4ee3\u7801\u57fa\u4e8ePython 3.7&#043;\u3001PyTorch 1.10&#043;\u7f16\u5199&#xff0c;\u786e\u4fdd\u517c\u5bb9\u6027\u3002<\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%971%EF%BC%9A%E8%AF%8D%E8%A1%A8%E6%9E%84%E5%BB%BA%EF%BC%88build_vocab.py%EF%BC%89\">\u6a21\u57571&#xff1a;\u8bcd\u8868\u6784\u5efa&#xff08;build_vocab.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u6784\u5efa\u5b57\u7b26\u7ea7\u8bcd\u8868&#xff0c;\u8fc7\u6ee4\u4f4e\u9891\u8bcd&#xff0c;\u4fdd\u5b58\u8bcd\u8868\u5230\u672c\u5730&#xff08;\u4ec5\u9700\u8fd0\u884c\u4e00\u6b21&#xff09;\u3002<\/p>\n<p>from tqdm import tqdm<br \/>\nimport pickle as pkl  # \u7528\u4e8e\u4fdd\u5b58\/\u52a0\u8f7d\u8bcd\u8868<\/p>\n<p>max_vocab_size &#061; 4760  # \u8bcd\u8868\u6700\u5927\u5bb9\u91cf<br \/>\nunk, pad &#061; &#039;&lt;unk&gt;&#039;, &#039;&lt;pad&gt;&#039;  # \u672a\u77e5\u5b57\u7b26\/\u586b\u5145\u5b57\u7b26&#xff0c;\u4e0e\u540e\u7eed\u6570\u636e\u5904\u7406\u4fdd\u6301\u4e00\u81f4<\/p>\n<p>def build_vocab(file_path, max_size, min_freq):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u6784\u5efa\u5b57\u7b26\u7ea7\u8bcd\u8868&#xff1a;\u5b57\u7b26\u5206\u8bcd&#043;\u9891\u7387\u7edf\u8ba1&#043;\u4f4e\u9891\u8fc7\u6ee4&#043;\u4fdd\u5b58\u8bcd\u8868<br \/>\n    :param file_path: \u6570\u636e\u96c6\u8def\u5f84&#xff08;simplifyweibo_4_moods.csv&#xff09;<br \/>\n    :param max_size: \u8bcd\u8868\u6700\u5927\u5bb9\u91cf<br \/>\n    :param min_freq: \u6700\u5c0f\u8bcd\u9891&#xff08;\u4f4e\u4e8e\u8be5\u503c\u7684\u5b57\u7b26\u5c06\u88ab\u8fc7\u6ee4&#xff09;<br \/>\n    :return: \u6784\u5efa\u597d\u7684\u8bcd\u8868&#xff08;\u5b57\u5178&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15&#xff09;<br \/>\n    &#034;&#034;&#034;<br \/>\n    # \u5b57\u7b26\u7ea7\u5206\u8bcd\u5668&#xff1a;\u9010\u4e2a\u62c6\u5206\u4e2d\u6587\u6c49\u5b57\/\u7b26\u53f7&#xff08;\u9002\u914d\u4e2d\u6587\u5fae\u535a\u8bc4\u8bba\u573a\u666f&#xff09;<br \/>\n    tokenizer &#061; lambda x: [y for y in x]<br \/>\n    vocab_dict &#061; {}  # \u7528\u4e8e\u7edf\u8ba1\u5b57\u7b26\u9891\u7387&#xff08;key&#xff1a;\u5b57\u7b26&#xff0c;value&#xff1a;\u9891\u7387&#xff09;<\/p>\n<p>    # \u8bfb\u53d6\u6570\u636e\u96c6&#xff0c;\u7edf\u8ba1\u5b57\u7b26\u9891\u7387<br \/>\n    with open(file_path, &#039;r&#039;, encoding&#061;&#039;utf-8&#039;) as f:<br \/>\n        i &#061; 0<br \/>\n        for line in tqdm(f, desc&#061;&#034;\u6b63\u5728\u6784\u5efa\u8bcd\u8868&#034;):<br \/>\n            if i &#061;&#061; 0:  # \u8df3\u8fc7\u6570\u636e\u96c6\u8868\u5934&#xff08;\u7b2c\u4e00\u884c&#xff09;<br \/>\n                i &#043;&#061; 1<br \/>\n                continue<br \/>\n            lin &#061; line[2:].strip()  # \u53bb\u6389\u6807\u7b7e&#xff08;\u9996\u5b57\u7b26&#xff09;&#xff0c;\u63d0\u53d6\u6587\u672c\u5185\u5bb9<br \/>\n            if not lin:  # \u8df3\u8fc7\u7a7a\u884c&#xff08;\u907f\u514d\u65e0\u6548\u6570\u636e&#xff09;<br \/>\n                continue<br \/>\n            # \u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u9891\u7387<br \/>\n            for word in tokenizer(lin):<br \/>\n                vocab_dict[word] &#061; vocab_dict.get(word, 0) &#043; 1<\/p>\n<p>    # \u8fc7\u6ee4\u4f4e\u9891\u8bcd&#xff0c;\u5e76\u6309\u9891\u7387\u964d\u5e8f\u6392\u5e8f&#xff08;\u4fdd\u7559\u9ad8\u9891\u5b57\u7b26&#xff09;<br \/>\n    vocab_list &#061; sorted([_ for _ in vocab_dict.items() if _[1] &gt;&#061; min_freq],<br \/>\n                        key&#061;lambda x: x[1], reverse&#061;True)[:max_size]<br \/>\n    # \u5c06\u5b57\u7b26\u6620\u5c04\u4e3a\u7d22\u5f15&#xff08;\u4ece0\u5f00\u59cb&#xff09;<br \/>\n    vocab_dict &#061; {word_count[0]: idx for idx, word_count in enumerate(vocab_list)}<br \/>\n    # \u8865\u5145\u672a\u77e5\u5b57\u7b26&#xff08;unk&#xff09;\u548c\u586b\u5145\u5b57\u7b26&#xff08;pad&#xff09;\u7684\u7d22\u5f15&#xff08;\u653e\u5728\u8bcd\u8868\u6700\u540e&#xff09;<br \/>\n    vocab_dict[unk] &#061; len(vocab_dict)<br \/>\n    vocab_dict[pad] &#061; len(vocab_dict)<\/p>\n<p>    # \u4fdd\u5b58\u8bcd\u8868\u5230\u672c\u5730&#xff08;\u540e\u7eed\u6570\u636e\u5904\u7406\u3001\u9884\u6d4b\u65f6\u76f4\u63a5\u52a0\u8f7d&#xff0c;\u65e0\u9700\u91cd\u65b0\u6784\u5efa&#xff09;<br \/>\n    pkl.dump(vocab_dict, open(&#039;vocab.pkl&#039;, &#039;wb&#039;))<br \/>\n    print(f&#034;\u8bcd\u8868\u6784\u5efa\u5b8c\u6210&#xff01;\u8bcd\u8868\u603b\u5927\u5c0f&#xff1a;{len(vocab_dict)}&#xff08;\u542bunk\u548cpad&#xff09;&#034;)  # \u9884\u671f\u5927\u5c0f&#xff1a;4762&#xff08;4760&#043;2&#xff09;<br \/>\n    print(f&#034;\u8bcd\u8868\u793a\u4f8b&#xff1a;{list(vocab_dict.items())}&#034;)<br \/>\n    return vocab_dict<\/p>\n<p># \u8c03\u7528\u793a\u4f8b&#xff1a;\u8fd0\u884c\u8be5\u51fd\u6570\u6784\u5efa\u8bcd\u8868&#xff08;\u4ec5\u9700\u8fd0\u884c\u4e00\u6b21&#xff09;<br \/>\n# \u8bf7\u6839\u636e\u5b9e\u9645\u6570\u636e\u96c6\u8def\u5f84\u4fee\u6539file_path&#xff0c;min_freq\u5efa\u8bae\u8bbe\u4e3a5&#xff08;\u8fc7\u6ee4\u6781\u4f4e\u9891\u5b57\u7b26&#xff09;<br \/>\nbuild_vocab(&#034;simplifyweibo_4_moods.csv&#034;, max_vocab_size, min_freq&#061;5) <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"973\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163252-6984c6347c22a.png\" width=\"1676\" \/><\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%972%EF%BC%9A%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD%E4%B8%8E%E8%BF%AD%E4%BB%A3%E5%99%A8%EF%BC%88load_dataset.py%EF%BC%89\">\u6a21\u57572&#xff1a;\u6570\u636e\u52a0\u8f7d\u4e0e\u8fed\u4ee3\u5668&#xff08;load_dataset.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u52a0\u8f7d\u8bcd\u8868&#xff0c;\u5904\u7406\u539f\u59cb\u6570\u636e&#xff08;\u7edf\u4e00\u957f\u5ea6\u3001\u5b57\u7b26\u8f6c\u7d22\u5f15&#xff09;&#xff0c;\u62c6\u5206\u6570\u636e\u96c6&#xff0c;\u6784\u5efa\u6570\u636e\u8fed\u4ee3\u5668&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165\u3002<\/p>\n<p>from tqdm import tqdm<br \/>\nimport pickle as pkl<br \/>\nimport random<br \/>\nimport torch<\/p>\n<p>unk, pad &#061; &#039;&lt;unk&gt;&#039;, &#039;&lt;pad&gt;&#039;  # \u4e0e\u8bcd\u8868\u6784\u5efa\u65f6\u4fdd\u6301\u4e00\u81f4<\/p>\n<p>def load_dataset(path, pad_size&#061;70):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u5904\u7406\u539f\u59cb\u6570\u636e&#xff1a;\u52a0\u8f7d\u8bcd\u8868\u2192\u5b57\u7b26\u8f6c\u7d22\u5f15\u2192\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6\u2192\u62c6\u5206\u6570\u636e\u96c6<br \/>\n    :param path: \u6570\u636e\u96c6\u8def\u5f84&#xff08;simplifyweibo_4_moods.csv&#xff09;<br \/>\n    :param pad_size: \u53e5\u5b50\u7edf\u4e00\u957f\u5ea6&#xff08;\u9ed8\u8ba470&#xff0c;\u4e0e\u4efb\u52a1\u8981\u6c42\u4e00\u81f4&#xff09;<br \/>\n    :return: \u8bcd\u8868\u3001\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u3001\u6d4b\u8bd5\u96c6<br \/>\n    &#034;&#034;&#034;<br \/>\n    contents &#061; []  # \u7528\u4e8e\u5b58\u50a8\u5904\u7406\u540e\u7684\u6570\u636e&#xff08;\u6bcf\u4e2a\u5143\u7d20&#xff1a;(\u5b57\u7b26\u7d22\u5f15\u5217\u8868, \u6807\u7b7e, \u539f\u59cb\u53e5\u5b50\u957f\u5ea6)&#xff09;<br \/>\n    vocab &#061; pkl.load(open(&#039;vocab.pkl&#039;, &#039;rb&#039;))  # \u52a0\u8f7d\u5df2\u6784\u5efa\u597d\u7684\u8bcd\u8868<br \/>\n    tokenizer &#061; lambda x: [y for y in x]  # \u5b57\u7b26\u7ea7\u5206\u8bcd\u5668&#xff08;\u4e0e\u8bcd\u8868\u6784\u5efa\u4e00\u81f4&#xff09;<\/p>\n<p>    # \u8bfb\u53d6\u5e76\u5904\u7406\u539f\u59cb\u6570\u636e<br \/>\n    with open(path, &#039;r&#039;, encoding&#061;&#039;utf-8&#039;) as f:<br \/>\n        i &#061; 0<br \/>\n        for line in tqdm(f, desc&#061;&#034;\u6b63\u5728\u5904\u7406\u6570\u636e&#034;):<br \/>\n            if i &#061;&#061; 0:  # \u8df3\u8fc7\u8868\u5934<br \/>\n                i &#043;&#061; 1<br \/>\n                continue<br \/>\n            if not line:  # \u8df3\u8fc7\u7a7a\u884c<br \/>\n                continue<br \/>\n            # \u63d0\u53d6\u6807\u7b7e&#xff08;\u9996\u5b57\u7b26&#xff0c;\u8f6c\u4e3a\u6574\u6570&#xff09;\u548c\u6587\u672c\u5185\u5bb9&#xff08;\u53bb\u6389\u6807\u7b7e\u548c\u6362\u884c\u7b26&#xff09;<br \/>\n            label &#061; int(line[0])<br \/>\n            content &#061; line[2:].strip(&#034;\\\\n&#034;)<\/p>\n<p>            # \u5b57\u7b26\u5206\u8bcd\u2192\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6\u2192\u5b57\u7b26\u8f6c\u7d22\u5f15<br \/>\n            token &#061; tokenizer(content)  # \u5206\u8bcd&#xff08;\u9010\u4e2a\u5b57\u7b26&#xff09;<br \/>\n            seq_len &#061; len(token)        # \u8bb0\u5f55\u53e5\u5b50\u539f\u59cb\u957f\u5ea6&#xff08;\u5907\u7528&#xff09;<br \/>\n            # \u7edf\u4e00\u957f\u5ea6&#xff1a;\u622a\u65ad\u6216\u586b\u5145<br \/>\n            if seq_len &gt;&#061; pad_size:<br \/>\n                token &#061; token[:pad_size]  # \u8d85\u8fc770&#xff0c;\u622a\u65ad\u591a\u4f59\u90e8\u5206<br \/>\n                seq_len &#061; pad_size<br \/>\n            else:<br \/>\n                token.extend([pad] * (pad_size &#8211; len(token)))  # \u4e0d\u8db370&#xff0c;\u586b\u5145pad<\/p>\n<p>            # \u5c06\u5b57\u7b26\u6620\u5c04\u4e3a\u7d22\u5f15&#xff08;\u672a\u77e5\u5b57\u7b26\u7528unk\u7684\u7d22\u5f15&#xff09;<br \/>\n            words_line &#061; [vocab.get(word, vocab[unk]) for word in token]<br \/>\n            contents.append((words_line, label, seq_len))<\/p>\n<p>    # \u968f\u673a\u6253\u4e71\u6570\u636e&#xff08;\u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u53ef\u4fdd\u8bc1\u7ed3\u679c\u53ef\u590d\u73b0&#xff09;<br \/>\n    random.shuffle(contents)<br \/>\n    # \u63098:1:1\u62c6\u5206\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u3001\u6d4b\u8bd5\u96c6<br \/>\n    train_data &#061; contents[:int(len(contents)*0.8)]<br \/>\n    dev_data &#061; contents[int(len(contents)*0.8):int(len(contents)*0.9)]<br \/>\n    test_data &#061; contents[int(len(contents)*0.9):]<br \/>\n    return vocab, train_data, dev_data, test_data<\/p>\n<p>class DatasetIterater(object):<br \/>\n    &#034;&#034;&#034;\u6570\u636e\u8fed\u4ee3\u5668&#xff1a;\u5c06\u5904\u7406\u597d\u7684\u6570\u636e\u8f6c\u4e3aPyTorch\u5f20\u91cf&#xff0c;\u5e76\u6309\u6279\u6b21\u8fd4\u56de&#xff0c;\u9002\u914d\u6a21\u578b\u8bad\u7ec3\/\u6d4b\u8bd5&#034;&#034;&#034;<br \/>\n    def __init__(self, batches, batch_size, device):<br \/>\n        self.batch_size &#061; batch_size  # \u6279\u6b21\u5927\u5c0f&#xff08;\u4e0emain.py\u4e2d\u4e00\u81f4&#xff09;<br \/>\n        self.batches &#061; batches        # \u8f93\u5165\u6570\u636e&#xff08;\u8bad\u7ec3\/\u9a8c\u8bc1\/\u6d4b\u8bd5\u96c6&#xff09;<br \/>\n        self.n_batches &#061; len(batches) \/\/ batch_size  # \u603b\u6279\u6b21\u6570\u91cf&#xff08;\u6574\u6570\u90e8\u5206&#xff09;<br \/>\n        self.residue &#061; len(batches) % self.n_batches !&#061; 0  # \u662f\u5426\u6709\u5269\u4f59\u6570\u636e&#xff08;\u4e0d\u8db3\u4e00\u4e2a\u6279\u6b21&#xff09;<br \/>\n        self.index &#061; 0  # \u5f53\u524d\u6279\u6b21\u7d22\u5f15<br \/>\n        self.device &#061; device  # \u6570\u636e\u52a0\u8f7d\u5230\u7684\u8bbe\u5907&#xff08;CPU\/GPU&#xff09;<\/p>\n<p>    def _to_tensor(self, datas):<br \/>\n        &#034;&#034;&#034;\u5c06\u5355\u4e2a\u6279\u6b21\u7684\u6570\u636e\u8f6c\u4e3aPyTorch\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165\u683c\u5f0f&#034;&#034;&#034;<br \/>\n        # x&#xff1a;\u5b57\u7b26\u7d22\u5f15\u5f20\u91cf&#xff08;[batch_size, pad_size]&#xff09;<br \/>\n        x &#061; torch.LongTensor([_[0] for _ in datas]).to(self.device)<br \/>\n        # y&#xff1a;\u6807\u7b7e\u5f20\u91cf&#xff08;[batch_size]&#xff09;<br \/>\n        y &#061; torch.LongTensor([_[1] for _ in datas]).to(self.device)<br \/>\n        # seq_len&#xff1a;\u53e5\u5b50\u539f\u59cb\u957f\u5ea6\u5f20\u91cf&#xff08;\u5907\u7528&#xff0c;\u6a21\u578b\u672a\u7528\u5230&#xff0c;\u4ec5\u4fdd\u6301\u8f93\u5165\u683c\u5f0f\u7edf\u4e00&#xff09;<br \/>\n        seq_len &#061; torch.LongTensor([_[2] for _ in datas]).to(self.device)<br \/>\n        return (x, seq_len), y<\/p>\n<p>    def __next__(self):<br \/>\n        &#034;&#034;&#034;\u8fed\u4ee3\u5668&#xff1a;\u8fd4\u56de\u4e0b\u4e00\u4e2a\u6279\u6b21\u7684\u6570\u636e&#xff0c;\u7ed3\u675f\u65f6\u629b\u51faStopIteration&#034;&#034;&#034;<br \/>\n        if self.residue and self.index &#061;&#061; self.n_batches:<br \/>\n            # \u5904\u7406\u5269\u4f59\u6570\u636e&#xff08;\u4e0d\u8db3\u4e00\u4e2a\u6279\u6b21&#xff09;<br \/>\n            batches &#061; self.batches[self.index*self.batch_size:]<br \/>\n            self.index &#043;&#061; 1<br \/>\n            return self._to_tensor(batches)<br \/>\n        elif self.index &gt;&#061; self.n_batches:<br \/>\n            # \u6240\u6709\u6279\u6b21\u8fed\u4ee3\u5b8c\u6210&#xff0c;\u91cd\u7f6e\u7d22\u5f15\u5e76\u629b\u51fa\u505c\u6b62\u4fe1\u53f7<br \/>\n            self.index &#061; 0<br \/>\n            raise StopIteration<br \/>\n        else:<br \/>\n            # \u8fd4\u56de\u5f53\u524d\u6279\u6b21\u7684\u6570\u636e&#xff0c;\u7d22\u5f15\u52a01<br \/>\n            batches &#061; self.batches[self.index*self.batch_size:(self.index&#043;1)*self.batch_size]<br \/>\n            self.index &#043;&#061; 1<br \/>\n            return self._to_tensor(batches)<\/p>\n<p>    def __iter__(self):<br \/>\n        &#034;&#034;&#034;\u8fd4\u56de\u8fed\u4ee3\u5668\u81ea\u8eab&#xff08;\u9002\u914dfor\u5faa\u73af\u8fed\u4ee3&#xff09;&#034;&#034;&#034;<br \/>\n        return self<\/p>\n<p>    def __len__(self):<br \/>\n        &#034;&#034;&#034;\u8fd4\u56de\u603b\u6279\u6b21\u6570\u91cf&#xff08;\u542b\u5269\u4f59\u6279\u6b21&#xff09;&#034;&#034;&#034;<br \/>\n        return self.n_batches &#043; 1 if self.residue else self.n_batches <\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%973%EF%BC%9ALSTM%E6%A8%A1%E5%9E%8B%E6%9E%84%E5%BB%BA%EF%BC%88TextRNN.py%EF%BC%89\">\u6a21\u57573&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;TextRNN.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u5b9a\u4e49LSTM\u6a21\u578b\u7ed3\u6784&#xff0c;\u9002\u914dEmbedding\u5c42\u3001\u53cc\u5411LSTM\u5c42\u3001\u5168\u8fde\u63a5\u5c42&#xff0c;\u8d34\u5408\u4efb\u52a1\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<p>import torch.nn as nn<\/p>\n<p>class Model(nn.Module):<br \/>\n    def __init__(self, embedding_pretrained, n_vocab, embed, num_classes):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u521d\u59cb\u5316LSTM\u6a21\u578b<br \/>\n        :param embedding_pretrained: \u9884\u8bad\u7ec3\u8bcd\u5411\u91cf&#xff08;None\u8868\u793a\u968f\u673a\u521d\u59cb\u5316&#xff09;<br \/>\n        :param n_vocab: \u8bcd\u8868\u5927\u5c0f<br \/>\n        :param embed: \u8bcd\u5411\u91cf\u7ef4\u5ea6&#xff08;\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u4e3a200\u7ef4&#xff09;<br \/>\n        :param num_classes: \u7c7b\u522b\u6570&#xff08;4\u7c7b\u60c5\u611f&#xff09;<br \/>\n        &#034;&#034;&#034;<br \/>\n        super(Model, self).__init__()<br \/>\n        # 1. Embedding\u5c42&#xff08;\u8bcd\u5411\u91cf\u5c42&#xff09;<br \/>\n        if embedding_pretrained is not None:<br \/>\n            # \u7528\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u521d\u59cb\u5316&#xff0c;\u5f00\u542f\u5fae\u8c03&#xff08;freeze&#061;False&#xff09;<br \/>\n            self.embedding &#061; nn.Embedding.from_pretrained(<br \/>\n                embedding_pretrained,<br \/>\n                padding_idx&#061;n_vocab-1,  # pad\u7684\u7d22\u5f15\u4e3a\u8bcd\u8868\u6700\u540e\u4e00\u4f4d<br \/>\n                freeze&#061;False<br \/>\n            )<br \/>\n        else:<br \/>\n            # \u65e0\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u65f6&#xff0c;\u968f\u673a\u521d\u59cb\u5316\u8bcd\u5411\u91cf<br \/>\n            self.embedding &#061; nn.Embedding(n_vocab, embed, padding_idx&#061;n_vocab-1)<\/p>\n<p>        # 2. \u53cc\u5411LSTM\u5c42&#xff08;\u65f6\u5e8f\u7279\u5f81\u63d0\u53d6&#xff09;<br \/>\n        self.lstm &#061; nn.LSTM(<br \/>\n            embed, 128, 3,  # \u8f93\u5165\u7ef4\u5ea6&#xff08;\u8bcd\u5411\u91cf\u7ef4\u5ea6&#xff09;\u3001\u9690\u85cf\u5c42\u7ef4\u5ea6\u3001\u5c42\u6570<br \/>\n            bidirectional&#061;True,  # \u5f00\u542f\u53cc\u5411<br \/>\n            batch_first&#061;True,   # \u8f93\u5165\u683c\u5f0f&#xff1a;[batch_size, seq_len, embed_dim]<br \/>\n            dropout&#061;0.3         # dropout\u9632\u6b62\u8fc7\u62df\u5408&#xff08;\u4ec5\u4e2d\u95f4\u5c42\u751f\u6548&#xff09;<br \/>\n        )<\/p>\n<p>        # 3. \u5168\u8fde\u63a5\u5c42&#xff08;\u5206\u7c7b\u5c42&#xff09;&#xff1a;\u53cc\u5411LSTM\u8f93\u51fa\u7ef4\u5ea6&#061;128\u00d72&#061;256<br \/>\n        self.fc &#061; nn.Linear(128*2, num_classes)<\/p>\n<p>    def forward(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u6a21\u578b\u524d\u5411\u4f20\u64ad&#xff08;\u8f93\u5165\u2192Embedding\u2192LSTM\u2192\u5168\u8fde\u63a5\u2192\u8f93\u51fa&#xff09;<br \/>\n        :param x: \u8f93\u5165\u6570\u636e&#xff08;\u5143\u7ec4&#xff1a;(\u5b57\u7b26\u7d22\u5f15\u5f20\u91cf, \u53e5\u5b50\u957f\u5ea6\u5f20\u91cf)&#xff09;<br \/>\n        :return: \u6a21\u578b\u9884\u6d4b\u6982\u7387&#xff08;[batch_size, num_classes]&#xff09;<br \/>\n        &#034;&#034;&#034;<br \/>\n        # x\u662f\u5143\u7ec4(x_idx, seq_len)&#xff0c;\u4ec5\u4f7f\u7528\u5b57\u7b26\u7d22\u5f15\u90e8\u5206&#xff08;seq_len\u5907\u7528&#xff09;<br \/>\n        x, _ &#061; x<br \/>\n        # Embedding\u5c42&#xff1a;[batch_size, 70] \u2192 [batch_size, 70, 200]<br \/>\n        out &#061; self.embedding(x)<br \/>\n        # LSTM\u5c42&#xff1a;\u8f93\u51fa[batch_size, 70, 256]&#xff0c;\u53d6\u6700\u540e\u4e00\u6b65\u8f93\u51fa[batch_size, 256]<br \/>\n        out, _ &#061; self.lstm(out)<br \/>\n        out &#061; out[:, -1, :]  # \u53d6\u6700\u540e\u4e00\u6b65\u8f93\u51fa&#xff0c;\u4f5c\u4e3a\u6587\u672c\u7684\u6574\u4f53\u7279\u5f81<br \/>\n        # \u5168\u8fde\u63a5\u5c42&#xff1a;[batch_size, 256] \u2192 [batch_size, 4]&#xff08;4\u7c7b\u60c5\u611f\u7684\u9884\u6d4b\u6982\u7387&#xff09;<br \/>\n        out &#061; self.fc(out)<br \/>\n        return out <\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%974%EF%BC%9A%E8%AE%AD%E7%BB%83%E4%B8%8E%E6%B5%8B%E8%AF%95%EF%BC%88train_eval_test.py%EF%BC%89\">\u6a21\u57574&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;train_eval_test.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u5b9a\u4e49\u6a21\u578b\u8bad\u7ec3\u3001\u8bc4\u4f30\u3001\u6d4b\u8bd5\u51fd\u6570&#xff0c;\u52a0\u5165\u65e9\u505c\u7b56\u7565\u3001\u8bad\u7ec3\u53ef\u89c6\u5316&#xff0c;\u8f93\u51fa\u8bad\u7ec3\u65e5\u5fd7\u548c\u6d4b\u8bd5\u62a5\u544a\u3002<\/p>\n<p>import torch<br \/>\nimport torch.nn.functional as F<br \/>\nfrom sklearn import metrics<br \/>\nimport numpy as np<br \/>\nimport time<br \/>\nfrom tensorboardX import SummaryWriter  # \u5bfc\u5165\u8bad\u7ec3\u53ef\u89c6\u5316\u5de5\u5177<\/p>\n<p># \u521d\u59cb\u5316TensorBoardX\u65e5\u5fd7\u5199\u5165\u5668&#xff08;\u65e5\u5fd7\u4fdd\u5b58\u5728runs\u76ee\u5f55&#xff09;<br \/>\nwriter &#061; SummaryWriter(&#039;runs\/lstm_weibo_sentiment&#039;)<\/p>\n<p>def train(model, train_iter, dev_iter, test_iter, class_list):<br \/>\n    &#034;&#034;&#034;\u6a21\u578b\u8bad\u7ec3&#xff1a;\u542b\u65e9\u505c\u7b56\u7565\u3001\u6a21\u578b\u4fdd\u5b58\u3001\u8bad\u7ec3\u53ef\u89c6\u5316&#034;&#034;&#034;<br \/>\n    model.train()  # \u5207\u6362\u4e3a\u8bad\u7ec3\u6a21\u5f0f&#xff08;\u5f00\u542fdropout&#xff09;<br \/>\n    optimizer &#061; torch.optim.Adam(model.parameters(), lr&#061;0.001)  # Adam\u4f18\u5316\u5668<br \/>\n    total_batch &#061; 0  # \u603b\u6279\u6b21\u8ba1\u6570\u5668<br \/>\n    dev_best_loss &#061; float(&#039;inf&#039;)  # \u9a8c\u8bc1\u96c6\u6700\u4f18\u635f\u5931&#xff08;\u521d\u59cb\u8bbe\u4e3a\u65e0\u7a77\u5927&#xff09;<br \/>\n    last_improve &#061; 0  # \u6700\u540e\u4e00\u6b21\u9a8c\u8bc1\u96c6\u635f\u5931\u63d0\u5347\u7684\u6279\u6b21<br \/>\n    flag &#061; False  # \u662f\u5426\u89e6\u53d1\u65e9\u505c<br \/>\n    epochs &#061; 20  # \u603b\u8bad\u7ec3\u8f6e\u6b21<\/p>\n<p>    for epoch in range(epochs):<br \/>\n        print(f&#034;\\\\nEpoch [{epoch &#043; 1}\/{epochs}]&#034;)<br \/>\n        epoch_start_time &#061; time.time()<br \/>\n        for i, (trains, labels) in enumerate(train_iter):<br \/>\n            # \u524d\u5411\u4f20\u64ad&#xff1a;\u8f93\u5165\u6570\u636e\u2192\u6a21\u578b\u8f93\u51fa\u2192\u8ba1\u7b97\u635f\u5931<br \/>\n            outputs &#061; model(trains)<br \/>\n            loss &#061; F.cross_entropy(outputs, labels)<br \/>\n            # \u53cd\u5411\u4f20\u64ad&#xff1a;\u6e05\u7a7a\u68af\u5ea6\u2192\u8ba1\u7b97\u68af\u5ea6\u2192\u66f4\u65b0\u53c2\u6570<br \/>\n            model.zero_grad()<br \/>\n            loss.backward()<br \/>\n            optimizer.step()<\/p>\n<p>            # \u6bcf100\u6279\u6b21\u76d1\u63a7\u8bad\u7ec3\u96c6\/\u9a8c\u8bc1\u96c6\u6548\u679c&#xff0c;\u5e76\u5199\u5165TensorBoardX<br \/>\n            if total_batch % 100 &#061;&#061; 0:<br \/>\n                # \u8ba1\u7b97\u8bad\u7ec3\u96c6\u51c6\u786e\u7387<br \/>\n                predic &#061; torch.max(outputs.data, 1)[1].cpu()<br \/>\n                train_acc &#061; metrics.accuracy_score(labels.data.cpu(), predic)<br \/>\n                # \u8ba1\u7b97\u9a8c\u8bc1\u96c6\u51c6\u786e\u7387\u548c\u635f\u5931&#xff08;\u5173\u95ed\u68af\u5ea6\u8ba1\u7b97&#xff0c;\u8282\u7701\u5185\u5b58&#xff09;<br \/>\n                dev_acc, dev_loss &#061; evaluate(class_list, model, dev_iter)<\/p>\n<p>                # \u4fdd\u5b58\u9a8c\u8bc1\u96c6\u635f\u5931\u6700\u4f18\u7684\u6a21\u578b<br \/>\n                if dev_loss &lt; dev_best_loss:<br \/>\n                    dev_best_loss &#061; dev_loss<br \/>\n                    torch.save(model.state_dict(), &#039;TextRNN.ckpt&#039;)<br \/>\n                    last_improve &#061; total_batch<\/p>\n<p>                # \u6253\u5370\u76d1\u63a7\u4fe1\u606f<br \/>\n                msg &#061; &#034;Iter: {0:&gt;6},  Train Loss: {1:&gt;5.2},  Train Acc: {2:&gt;6.2%},  Val Loss: {3:&gt;5.2},  Val Acc: {4:&gt;6.2%}&#034;<br \/>\n                print(msg.format(total_batch, loss.item(), train_acc, dev_loss, dev_acc))<\/p>\n<p>                # \u5199\u5165TensorBoardX&#xff08;\u76d1\u63a7loss\u548caccuracy&#xff09;<br \/>\n                writer.add_scalar(&#039;train\/loss&#039;, loss.item(), total_batch)<br \/>\n                writer.add_scalar(&#039;train\/accuracy&#039;, train_acc, total_batch)<br \/>\n                writer.add_scalar(&#039;val\/loss&#039;, dev_loss, total_batch)<br \/>\n                writer.add_scalar(&#039;val\/accuracy&#039;, dev_acc, total_batch)<\/p>\n<p>                model.train()  # \u56de\u5230\u8bad\u7ec3\u6a21\u5f0f&#xff08;\u8bc4\u4f30\u6a21\u5f0f\u540e\u5207\u6362\u56de\u6765&#xff09;<\/p>\n<p>            total_batch &#043;&#061; 1<br \/>\n            # \u65e9\u505c\u7b56\u7565&#xff1a;\u8fde\u7eed10000\u6279\u6b21\u9a8c\u8bc1\u96c6\u635f\u5931\u65e0\u63d0\u5347&#xff0c;\u81ea\u52a8\u505c\u6b62\u8bad\u7ec3<br \/>\n            if total_batch &#8211; last_improve &gt; 10000:<br \/>\n                print(&#034;\u957f\u65f6\u95f4\u65e0\u4f18\u5316&#xff0c;\u81ea\u52a8\u505c\u6b62\u8bad\u7ec3&#8230;&#034;)<br \/>\n                flag &#061; True<br \/>\n                break<br \/>\n        # \u6253\u5370\u672c\u8f6e\u8bad\u7ec3\u8017\u65f6<br \/>\n        print(f&#034;Epoch [{epoch&#043;1}\/{epochs}] \u8017\u65f6&#xff1a;{time.time() &#8211; epoch_start_time:.2f}\u79d2&#034;)<br \/>\n        if flag:<br \/>\n            break<br \/>\n    # \u8bad\u7ec3\u7ed3\u675f\u540e&#xff0c;\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8bc4\u4f30\u6a21\u578b\u6548\u679c<br \/>\n    test(model, test_iter, class_list)<br \/>\n    # \u5173\u95edTensorBoardX\u65e5\u5fd7\u5199\u5165\u5668<br \/>\n    writer.close()<\/p>\n<p>def evaluate(class_list, model, data_iter, test&#061;False):<br \/>\n    &#034;&#034;&#034;\u8bc4\u4f30\u51fd\u6570&#xff1a;\u8ba1\u7b97\u51c6\u786e\u7387\/\u635f\u5931&#xff0c;\u6d4b\u8bd5\u65f6\u8fd4\u56de\u5206\u7c7b\u62a5\u544a&#034;&#034;&#034;<br \/>\n    model.eval()  # \u5207\u6362\u4e3a\u8bc4\u4f30\u6a21\u5f0f&#xff08;\u5173\u95eddropout\u3001\u6279\u91cf\u5f52\u4e00\u5316&#xff09;<br \/>\n    loss_total &#061; 0  # \u603b\u635f\u5931<br \/>\n    predict_all &#061; np.array([], dtype&#061;int)  # \u6240\u6709\u9884\u6d4b\u6807\u7b7e<br \/>\n    labels_all &#061; np.array([], dtype&#061;int)   # \u6240\u6709\u771f\u5b9e\u6807\u7b7e<\/p>\n<p>    with torch.no_grad():  # \u5173\u95ed\u68af\u5ea6\u8ba1\u7b97&#xff0c;\u8282\u7701\u5185\u5b58&#xff0c;\u52a0\u5feb\u8bc4\u4f30\u901f\u5ea6<br \/>\n        for texts, labels in data_iter:<br \/>\n            outputs &#061; model(texts)<br \/>\n            loss &#061; F.cross_entropy(outputs, labels)<br \/>\n            loss_total &#043;&#061; loss<\/p>\n<p>            # \u6536\u96c6\u9884\u6d4b\u7ed3\u679c\u548c\u771f\u5b9e\u6807\u7b7e&#xff08;\u8f6c\u4e3anumpy\u6570\u7ec4&#xff0c;\u4fbf\u4e8e\u8ba1\u7b97\u8bc4\u4f30\u6307\u6807&#xff09;<br \/>\n            labels &#061; labels.data.cpu().numpy()<br \/>\n            predic &#061; torch.max(outputs.data, 1)[1].cpu().numpy()<br \/>\n            labels_all &#061; np.append(labels_all, labels)<br \/>\n            predict_all &#061; np.append(predict_all, predic)<\/p>\n<p>    # \u8ba1\u7b97\u51c6\u786e\u7387&#xff08;\u6240\u6709\u7c7b\u522b\u7684\u6574\u4f53\u51c6\u786e\u7387&#xff09;<br \/>\n    acc &#061; metrics.accuracy_score(labels_all, predict_all)<br \/>\n    if test:<br \/>\n        # \u6d4b\u8bd5\u65f6&#xff0c;\u8fd4\u56de\u51c6\u786e\u7387\u3001\u5e73\u5747\u635f\u5931\u3001\u5206\u7c7b\u62a5\u544a&#xff08;\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u503c&#xff09;<br \/>\n        report &#061; metrics.classification_report(<br \/>\n            labels_all, predict_all,<br \/>\n            target_names&#061;class_list,<br \/>\n            digits&#061;4  # \u4fdd\u75594\u4f4d\u5c0f\u6570&#xff0c;\u63d0\u5347\u7cbe\u5ea6<br \/>\n        )<br \/>\n        return acc, loss_total \/ len(data_iter), report<br \/>\n    # \u9a8c\u8bc1\u65f6&#xff0c;\u4ec5\u8fd4\u56de\u51c6\u786e\u7387\u548c\u5e73\u5747\u635f\u5931<br \/>\n    return acc, loss_total \/ len(data_iter)<\/p>\n<p>def test(model, test_iter, class_list):<br \/>\n    &#034;&#034;&#034;\u6d4b\u8bd5\u96c6\u8bc4\u4f30&#xff1a;\u52a0\u8f7d\u6700\u4f18\u6a21\u578b&#xff0c;\u8f93\u51fa\u6d4b\u8bd5\u7ed3\u679c\u548c\u5206\u7c7b\u62a5\u544a&#034;&#034;&#034;<br \/>\n    # \u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6700\u4f18\u6a21\u578b\u53c2\u6570<br \/>\n    model.load_state_dict(torch.load(&#039;TextRNN.ckpt&#039;))<br \/>\n    model.eval()  # \u5207\u6362\u4e3a\u8bc4\u4f30\u6a21\u5f0f<br \/>\n    start_time &#061; time.time()<br \/>\n    # \u8ba1\u7b97\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387\u3001\u5e73\u5747\u635f\u5931\u3001\u5206\u7c7b\u62a5\u544a<br \/>\n    test_acc, test_loss, test_report &#061; evaluate(class_list, model, test_iter, test&#061;True)<br \/>\n    # \u6253\u5370\u6d4b\u8bd5\u7ed3\u679c<br \/>\n    print(&#034;\\\\n&#034; &#043; &#034;&#061;&#034;*50)<br \/>\n    print(&#034;\u6d4b\u8bd5\u96c6\u8bc4\u4f30\u7ed3\u679c&#xff1a;&#034;)<br \/>\n    msg &#061; &#039;Test Loss: {0:&gt;5.2},  Test Acc: {1:&gt;6.2%}&#039;<br \/>\n    print(msg.format(test_loss, test_acc))<br \/>\n    print(&#034;\\\\n\u5206\u7c7b\u62a5\u544a&#xff08;\u7cbe\u786e\u7387\/\u53ec\u56de\u7387\/F1\u503c&#xff09;&#xff1a;&#034;)<br \/>\n    print(test_report)<br \/>\n    print(f&#034;\u6d4b\u8bd5\u8017\u65f6&#xff1a;{time.time() &#8211; start_time:.2f}\u79d2&#034;)<br \/>\n    print(&#034;&#061;&#034;*50) <\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%975%EF%BC%9A%E4%B8%BB%E7%A8%8B%E5%BA%8F%EF%BC%88main.py%EF%BC%89\">\u6a21\u57575&#xff1a;\u4e3b\u7a0b\u5e8f&#xff08;main.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u6574\u5408\u6240\u6709\u6a21\u5757&#xff0c;\u52a0\u8f7d\u6570\u636e\u3001\u521d\u59cb\u5316\u6a21\u578b\u3001\u542f\u52a8\u8bad\u7ec3&#xff0c;\u662f\u6574\u4e2a\u5b9e\u6218\u7684\u5165\u53e3\u6587\u4ef6&#xff0c;\u65b0\u624b\u53ea\u9700\u4fee\u6539\u5c11\u91cf\u53c2\u6570\u5373\u53ef\u8fd0\u884c\u3002<\/p>\n<p>import torch<br \/>\nimport numpy as np<br \/>\nimport load_dataset  # \u5bfc\u5165\u6570\u636e\u52a0\u8f7d\u6a21\u5757<br \/>\nimport TextRNN       # \u5bfc\u5165\u6a21\u578b\u6a21\u5757<br \/>\nfrom train_eval_test import train  # \u5bfc\u5165\u8bad\u7ec3\u51fd\u6570<\/p>\n<p># \u56fa\u5b9a\u968f\u673a\u79cd\u5b50&#xff08;\u4fdd\u8bc1\u6a21\u578b\u8bad\u7ec3\u7ed3\u679c\u53ef\u590d\u73b0&#xff09;<br \/>\nnp.random.seed(1)<br \/>\ntorch.manual_seed(1)<br \/>\ntorch.cuda.manual_seed_all(1)<br \/>\ntorch.backends.cudnn.deterministic &#061; True<\/p>\n<p># \u8bbe\u5907\u9009\u62e9&#xff1a;\u4f18\u5148\u4f7f\u7528GPU&#xff08;\u8bad\u7ec3\u901f\u5ea6\u66f4\u5feb&#xff09;&#xff0c;\u65e0GPU\u5219\u4f7f\u7528CPU<br \/>\ndevice &#061; &#034;cuda&#034; if torch.cuda.is_available() else &#034;cpu&#034;<br \/>\nprint(f&#034;\u5f53\u524d\u4f7f\u7528\u8bbe\u5907&#xff1a;{device}&#034;)<\/p>\n<p># 1. \u52a0\u8f7d\u5e76\u5904\u7406\u6570\u636e&#xff08;\u6570\u636e\u96c6\u8def\u5f84\u8bf7\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539&#xff09;<br \/>\nvocab, train_data, dev_data, test_data &#061; load_dataset.load_dataset(&#034;simplifyweibo_4_moods.csv&#034;)<br \/>\n# \u6784\u5efa\u6570\u636e\u8fed\u4ee3\u5668&#xff08;batch_size&#061;128&#xff0c;\u53ef\u6839\u636eGPU\u663e\u5b58\u8c03\u6574&#xff09;<br \/>\ntrain_iter &#061; load_dataset.DatasetIterater(train_data, batch_size&#061;128, device&#061;device)<br \/>\ndev_iter &#061; load_dataset.DatasetIterater(dev_data, batch_size&#061;128, device&#061;device)<br \/>\ntest_iter &#061; load_dataset.DatasetIterater(test_data, batch_size&#061;128, device&#061;device)<\/p>\n<p># 2. \u52a0\u8f7d\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf&#xff08;\u817e\u8baf\u8bcd\u5411\u91cf&#xff0c;200\u7ef4&#xff09;<br \/>\n# \u82e5\u6ca1\u6709\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf&#xff0c;\u8bbe\u4e3aembedding_pretrained &#061; None&#xff08;\u968f\u673a\u521d\u59cb\u5316&#xff09;<br \/>\ntry:<br \/>\n    embedding_pretrained &#061; torch.tensor(np.load(&#034;embedding_Tencent.npz&#034;)[&#034;embeddings&#034;].astype(&#039;float32&#039;))<br \/>\nexcept FileNotFoundError:<br \/>\n    raise Exception(&#034;\u672a\u627e\u5230\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u6587\u4ef6embedding_Tencent.npz&#xff0c;\u8bf7\u786e\u8ba4\u6587\u4ef6\u8def\u5f84\u6b63\u786e&#xff01;&#034;)<\/p>\n<p># 3. \u6a21\u578b\u53c2\u6570\u914d\u7f6e&#xff08;\u4e0e\u8bad\u7ec3\u65f6\u4fdd\u6301\u4e00\u81f4&#xff09;<br \/>\nembed &#061; embedding_pretrained.size(1)  # \u8bcd\u5411\u91cf\u7ef4\u5ea6&#xff08;200\u7ef4&#xff09;<br \/>\nclass_list &#061; [&#034;\u559c\u60a6&#034;, &#034;\u6124\u6012&#034;, &#034;\u538c\u6076&#034;, &#034;\u4f4e\u843d&#034;]  # \u60c5\u611f\u7c7b\u522b\u5217\u8868&#xff08;\u4e0e\u6807\u7b7e0-3\u4e00\u4e00\u5bf9\u5e94&#xff09;<br \/>\nnum_classes &#061; len(class_list)  # \u7c7b\u522b\u6570&#xff08;4&#xff09;<br \/>\nn_vocab &#061; len(vocab)  # \u8bcd\u8868\u5927\u5c0f&#xff08;4762&#xff09;<\/p>\n<p># 4. \u521d\u59cb\u5316\u6a21\u578b&#xff0c;\u5e76\u52a0\u8f7d\u5230\u6307\u5b9a\u8bbe\u5907&#xff08;CPU\/GPU&#xff09;<br \/>\nmodel &#061; TextRNN.Model(<br \/>\n    embedding_pretrained,<br \/>\n    n_vocab,<br \/>\n    embed,<br \/>\n    num_classes<br \/>\n).to(device)<\/p>\n<p># 5. \u542f\u52a8\u6a21\u578b\u8bad\u7ec3&#xff08;\u8bad\u7ec3\u5b8c\u6210\u540e\u81ea\u52a8\u8fdb\u884c\u6d4b\u8bd5&#xff09;<br \/>\nprint(&#034;\\\\n&#034; &#043; &#034;&#061;&#034;*50)<br \/>\nprint(&#034;\u5f00\u59cb\u6a21\u578b\u8bad\u7ec3&#8230;&#034;)<br \/>\ntrain(model, train_iter, dev_iter, test_iter, class_list) <\/p>\n<h3 id=\"%E6%A8%A1%E5%9D%976%EF%BC%9A%E5%8D%95%E4%B8%AA%E5%8F%A5%E5%AD%90%E9%A2%84%E6%B5%8B%EF%BC%88predict.py%EF%BC%89\">\u6a21\u57576&#xff1a;\u5355\u4e2a\u53e5\u5b50\u9884\u6d4b&#xff08;predict.py&#xff09;<\/h3>\n<p>\u6838\u5fc3\u529f\u80fd&#xff1a;\u8bad\u7ec3\u5b8c\u6210\u540e&#xff0c;\u9884\u6d4b\u5355\u4e2a\u5fae\u535a\u8bc4\u8bba\u7684\u60c5\u611f&#xff0c;\u9002\u914d\u5b9e\u6218\u573a\u666f&#xff0c;\u8f93\u5165\u53e5\u5b50\u5373\u53ef\u5f97\u5230\u9884\u6d4b\u7ed3\u679c\u3002<\/p>\n<p>import torch<br \/>\nimport pickle as pkl<\/p>\n<p># \u5168\u5c40\u914d\u7f6e&#xff08;\u4e0e\u8bad\u7ec3\u65f6\u4fdd\u6301\u4e00\u81f4&#xff09;<br \/>\nunk, pad, PAD_SIZE &#061; &#039;&lt;unk&gt;&#039;, &#039;&lt;pad&gt;&#039;, 70<\/p>\n<p>def predict(model, sentence, class_list):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u9884\u6d4b\u5355\u4e2a\u53e5\u5b50\u7684\u60c5\u611f\u7c7b\u522b<br \/>\n    :param model: \u8bad\u7ec3\u597d\u7684LSTM\u6a21\u578b<br \/>\n    :param sentence: \u5f85\u9884\u6d4b\u7684\u5fae\u535a\u8bc4\u8bba&#xff08;\u5b57\u7b26\u4e32&#xff09;<br \/>\n    :param class_list: \u60c5\u611f\u7c7b\u522b\u5217\u8868<br \/>\n    :return: \u9884\u6d4b\u7ed3\u679c\u5b57\u5178&#xff08;\u542b\u8f93\u5165\u53e5\u5b50\u3001\u9884\u6d4b\u7c7b\u522b\u3001\u5404\u7c7b\u522b\u6982\u7387&#xff09;<br \/>\n    &#034;&#034;&#034;<br \/>\n    model.eval()  # \u5207\u6362\u4e3a\u8bc4\u4f30\u6a21\u5f0f<br \/>\n    # \u52a0\u8f7d\u8bcd\u8868<br \/>\n    try:<br \/>\n        vocab &#061; pkl.load(open(&#039;vocab.pkl&#039;, &#039;rb&#039;))<br \/>\n    except FileNotFoundError:<br \/>\n        raise Exception(&#034;\u672a\u627e\u5230vocab.pkl\u6587\u4ef6&#xff0c;\u8bf7\u786e\u4fdd\u8be5\u6587\u4ef6\u5728\u5f53\u524d\u76ee\u5f55\u4e0b&#xff01;&#034;)<\/p>\n<p>    # \u6570\u636e\u9884\u5904\u7406&#xff08;\u4e0e\u8bad\u7ec3\u65f6\u5b8c\u5168\u4e00\u81f4&#xff09;<br \/>\n    token &#061; [y for y in sentence][:PAD_SIZE] if len(sentence)&gt;&#061;PAD_SIZE else [y for y in sentence] &#043; [pad]*(PAD_SIZE-len(sentence))<br \/>\n    idx &#061; [vocab.get(w, vocab[unk]) for w in token]<\/p>\n<p>    # \u6784\u5efa\u6a21\u578b\u8f93\u5165&#xff08;\u9002\u914d\u6a21\u578b\u8f93\u5165\u683c\u5f0f&#xff09;<br \/>\n    x &#061; torch.LongTensor([idx]).to(next(model.parameters()).device)<br \/>\n    with torch.no_grad():<br \/>\n        out &#061; model((x, None))<br \/>\n        pred_label &#061; class_list[out.argmax(1).item()]<br \/>\n        probs &#061; torch.softmax(out, 1).cpu().numpy()[0].round(4)<\/p>\n<p>    # \u8fd4\u56de\u9884\u6d4b\u7ed3\u679c<br \/>\n    return {<br \/>\n        &#034;\u8f93\u5165\u53e5\u5b50&#034;: sentence,<br \/>\n        &#034;\u9884\u6d4b\u60c5\u611f\u7c7b\u522b&#034;: pred_label,<br \/>\n        &#034;\u5404\u7c7b\u522b\u6982\u7387&#034;: dict(zip(class_list, probs))<br \/>\n    }<\/p>\n<p># \u8c03\u7528\u793a\u4f8b&#xff08;\u9700\u5148\u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b&#xff09;<br \/>\nif __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    # \u521d\u59cb\u5316\u6a21\u578b&#xff08;\u53c2\u6570\u4e0e\u8bad\u7ec3\u65f6\u4e00\u81f4&#xff09;<br \/>\n    embedding_pretrained &#061; torch.tensor(np.load(&#034;embedding_Tencent.npz&#034;)[&#034;embeddings&#034;].astype(&#039;float32&#039;))<br \/>\n    vocab &#061; pkl.load(open(&#039;vocab.pkl&#039;, &#039;rb&#039;))<br \/>\n    model &#061; TextRNN.Model(embedding_pretrained, len(vocab), 200, 4)<br \/>\n    # \u52a0\u8f7d\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u53c2\u6570<br \/>\n    model.load_state_dict(torch.load(&#039;TextRNN.ckpt&#039;, map_location&#061;torch.device(&#039;cpu&#039;)))<br \/>\n    # \u7c7b\u522b\u5217\u8868<br \/>\n    class_list &#061; [&#034;\u559c\u60a6&#034;, &#034;\u6124\u6012&#034;, &#034;\u538c\u6076&#034;, &#034;\u4f4e\u843d&#034;]<\/p>\n<p>    # \u6301\u7eed\u9884\u6d4b&#xff08;\u8f93\u5165q\u7ed3\u675f&#xff09;<br \/>\n    print(&#034;&#061;&#061;&#061;&#061;&#061; \u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u9884\u6d4b&#xff08;\u8f93\u5165q\u7ed3\u675f&#xff09;&#061;&#061;&#061;&#061;&#061;&#034;)<br \/>\n    while True:<br \/>\n        s &#061; input(&#034;\u8bf7\u8f93\u5165\u5f85\u9884\u6d4b\u7684\u5fae\u535a\u8bc4\u8bba&#xff1a;&#034;).strip()<br \/>\n        if s.lower() &#061;&#061; &#039;q&#039;:<br \/>\n            print(&#034;\u9884\u6d4b\u7ed3\u675f&#xff01;&#034;)<br \/>\n            break<br \/>\n        if not s:<br \/>\n            print(&#034;\u8f93\u5165\u4e0d\u80fd\u4e3a\u7a7a&#xff0c;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;)<br \/>\n            continue<br \/>\n        # \u6267\u884c\u9884\u6d4b\u5e76\u6253\u5370\u7ed3\u679c<br \/>\n        result &#061; predict(model, s, class_list)<br \/>\n        print(f&#034;\\\\n\u9884\u6d4b\u7ed3\u679c&#xff1a;{result[&#039;\u9884\u6d4b\u60c5\u611f\u7c7b\u522b&#039;]}&#034;)<br \/>\n        print(&#034;\u5404\u7c7b\u522b\u6982\u7387&#xff1a;&#034;, result[&#039;\u5404\u7c7b\u522b\u6982\u7387&#039;])<br \/>\n        print(&#034;-&#034;*50) <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"403\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163253-6984c6353e06a.png\" width=\"1625\" \/><\/p>\n<h2 id=\"%E4%BA%94%E3%80%81%E5%85%B3%E9%94%AE%E4%BF%AE%E6%AD%A3%E4%B8%8E%E8%A1%A5%E5%85%85%E8%AF%B4%E6%98%8E\">\u4e94\u3001\u5173\u952e\u4fee\u6b63\u4e0e\u8865\u5145\u8bf4\u660e<\/h2>\n<h3 id=\"1.%20%E5%8E%9F%E5%A7%8B%E6%B5%81%E7%A8%8B%E6%A0%B8%E5%BF%83%E4%BF%AE%E6%AD%A3\">1. \u539f\u59cb\u6d41\u7a0b\u6838\u5fc3\u4fee\u6b63<\/h3>\n<ul>\n<li>\n<p>\u4fee\u6b63\u300c4762\u7684\u72ec\u70ed\u7f16\u7801\u300d\u8868\u8ff0&#xff1a;\u5b9e\u9645\u662f\u300c\u5b57\u7b26\u7d22\u5f15\u2192\u8bcd\u5411\u91cf\u300d\u7684\u8f6c\u5316&#xff0c;\u8bcd\u8868\u5927\u5c0f\u4e3a4762&#xff08;4760\u4e2a\u9ad8\u9891\u5b57\u7b26&#043;unk&#043;pad&#xff09;&#xff0c;\u72ec\u70ed\u7f16\u7801\u4f1a\u5bfc\u81f4\u7ef4\u5ea6\u7206\u70b8&#xff08;4762\u7ef4&#xff09;&#xff0c;\u4e0d\u9002\u7528\u4e8e\u8be5\u4efb\u52a1&#xff0c;Embedding\u5c42\u53ef\u5c06\u5176\u538b\u7f29\u4e3a200\u7ef4\u8bcd\u5411\u91cf&#xff0c;\u66f4\u9002\u5408\u6a21\u578b\u8bad\u7ec3\u3002<\/p>\n<\/li>\n<li>\n<p>\u8865\u5145\u300c\u65e9\u505c\u7b56\u7565\u300d\u300c\u8bad\u7ec3\u53ef\u89c6\u5316\u300d&#xff1a;\u539f\u59cb\u6d41\u7a0b\u672a\u63d0\u53ca\u8fc7\u62df\u5408\u89e3\u51b3\u65b9\u6848\u548c\u8bad\u7ec3\u76d1\u63a7\u65b9\u6cd5&#xff0c;\u8865\u5145\u540e\u53ef\u907f\u514d\u6a21\u578b\u8fc7\u5ea6\u8bad\u7ec3&#xff0c;\u540c\u65f6\u76f4\u89c2\u76d1\u63a7\u8bad\u7ec3\u8fc7\u7a0b&#xff0c;\u89e3\u51b3\u300c\u76f2\u8bad\u300d\u95ee\u9898\u3002<\/p>\n<\/li>\n<li>\n<p>\u660e\u786e\u300c\u6253\u5305\u300d\u542b\u4e49&#xff1a;\u539f\u59cb\u6d41\u7a0b\u4e2d\u300c\u6253\u5305\u300d\u8868\u8ff0\u6a21\u7cca&#xff0c;\u4fee\u6b63\u4e3a\u300c\u6784\u5efa\u6570\u636e\u8fed\u4ee3\u5668&#xff0c;\u5c06\u5904\u7406\u597d\u7684\u6570\u636e\u8f6c\u4e3a\u5f20\u91cf\u5e76\u6309\u6279\u6b21\u52a0\u8f7d\u300d&#xff0c;\u9002\u914d\u6a21\u578b\u7684\u6279\u91cf\u8bad\u7ec3\u9700\u6c42&#xff0c;\u63d0\u5347\u8bad\u7ec3\u6548\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>\u8865\u5145\u300c\u5355\u4e2a\u53e5\u5b50\u9884\u6d4b\u529f\u80fd\u300d&#xff1a;\u539f\u59cb\u6d41\u7a0b\u4ec5\u5305\u542b\u8bad\u7ec3\u6d4b\u8bd5&#xff0c;\u8865\u5145\u9884\u6d4b\u51fd\u6570&#xff0c;\u5b9e\u73b0\u5b9e\u6218\u843d\u5730&#xff0c;\u8f93\u5165\u4efb\u610f\u5fae\u535a\u8bc4\u8bba\u5373\u53ef\u5f97\u5230\u60c5\u611f\u9884\u6d4b\u7ed3\u679c\u3002<\/p>\n<\/li>\n<\/ul>\n<h3 id=\"\"><\/h3>\n<h2 id=\"%E5%85%AD%E3%80%81%E6%80%BB%E7%BB%93%E4%B8%8E%E8%BF%9B%E9%98%B6%E6%96%B9%E5%90%91\">\u516d\u3001\u603b\u7ed3\u4e0e\u8fdb\u9636\u65b9\u5411<\/h2>\n<h3 id=\"1.%20%E5%AE%9E%E6%88%98%E6%80%BB%E7%BB%93\">1. \u5b9e\u6218\u603b\u7ed3<\/h3>\n<p>\u672c\u6b21\u5b9e\u6218\u57fa\u4e8eLSTM\u7f51\u7edc\u5b8c\u6210\u4e86\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b\u4efb\u52a1&#xff0c;\u8986\u76d6\u4e86\u6587\u672c\u5206\u7c7b\u7684\u5b8c\u6574\u6d41\u7a0b&#xff0c;\u6838\u5fc3\u8981\u70b9\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6570\u636e\u5904\u7406\u662f\u57fa\u7840&#xff1a;\u5b57\u7b26\u7ea7\u5904\u7406\u9002\u914d\u4e2d\u6587\u5fae\u535a\u573a\u666f&#xff0c;\u7edf\u4e00\u53e5\u5b50\u957f\u5ea6\u3001\u6784\u5efa\u8bcd\u8868\u5e76\u8fc7\u6ee4\u4f4e\u9891\u8bcd&#xff0c;\u80fd\u6709\u6548\u51cf\u5c11\u6a21\u578b\u5197\u4f59&#xff0c;\u63d0\u5347\u8bad\u7ec3\u6548\u7387\u548c\u6a21\u578b\u6548\u679c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6a21\u578b\u8bbe\u8ba1\u8981\u8d34\u5408\u4efb\u52a1&#xff1a;\u53cc\u5411LSTM\u80fd\u6355\u6349\u6587\u672c\u4e0a\u4e0b\u6587\u53cc\u5411\u7279\u5f81&#xff0c;\u6bd4\u5355\u5411LSTM\u66f4\u9002\u5408\u60c5\u611f\u5206\u7c7b&#xff1b;\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf\u7684\u4f7f\u7528\u7684\u80fd\u6709\u6548\u63d0\u5347\u6a21\u578b\u7684\u8bed\u4e49\u7406\u89e3\u80fd\u529b&#xff0c;\u5f00\u542f\u5fae\u8c03\u540e\u80fd\u66f4\u597d\u9002\u914d\u5f53\u524d\u4efb\u52a1&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8bad\u7ec3\u7b56\u7565\u662f\u5173\u952e&#xff1a;\u65e9\u505c\u7b56\u7565\u907f\u514d\u8fc7\u62df\u5408&#xff0c;Adam\u4f18\u5316\u5668\u52a0\u5feb\u6536\u655b&#xff0c;\u8bad\u7ec3\u53ef\u89c6\u5316\u80fd\u76f4\u89c2\u76d1\u63a7\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b&#xff0c;\u8fd9\u4e9b\u6280\u5de7\u5728\u5b9e\u6218\u4e2d\u975e\u5e38\u5b9e\u7528&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5757\u5316\u4ee3\u7801\u66f4\u6613\u7ef4\u62a4&#xff1a;\u5c06\u8bcd\u8868\u6784\u5efa\u3001\u6570\u636e\u52a0\u8f7d\u3001\u6a21\u578b\u5b9a\u4e49\u3001\u8bad\u7ec3\u6d4b\u8bd5\u62c6\u5206\u5230\u4e0d\u540c\u6a21\u5757&#xff0c;\u7ed3\u6784\u6e05\u6670&#xff0c;\u4fbf\u4e8e\u540e\u7eed\u4fee\u6539\u548c\u590d\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u4e00\u3001\u4efb\u52a1\u660e\u786e<br \/>\n1. \u4efb\u52a1\u76ee\u6807<br \/>\n2. \u6838\u5fc3\u8981\u6c42<br \/>\n\u4e8c\u3001\u6838\u5fc3\u601d\u8def<br \/>\n\u4e09\u3001\u6d41\u7a0b\u8bbe\u8ba1<br \/>\n\u9636\u6bb51&#xff1a;\u539f\u59cb\u6570\u636e\u5904\u7406&#xff08;\u6838\u5fc3&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15\u2192\u6279\u6b21\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165&#xff09;<br \/>\n\u9636\u6bb52&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;\u6838\u5fc3&#xff1a;\u8bcd\u5411\u91cf\u2192\u65f6\u5e8f\u7279\u5f81\u2192\u60c5\u611f\u5206\u7c7b&#xff09;<br \/>\n\u9636\u6bb53&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;\u6838\u5fc3&#xff1a;\u4f18\u5316\u6a21\u578b\u53c2\u6570&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff09;<br \/>\n\u56db\u3001<\/p>\n","protected":false},"author":2,"featured_media":72571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2951,50,2395],"topic":[],"class_list":["post-72574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-lstm","tag-50","tag-2395"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \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\/72574.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u4e00\u3001\u4efb\u52a1\u660e\u786e 1. \u4efb\u52a1\u76ee\u6807 2. \u6838\u5fc3\u8981\u6c42 \u4e8c\u3001\u6838\u5fc3\u601d\u8def \u4e09\u3001\u6d41\u7a0b\u8bbe\u8ba1 \u9636\u6bb51&#xff1a;\u539f\u59cb\u6570\u636e\u5904\u7406&#xff08;\u6838\u5fc3&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15\u2192\u6279\u6b21\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165&#xff09; \u9636\u6bb52&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;\u6838\u5fc3&#xff1a;\u8bcd\u5411\u91cf\u2192\u65f6\u5e8f\u7279\u5f81\u2192\u60c5\u611f\u5206\u7c7b&#xff09; \u9636\u6bb53&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;\u6838\u5fc3&#xff1a;\u4f18\u5316\u6a21\u578b\u53c2\u6570&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff09; \u56db\u3001\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/72574.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-05T16:32:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163252-6984c63423a42.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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72574.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/72574.html\",\"name\":\"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-05T16:32:54+00:00\",\"dateModified\":\"2026-02-05T16:32:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/72574.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/72574.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72574.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b\"}]},{\"@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":"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \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\/72574.html","og_locale":"zh_CN","og_type":"article","og_title":"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u4e00\u3001\u4efb\u52a1\u660e\u786e 1. \u4efb\u52a1\u76ee\u6807 2. \u6838\u5fc3\u8981\u6c42 \u4e8c\u3001\u6838\u5fc3\u601d\u8def \u4e09\u3001\u6d41\u7a0b\u8bbe\u8ba1 \u9636\u6bb51&#xff1a;\u539f\u59cb\u6570\u636e\u5904\u7406&#xff08;\u6838\u5fc3&#xff1a;\u5b57\u7b26\u2192\u7d22\u5f15\u2192\u6279\u6b21\u5f20\u91cf&#xff0c;\u9002\u914d\u6a21\u578b\u8f93\u5165&#xff09; \u9636\u6bb52&#xff1a;LSTM\u6a21\u578b\u6784\u5efa&#xff08;\u6838\u5fc3&#xff1a;\u8bcd\u5411\u91cf\u2192\u65f6\u5e8f\u7279\u5f81\u2192\u60c5\u611f\u5206\u7c7b&#xff09; \u9636\u6bb53&#xff1a;\u8bad\u7ec3\u4e0e\u6d4b\u8bd5&#xff08;\u6838\u5fc3&#xff1a;\u4f18\u5316\u6a21\u578b\u53c2\u6570&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6548\u679c&#xff09; \u56db\u3001","og_url":"https:\/\/www.wsisp.com\/helps\/72574.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-05T16:32:54+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163252-6984c63423a42.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/72574.html","url":"https:\/\/www.wsisp.com\/helps\/72574.html","name":"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-05T16:32:54+00:00","dateModified":"2026-02-05T16:32:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/72574.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/72574.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/72574.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LSTM\u7f51\u7edc\u5b9e\u6218\uff1a\u5b9e\u73b0\u5fae\u535a\u8bc4\u8bba\u60c5\u611f\u5206\u7c7b"}]},{"@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\/72574","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=72574"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/72574\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/72571"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=72574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=72574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=72574"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=72574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}