{"id":191,"date":"2025-07-10T14:51:23","date_gmt":"2025-07-10T06:51:23","guid":{"rendered":"https:\/\/mianka.xyz\/?p=191"},"modified":"2026-04-20T08:45:47","modified_gmt":"2026-04-20T00:45:47","slug":"pytorch-2","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=191","title":{"rendered":"\u57fa\u4e8ePyTorch\u7684\u6c49\u5b57\u60c5\u611f\u8868\u8fbe\u5206\u6790\uff1a\u6a21\u578b\u6784\u5efa\u4e0e\u8bad\u7ec3\u5b9e\u8df5"},"content":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e00\u3001\u6280\u672f\u80cc\u666f\u4e0e\u4efb\u52a1\u5b9a\u4e49<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u60c5\u611f\u5206\u6790\u4f5c\u4e3a\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u7684\u6838\u5fc3\u4efb\u52a1\uff0c\u65e8\u5728\u901a\u8fc7\u7b97\u6cd5\u8bc6\u522b\u6587\u672c\u4e2d\u7684\u60c5\u611f\u503e\u5411\uff08\u79ef\u6781\/\u6d88\u6781\/\u4e2d\u6027\uff09\u3002\u5728\u4e2d\u6587\u573a\u666f\u4e0b\uff0c\u6c49\u5b57\u7279\u6709\u7684\u8868\u610f\u7279\u5f81\u548c\u590d\u6742\u8bed\u6cd5\u7ed3\u6784\u5bf9\u6a21\u578b\u8bbe\u8ba1\u63d0\u51fa\u66f4\u9ad8\u8981\u6c42\u3002PyTorch\u51ed\u501f\u52a8\u6001\u8ba1\u7b97\u56fe\u548c\u4e30\u5bcc\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u5e93\uff0c\u6210\u4e3a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u4e3b\u6d41\u5f00\u53d1\u6846\u67b6\u3002<\/p>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5178\u578b\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u793e\u4ea4\u5a92\u4f53\u8206\u60c5\u76d1\u63a7\uff08\u5982\u5fae\u535a\u3001\u6296\u97f3\u8bc4\u8bba\u5206\u6790\uff09<\/p>\n<\/li>\n<li>\n<p>\u7535\u5546\u5e73\u53f0\u4ea7\u54c1\u8bc4\u4ef7\u5206\u7c7b\uff08\u5982\u6dd8\u5b9d\u3001\u4eac\u4e1c\u8bc4\u8bba\u6781\u6027\u5224\u65ad\uff09<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u670d\u52a1\u53cd\u9988\u5904\u7406\uff08\u5982\u94f6\u884c\u5de5\u5355\u60c5\u611f\u5206\u7ea7\uff09<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e8c\u3001\u6570\u636e\u51c6\u5907\u4e0e\u9884\u5904\u7406<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.1 \u6570\u636e\u96c6\u9009\u62e9<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u63a8\u8350\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f00\u6570\u636e\u96c6\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">ChnSentiCorp<\/span>\uff1a\u5305\u542b\u9152\u5e97\u3001\u7535\u8111\u3001\u4e66\u7c4d\u9886\u57df\u8bc4\u8bba\uff0c\u6807\u6ce8\u79ef\u6781\/\u6d88\u6781\u6807\u7b7e<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">NLPCC\u60c5\u611f\u5206\u6790\u6570\u636e\u96c6<\/span>\uff1a\u8986\u76d6\u5fae\u535a\u3001\u65b0\u95fb\u7b49\u573a\u666f\uff0c\u6807\u6ce8\u66f4\u7ec6\u7c92\u5ea6\u60c5\u611f<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">THUCNews\u60c5\u611f\u5206\u7c7b\u6570\u636e\u96c6<\/span>\uff1a\u65b0\u95fb\u6587\u672c\u60c5\u611f\u6807\u6ce8\uff0c\u9002\u5408\u957f\u6587\u672c\u5206\u6790<\/p>\n<\/li>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.2 \u5173\u952e\u9884\u5904\u7406\u6b65\u9aa4<\/h3>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;jieba\nfrom&nbsp;sklearn.model_selection&nbsp;import&nbsp;train_test_split\n\n#&nbsp;\u793a\u4f8b\u6570\u636e\u9884\u5904\u7406\u6d41\u7a0b\ndef&nbsp;preprocess_data(raw_data):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;1.&nbsp;\u6807\u7b7e\u6620\u5c04\n&nbsp;&nbsp;&nbsp;&nbsp;label_map&nbsp;=&nbsp;{&#39;positive&#39;:&nbsp;1,&nbsp;&#39;negative&#39;:&nbsp;0}\n&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;[(text,&nbsp;label_map[label])&nbsp;for&nbsp;text,&nbsp;label&nbsp;in&nbsp;raw_data]\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;2.&nbsp;\u4e2d\u6587\u5206\u8bcd\uff08\u4f7f\u7528jieba\u5e93\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;data&nbsp;=&nbsp;[(jieba.lcut(text),&nbsp;label)&nbsp;for&nbsp;text,&nbsp;label&nbsp;in&nbsp;data]\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;3.&nbsp;\u6784\u5efa\u8bcd\u6c47\u8868\uff08\u9700\u7edf\u8ba1\u8bcd\u9891\u5e76\u8fc7\u6ee4\u4f4e\u9891\u8bcd\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;vocab&nbsp;=&nbsp;set()\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;text,&nbsp;_&nbsp;in&nbsp;data:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vocab.update(text)\n&nbsp;&nbsp;&nbsp;&nbsp;word_to_idx&nbsp;=&nbsp;{word:&nbsp;idx+1&nbsp;for&nbsp;idx,&nbsp;word&nbsp;in&nbsp;enumerate(vocab)}&nbsp;&nbsp;#&nbsp;0\u4fdd\u7559\u4e3apadding\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;4.&nbsp;\u5e8f\u5217\u586b\u5145\u4e0e\u5206\u5272\n&nbsp;&nbsp;&nbsp;&nbsp;max_len&nbsp;=&nbsp;128&nbsp;&nbsp;#&nbsp;\u6839\u636e\u6570\u636e\u7edf\u8ba1\u8bbe\u5b9a\n&nbsp;&nbsp;&nbsp;&nbsp;X&nbsp;=&nbsp;[]\n&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;=&nbsp;[]\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;text,&nbsp;label&nbsp;in&nbsp;data:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text_idx&nbsp;=&nbsp;[word_to_idx[word]&nbsp;for&nbsp;word&nbsp;in&nbsp;text&nbsp;if&nbsp;word&nbsp;in&nbsp;word_to_idx]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padded_text&nbsp;=&nbsp;text_idx[:max_len]&nbsp;+&nbsp;[0]*(max_len&nbsp;-&nbsp;len(text_idx[:max_len]))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X.append(padded_text)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y.append(label)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;train_test_split(X,&nbsp;y,&nbsp;test_size=0.2,&nbsp;random_state=42)<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u5173\u952e\u53c2\u6570\u63a7\u5236<\/span>\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u6700\u5927\u5e8f\u5217\u957f\u5ea6\uff08max_len\uff09\uff1a\u5efa\u8bae\u901a\u8fc7\u6570\u636e\u7edf\u8ba1\u786e\u5b9a95%\u5206\u4f4d\u6570<\/p>\n<\/li>\n<li>\n<p>\u8bcd\u6c47\u8868\u5927\u5c0f\uff1a\u901a\u5e38\u9650\u5236\u57282\u4e07-5\u4e07\u8bcd<\/p>\n<\/li>\n<li>\n<p>\u505c\u7528\u8bcd\u8fc7\u6ee4\uff1a\u9700\u79fb\u9664&quot;\u7684&quot;\u3001&quot;\u4e86&quot;\u7b49\u9ad8\u9891\u65e0\u610f\u4e49\u8bcd<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e09\u3001\u6a21\u578b\u67b6\u6784\u8bbe\u8ba1<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">3.1 LSTM\u57fa\u7840\u6a21\u578b<\/h3>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torch\nimport&nbsp;torch.nn&nbsp;as&nbsp;nn\n\nclass&nbsp;LSTMModel(nn.Module):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;vocab_size,&nbsp;embedding_dim=128,&nbsp;hidden_dim=256,&nbsp;output_dim=2):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super().__init__()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.embedding&nbsp;=&nbsp;nn.Embedding(vocab_size,&nbsp;embedding_dim,&nbsp;padding_idx=0)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.lstm&nbsp;=&nbsp;nn.LSTM(embedding_dim,&nbsp;hidden_dim,&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bidirectional=True,&nbsp;batch_first=True)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.fc&nbsp;=&nbsp;nn.Linear(hidden_dim*2,&nbsp;output_dim)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.dropout&nbsp;=&nbsp;nn.Dropout(0.5)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;forward(self,&nbsp;x):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;x&nbsp;shape:&nbsp;[batch_size,&nbsp;seq_len]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;embedded&nbsp;=&nbsp;self.dropout(self.embedding(x))&nbsp;&nbsp;#&nbsp;[batch,&nbsp;seq,&nbsp;emb_dim]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output,&nbsp;(hidden,&nbsp;_)&nbsp;=&nbsp;self.lstm(embedded)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u53cc\u5411LSTM\u62fc\u63a5\u6700\u540e\u65f6\u523b\u7684\u9690\u85cf\u72b6\u6001\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hidden&nbsp;=&nbsp;torch.cat([hidden[-2],&nbsp;hidden[-1]],&nbsp;dim=1)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self.fc(self.dropout(hidden))<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u4f18\u5316\u6280\u5de7<\/span>\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u4f7f\u7528\u53cc\u5411LSTM\u6355\u6349\u4e0a\u4e0b\u6587\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>\u6dfb\u52a0Dropout\u5c42\u9632\u6b62\u8fc7\u62df\u5408\uff08\u5efa\u8bae0.3-0.5\uff09<\/p>\n<\/li>\n<li>\n<p>\u91c7\u7528PackedSequence\u5904\u7406\u53d8\u957f\u5e8f\u5217<\/p>\n<\/li>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">3.2 BERT\u9884\u8bad\u7ec3\u6a21\u578b<\/h3>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;transformers&nbsp;import&nbsp;BertTokenizer,&nbsp;BertForSequenceClassification\n\nclass&nbsp;BertSentimentModel:\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;model_name=&#39;bert-base-chinese&#39;):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.tokenizer&nbsp;=&nbsp;BertTokenizer.from_pretrained(model_name)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.model&nbsp;=&nbsp;BertForSequenceClassification.from_pretrained(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model_name,&nbsp;num_labels=2)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;predict(self,&nbsp;text):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputs&nbsp;=&nbsp;self.tokenizer(text,&nbsp;return_tensors=&quot;pt&quot;,&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max_length=128,&nbsp;truncation=True,&nbsp;padding=&#39;max_length&#39;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;torch.no_grad():\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputs&nbsp;=&nbsp;self.model(**inputs)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;torch.sigmoid(outputs.logits).item()&nbsp;&gt;&nbsp;0.5<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6027\u80fd\u5bf9\u6bd4<\/span>\uff1a<\/p>\n<p class=\"XurfUklW table-scroll\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 20px; margin-bottom: 0px; overflow-x: auto; width: 741.806px; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254); scrollbar-width: thin !important;\"><\/p>\n<table class=\"OxEWNITQ\" width=\"NaN\">\n<thead style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none;\">\n<tr style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none;\" class=\"firstRow\">\n<th style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Medium; list-style: none; margin: 0px; scrollbar-width: none; color: rgb(0, 24, 70); font-weight: 500; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; border-left-color: rgba(231, 229, 238, 0.7); border-top-left-radius: 10px; background: rgba(207, 222, 254, 0.2) !important; text-align: left !important; vertical-align: top !important;\">\u6a21\u578b\u7c7b\u578b<\/th>\n<th style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Medium; list-style: none; margin: 0px; scrollbar-width: none; color: rgb(0, 24, 70); font-weight: 500; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; background: rgba(207, 222, 254, 0.2) !important; text-align: left !important; vertical-align: top !important;\">\u8bad\u7ec3\u6570\u636e\u91cf<\/th>\n<th style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Medium; list-style: none; margin: 0px; scrollbar-width: none; color: rgb(0, 24, 70); font-weight: 500; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; background: rgba(207, 222, 254, 0.2) !important; text-align: left !important; vertical-align: top !important;\">\u51c6\u786e\u7387<\/th>\n<th style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Medium; list-style: none; margin: 0px; scrollbar-width: none; color: rgb(0, 24, 70); font-weight: 500; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; border-top-right-radius: 10px; background: rgba(207, 222, 254, 0.2) !important; text-align: left !important; vertical-align: top !important;\">\u63a8\u7406\u901f\u5ea6\uff08ms\/\u6837\u672c\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none;\">\n<tr style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none; background-color: rgb(253, 253, 254);\">\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-left-color: rgba(231, 229, 238, 0.7); vertical-align: top !important;\">LSTM<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">10\u4e07\u6761<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">82.7%<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">12.5<\/td>\n<\/tr>\n<tr style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none; background-color: rgb(253, 253, 254);\">\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-left-color: rgba(231, 229, 238, 0.7); vertical-align: top !important;\">BERT-base<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">10\u4e07\u6761<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">92.3%<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); vertical-align: top !important;\">45.8<\/td>\n<\/tr>\n<tr style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; list-style: none; margin: 0px; scrollbar-width: none; background-color: rgb(253, 253, 254);\">\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-bottom-left-radius: 10px; vertical-align: top !important;\">BERT-wwm-ext<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-bottom-color: rgba(231, 229, 238, 0.7); vertical-align: top !important;\">10\u4e07\u6761<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-bottom-color: rgba(231, 229, 238, 0.7); vertical-align: top !important;\">93.1%<\/td>\n<td style=\"box-sizing: border-box; padding: 10px 20px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Regular; list-style: none; margin: 0px; scrollbar-width: none; border-right-color: rgba(231, 229, 238, 0.7); border-top-color: rgba(231, 229, 238, 0.7); letter-spacing: 0px; max-width: 300px; color: rgb(18, 6, 73); border-bottom-color: rgba(231, 229, 238, 0.7); border-bottom-right-radius: 10px; vertical-align: top !important;\">48.2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 20px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u56db\u3001\u8bad\u7ec3\u4e0e\u4f18\u5316\u7b56\u7565<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.1 \u635f\u5931\u51fd\u6570\u9009\u62e9<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u4e8c\u5206\u7c7b\u4efb\u52a1\uff1a<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">nn.BCEWithLogitsLoss()<\/code>\uff08\u63a8\u8350\uff09<\/p>\n<\/li>\n<li>\n<p>\u591a\u5206\u7c7b\u4efb\u52a1\uff1a<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">nn.CrossEntropyLoss()<\/code><\/p>\n<\/li>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.2 \u4f18\u5316\u5668\u914d\u7f6e<\/h3>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\" class=\"prism-highlight prism-language-python\">def&nbsp;train_model(model,&nbsp;train_loader,&nbsp;val_loader,&nbsp;epochs=10):\n&nbsp;&nbsp;&nbsp;&nbsp;criterion&nbsp;=&nbsp;nn.BCEWithLogitsLoss()\n&nbsp;&nbsp;&nbsp;&nbsp;optimizer&nbsp;=&nbsp;torch.optim.Adam(model.parameters(),&nbsp;lr=0.001)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;epoch&nbsp;in&nbsp;range(epochs):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.train()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;train_loss&nbsp;=&nbsp;0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;batch&nbsp;in&nbsp;train_loader:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputs,&nbsp;labels&nbsp;=&nbsp;batch\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.zero_grad()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputs&nbsp;=&nbsp;model(inputs)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss&nbsp;=&nbsp;criterion(outputs.squeeze(),&nbsp;labels.float())\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss.backward()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.step()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;train_loss&nbsp;+=&nbsp;loss.item()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u9a8c\u8bc1\u9636\u6bb5\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val_loss,&nbsp;val_acc&nbsp;=&nbsp;evaluate(model,&nbsp;val_loader)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f&quot;Epoch&nbsp;{epoch+1}:&nbsp;Train&nbsp;Loss={train_loss\/len(train_loader):.4f},&nbsp;&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&quot;Val&nbsp;Acc={val_acc*100:.2f}%&quot;)<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.3 \u8bad\u7ec3\u5faa\u73af\u5b9e\u73b0<\/h3>\n<pre class=\"prism-highlight prism-language-python\">def&nbsp;train_model(model,&nbsp;train_loader,&nbsp;val_loader,&nbsp;epochs=10):\n&nbsp;&nbsp;&nbsp;&nbsp;criterion&nbsp;=&nbsp;nn.BCEWithLogitsLoss()\n&nbsp;&nbsp;&nbsp;&nbsp;optimizer&nbsp;=&nbsp;torch.optim.Adam(model.parameters(),&nbsp;lr=0.001)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;epoch&nbsp;in&nbsp;range(epochs):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.train()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;train_loss&nbsp;=&nbsp;0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;batch&nbsp;in&nbsp;train_loader:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputs,&nbsp;labels&nbsp;=&nbsp;batch\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.zero_grad()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputs&nbsp;=&nbsp;model(inputs)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss&nbsp;=&nbsp;criterion(outputs.squeeze(),&nbsp;labels.float())\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss.backward()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.step()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;train_loss&nbsp;+=&nbsp;loss.item()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u9a8c\u8bc1\u9636\u6bb5\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val_loss,&nbsp;val_acc&nbsp;=&nbsp;evaluate(model,&nbsp;val_loader)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f&quot;Epoch&nbsp;{epoch+1}:&nbsp;Train&nbsp;Loss={train_loss\/len(train_loader):.4f},&nbsp;&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&quot;Val&nbsp;Acc={val_acc*100:.2f}%&quot;)<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e94\u3001\u90e8\u7f72\u4e0e\u5e94\u7528\u5b9e\u8df5<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">5.1 \u6a21\u578b\u5bfc\u51fa\u4e0e\u63a8\u7406\u4f18\u5316<\/h3>\n<pre class=\"prism-highlight prism-language-python\">#&nbsp;\u5bfc\u51fa\u4e3aTorchScript\u683c\u5f0f\ntraced_model&nbsp;=&nbsp;torch.jit.trace(model.eval(),&nbsp;example_input)\ntraced_model.save(&quot;sentiment_model.pt&quot;)\n\n#&nbsp;\u4f7f\u7528ONNX\u52a0\u901f\u63a8\u7406\ndummy_input&nbsp;=&nbsp;torch.randint(0,&nbsp;20000,&nbsp;(1,&nbsp;128))\ntorch.onnx.export(model,&nbsp;dummy_input,&nbsp;&quot;sentiment.onnx&quot;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_names=[&#39;input&#39;],&nbsp;output_names=[&#39;output&#39;])<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">5.2 \u5b9e\u9645\u573a\u666f\u5e94\u7528\u793a\u4f8b<\/h3>\n<pre class=\"prism-highlight prism-language-python\">class&nbsp;SentimentAnalyzer:\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;model_path):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.model&nbsp;=&nbsp;torch.jit.load(model_path)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.vocab&nbsp;=&nbsp;self._load_vocab()&nbsp;&nbsp;#&nbsp;\u9700\u4fdd\u5b58\u8bad\u7ec3\u65f6\u7684\u8bcd\u6c47\u8868\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;analyze(self,&nbsp;text):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokens&nbsp;=&nbsp;jieba.lcut(text)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;token_ids&nbsp;=&nbsp;[self.vocab.get(token,&nbsp;0)&nbsp;for&nbsp;token&nbsp;in&nbsp;tokens][:128]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padded_input&nbsp;=&nbsp;token_ids&nbsp;+&nbsp;[0]*(128&nbsp;-&nbsp;len(token_ids))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_tensor&nbsp;=&nbsp;torch.tensor([padded_input])\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;torch.no_grad():\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;=&nbsp;torch.sigmoid(self.model(input_tensor))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;\u79ef\u6781&quot;&nbsp;if&nbsp;output.item()&nbsp;&gt;&nbsp;0.5&nbsp;else&nbsp;&quot;\u6d88\u6781&quot;<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":1,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}