{"id":65874,"date":"2026-01-25T20:24:56","date_gmt":"2026-01-25T12:24:56","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/65874.html"},"modified":"2026-01-25T20:24:56","modified_gmt":"2026-01-25T12:24:56","slug":"%e4%bb%8e%e9%9b%b6%e5%ae%9e%e7%8e%b0%e5%be%aa%e7%8e%af%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%ef%bc%9a%e4%b8%ad%e6%96%87%e6%83%85%e6%84%9f%e5%88%86%e6%9e%90%e7%9a%84%e5%ae%8c%e6%95%b4%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/65874.html","title":{"rendered":"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357"},"content":{"rendered":"<h2>1. \u5f15\u8a00&#xff1a;\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u5728\u60c5\u611f\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4ef7\u503c<\/h2>\n<p>\u5728\u5f53\u4eca\u5927\u6570\u636e\u65f6\u4ee3&#xff0c;\u60c5\u611f\u5206\u6790\u5df2\u6210\u4e3a\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff08;NLP&#xff09;\u9886\u57df\u4e2d\u6700\u91cd\u8981\u7684\u5e94\u7528\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7535\u5546\u5e73\u53f0\u7684\u4ea7\u54c1\u8bc4\u8bba\u5206\u6790&#xff0c;\u8fd8\u662f\u793e\u4ea4\u5a92\u4f53\u7684\u8206\u60c5\u76d1\u63a7&#xff0c;\u51c6\u786e\u8bc6\u522b\u7528\u6237\u60c5\u611f\u503e\u5411\u90fd\u5177\u6709\u5de8\u5927\u5546\u4e1a\u4ef7\u503c\u3002\u7136\u800c&#xff0c;\u4f20\u7edf\u7684\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u5728\u5904\u7406\u5e8f\u5217\u6570\u636e\u65f6\u9762\u4e34\u7740\u5de8\u5927\u6311\u6218&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u5177\u6709\u4e0a\u4e0b\u6587\u4f9d\u8d56\u7684\u6587\u672c\u6570\u636e\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u5faa\u73af\u795e\u7ecf\u7f51\u7edc&#xff08;RNN&#xff09;\u4f5c\u4e3a\u4e00\u79cd\u4e13\u95e8\u5904\u7406\u5e8f\u5217\u6570\u636e\u7684\u795e\u7ecf\u7f51\u7edc\u67b6\u6784&#xff0c;\u5728\u60c5\u611f\u5206\u6790\u4efb\u52a1\u4e2d\u5c55\u73b0\u51fa\u72ec\u7279\u4f18\u52bf\u3002\u4e0e\u4f20\u7edf\u65b9\u6cd5\u4e0d\u540c&#xff0c;RNN\u80fd\u591f\u6355\u6349\u6587\u672c\u4e2d\u7684\u65f6\u5e8f\u4fe1\u606f\u548c\u957f\u671f\u4f9d\u8d56\u5173\u7cfb&#xff0c;\u8fd9\u5bf9\u4e8e\u7406\u89e3\u81ea\u7136\u8bed\u8a00\u7684\u8bed\u4e49\u81f3\u5173\u91cd\u8981\u3002\u4f8b\u5982&#xff0c;\u5728\u4e2d\u6587\u60c5\u611f\u5206\u6790\u4e2d&#xff0c;&#034;\u8fd9\u4e2a\u4ea7\u54c1\u867d\u7136\u8d35\u4f46\u662f\u8d28\u91cf\u5f88\u597d&#034;\u8fd9\u6837\u7684\u53e5\u5b50&#xff0c;\u53ea\u6709\u7406\u89e3\u6574\u4e2a\u53e5\u5b50\u4e0a\u4e0b\u6587\u624d\u80fd\u51c6\u786e\u5224\u65ad\u5176\u79ef\u6781\u60c5\u611f\u503e\u5411\u3002<\/span><\/p>\n<p>\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb&#xff0c;\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684RNN\u6a21\u578b&#xff0c;\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u4e2d\u6587\u60c5\u611f\u5206\u6790\u4efb\u52a1\u3002\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u8df5\u9879\u76ee&#xff0c;\u60a8\u4e0d\u4ec5\u5c06\u6df1\u5165\u7406\u89e3RNN\u7684\u5de5\u4f5c\u539f\u7406&#xff0c;\u8fd8\u80fd\u638c\u63e1\u4ece\u6570\u636e\u5904\u7406\u5230\u6a21\u578b\u8bad\u7ec3\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<h2>2. \u73af\u5883\u51c6\u5907\u4e0e\u7406\u8bba\u57fa\u7840<\/h2>\n<h3>2.1. \u6838\u5fc3\u5e93\u4ecb\u7ecd<\/h3>\n<p>\u6784\u5efaRNN\u6a21\u578b\u9700\u8981\u591a\u4e2aPython\u5e93\u7684\u652f\u6301&#xff0c;\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u7279\u5b9a\u4f5c\u7528&#xff1a;<\/p>\n<p># \u5bfc\u5165\u6570\u503c\u8ba1\u7b97\u5e93<br \/>\nimport numpy as np<br \/>\n# \u5bfc\u5165\u6570\u636e\u5904\u7406\u5e93<br \/>\nimport pandas as pd<br \/>\n# \u5bfc\u5165\u4e2d\u6587\u5206\u8bcd\u5e93<br \/>\nimport jieba as jb<br \/>\n# \u5bfc\u5165\u6570\u636e\u96c6\u5212\u5206\u5de5\u5177<br \/>\nfrom sklearn.model_selection import train_test_split<br \/>\n# \u5bfc\u5165\u6a21\u578b\u8bc4\u4f30\u5de5\u5177&#xff0c;\u7528\u4e8e\u8ba1\u7b97\u6a21\u578b\u7684\u51c6\u786e\u7387<br \/>\nfrom sklearn.metrics import accuracy_score<br \/>\n# \u5bfc\u5165\u8b66\u544a\u8fc7\u6ee4\u5de5\u5177<br \/>\nimport warnings<br \/>\n# \u5ffd\u7565\u8b66\u544a\u4fe1\u606f<br \/>\nwarnings.filterwarnings(&#034;ignore&#034;)<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">NumPy&#xff1a;\u63d0\u4f9b\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c&#xff0c;\u662f\u795e\u7ecf\u7f51\u7edc\u8ba1\u7b97\u7684\u57fa\u7840<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">Pandas&#xff1a;\u7b80\u5316\u6570\u636e\u5904\u7406\u6d41\u7a0b&#xff0c;\u65b9\u4fbf\u6570\u636e\u63a2\u7d22\u548c\u5206\u6790<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">Jieba&#xff1a;\u51c6\u786e\u9ad8\u6548\u7684\u4e2d\u6587\u5206\u8bcd\u5de5\u5177<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">scikit-learn&#xff1a;\u63d0\u4f9b\u6570\u636e\u96c6\u5212\u5206\u548c\u8bc4\u4f30\u529f\u80fd<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">warnings&#xff1a;\u63a7\u5236\u8b66\u544a\u4fe1\u606f\u8f93\u51fa&#xff0c;\u4fdd\u6301\u4ee3\u7801\u6574\u6d01<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>2.2. RNN\u7406\u8bba\u57fa\u7840<\/h3>\n<p><span style=\"color:#38d8f0\">1.\u00a0RNN\u7684\u6838\u5fc3\u601d\u60f3<\/span><\/p>\n<p>\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u901a\u8fc7\u5f15\u5165\u5faa\u73af\u8fde\u63a5&#xff0c;\u4f7f\u7f51\u7edc\u80fd\u591f&#034;\u8bb0\u4f4f&#034;\u4e4b\u524d\u7684\u4fe1\u606f\u3002\u8fd9\u79cd\u8bb0\u5fc6\u80fd\u529b\u4f7fRNN\u7279\u522b\u9002\u5408\u5904\u7406\u5e8f\u5217\u6570\u636e&#xff0c;\u5982\u6587\u672c\u3001\u8bed\u97f3\u3001\u65f6\u95f4\u5e8f\u5217\u7b49\u3002<\/p>\n<p><span style=\"color:#38d8f0\">2.\u00a0RNN\u7684\u57fa\u672c\u7ed3\u6784<\/span><\/p>\n<p>RNN\u5728\u65f6\u95f4\u6b65t\u7684\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u9690\u85cf\u72b6\u6001&#xff1a;h_t &#061; tanh(W_xh\u00b7x_t &#043; W_hh\u00b7h_{t-1} &#043; b_h)<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u8f93\u51fa&#xff1a;y_t &#061; softmax(W_hy\u00b7h_t &#043; b_y)<\/span><\/p>\n<\/li>\n<\/ul>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">x_t&#xff1a;\u65f6\u95f4\u6b65t\u7684\u8f93\u5165<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">h_t&#xff1a;\u65f6\u95f4\u6b65t\u7684\u9690\u85cf\u72b6\u6001<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">y_t&#xff1a;\u65f6\u95f4\u6b65t\u7684\u8f93\u51fa<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">W_xh, W_hh, W_hy&#xff1a;\u6743\u91cd\u77e9\u9635<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">b_h, b_y&#xff1a;\u504f\u7f6e\u9879<\/span><\/p>\n<\/li>\n<\/ul>\n<h2>3. SimpleRNN\u7c7b\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0<\/h2>\n<h3>3.1. \u7c7b\u521d\u59cb\u5316<\/h3>\n<p>RNN\u6a21\u578b\u7684\u521d\u59cb\u5316\u6d89\u53ca\u591a\u4e2a\u91cd\u8981\u53c2\u6570\u7684\u8bbe\u7f6e&#xff1a;<\/p>\n<p>class SimpleRNN:<br \/>\n    &#034;&#034;&#034;\u7b80\u5355RNN\u6a21\u578b\u521d\u59cb\u5316&#034;&#034;&#034;<br \/>\n    def __init__(self, input_size, hidden_size, output_size, learning_rate&#061;0.01):<br \/>\n        # \u53c2\u6570\u8bf4\u660e&#xff1a;<br \/>\n        # input_size: \u8f93\u5165\u7279\u5f81\u5927\u5c0f<br \/>\n        # hidden_size: \u9690\u85cf\u5c42\u5927\u5c0f<br \/>\n        # output_size: \u8f93\u51fa\u5c42\u5927\u5c0f<br \/>\n        # learning_rate: \u5b66\u4e60\u7387&#xff0c;\u9ed8\u8ba4\u503c\u4e3a0.01<br \/>\n        self.input_size &#061; input_size<br \/>\n        self.hidden_size &#061; hidden_size<br \/>\n        self.output_size &#061; output_size<br \/>\n        self.learning_rate &#061; learning_rate<\/p>\n<p>        # \u521d\u59cb\u5316\u6743\u91cd\u77e9\u9635<br \/>\n        self.W_xh &#061; np.random.randn(input_size, hidden_size) * 0.01<br \/>\n        self.W_hh &#061; np.random.randn(hidden_size, hidden_size) * 0.01<br \/>\n        self.W_hy &#061; np.random.randn(hidden_size, output_size) * 0.01<\/p>\n<p>        # \u521d\u59cb\u5316\u504f\u7f6e\u9879<br \/>\n        self.b_h &#061; np.zeros((1, hidden_size))<br \/>\n        self.b_y &#061; np.zeros((1, output_size))<\/p>\n<p>\u6743\u91cd\u521d\u59cb\u5316\u91c7\u7528\u5c0f\u968f\u673a\u6570\u7b56\u7565&#xff08;\u4e58\u4ee50.01&#xff09;&#xff0c;\u8fd9\u79cd\u7b56\u7565\u6709\u52a9\u4e8e\u907f\u514d\u68af\u5ea6\u7206\u70b8\u6216\u6d88\u5931\u95ee\u9898\u3002\u504f\u7f6e\u9879\u521d\u59cb\u5316\u4e3a\u96f6&#xff0c;\u8fd9\u662f\u795e\u7ecf\u7f51\u7edc\u4e2d\u7684\u5e38\u89c1\u505a\u6cd5\u3002<\/p>\n<h3>3.2. \u6fc0\u6d3b\u51fd\u6570\u5b9e\u73b0<\/h3>\n<p>\u6fc0\u6d3b\u51fd\u6570\u662f\u795e\u7ecf\u7f51\u7edc\u7684\u975e\u7ebf\u6027\u6765\u6e90&#xff0c;\u4f7f\u7f51\u7edc\u80fd\u591f\u5b66\u4e60\u590d\u6742\u7684\u6a21\u5f0f&#xff1a;<\/p>\n<p>&#034;&#034;&#034;Tanh\u6fc0\u6d3b\u51fd\u6570&#034;&#034;&#034;<br \/>\ndef tanh(self, x):<br \/>\n    return np.tanh(x)  # \u9690\u85cf\u5c42\u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>&#034;&#034;&#034;Softmax\u6fc0\u6d3b\u51fd\u6570&#034;&#034;&#034;<br \/>\ndef softmax(self, x):<br \/>\n    exp_x &#061; np.exp(x &#8211; np.max(x, axis&#061;1, keepdims&#061;True))  # \u9632\u6b62\u6ea2\u51fa<br \/>\n    return exp_x \/ np.sum(exp_x, axis&#061;1, keepdims&#061;True)  # \u8f93\u51fa\u5c42\u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>\u6fc0\u6d3b\u51fd\u6570\u9009\u62e9\u4f9d\u636e&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">Tanh&#xff1a;\u8f93\u51fa\u8303\u56f4\u5728[-1, 1]&#xff0c;\u9002\u5408\u4f5c\u4e3a\u9690\u85cf\u5c42\u6fc0\u6d3b\u51fd\u6570<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">Softmax&#xff1a;\u5c06\u8f93\u51fa\u8f6c\u6362\u4e3a\u6982\u7387\u5206\u5e03&#xff0c;\u9002\u5408\u591a\u5206\u7c7b\u95ee\u9898<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>3.3. \u524d\u5411\u4f20\u64ad\u5b9e\u73b0<\/h3>\n<p>\u524d\u5411\u4f20\u64ad\u662fRNN\u7684\u6838\u5fc3&#xff0c;\u8d1f\u8d23\u8ba1\u7b97\u7f51\u7edc\u8f93\u51fa&#xff1a;<\/p>\n<p>&#034;&#034;&#034;\u524d\u5411\u4f20\u64ad&#034;&#034;&#034;<br \/>\ndef forward(self, X):<br \/>\n    # \u83b7\u53d6\u8f93\u5165\u5e8f\u5217\u957f\u5ea6\u3001\u6279\u6b21\u5927\u5c0f<br \/>\n    seq_len, batch_size, _ &#061; X.shape<br \/>\n    # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u72b6\u6001<br \/>\n    self.h &#061; np.zeros((seq_len &#043; 1, batch_size, self.hidden_size))<br \/>\n    # \u521d\u59cb\u5316\u8f93\u51fa\u5c42\u72b6\u6001<br \/>\n    self.y &#061; np.zeros((seq_len, batch_size, self.output_size))<\/p>\n<p>    for t in range(seq_len):<br \/>\n        # \u8ba1\u7b97\u9690\u85cf\u5c42\u72b6\u6001<br \/>\n        # h_t &#061; tanh(W_xh * x_t &#043; W_hh * h_t-1 &#043; b_h)<br \/>\n        self.h[t &#043; 1] &#061; self.tanh(np.dot(X[t], self.W_xh) &#043;<br \/>\n                                  np.dot(self.h[t], self.W_hh) &#043; self.b_h)<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u72b6\u6001<br \/>\n        # y_t &#061; softmax(W_hy * h_t &#043; b_y)<br \/>\n        self.y[t] &#061; self.softmax(np.dot(self.h[t &#043; 1], self.W_hy) &#043; self.b_y)<\/p>\n<p>    return self.y<\/p>\n<p><span style=\"color:null\">\u65f6\u95f4\u6b65\u8ba1\u7b97\u6d41\u7a0b&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u8f93\u5165\u51c6\u5907&#xff1a;\u63a5\u6536\u4e09\u7ef4\u5f20\u91cf&#xff08;\u5e8f\u5217\u957f\u5ea6\u00d7\u6279\u6b21\u5927\u5c0f\u00d7\u7279\u5f81\u7ef4\u5ea6&#xff09;<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u72b6\u6001\u521d\u59cb\u5316&#xff1a;\u9690\u85cf\u72b6\u6001\u521d\u59cb\u5316\u4e3a\u96f6\u5411\u91cf<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u5faa\u73af\u8ba1\u7b97&#xff1a;\u6309\u65f6\u95f4\u6b65\u987a\u5e8f\u8ba1\u7b97\u6bcf\u4e2a\u65f6\u95f4\u6b65\u7684\u9690\u85cf\u72b6\u6001\u548c\u8f93\u51fa<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>3.4. \u53cd\u5411\u4f20\u64ad\u5b9e\u73b0<\/h3>\n<p>\u53cd\u5411\u4f20\u64ad\u901a\u8fc7\u65f6\u95f4&#xff08;BPTT&#xff09;\u662fRNN\u8bad\u7ec3\u7684\u5173\u952e&#xff1a;<\/p>\n<p>&#034;&#034;&#034;\u53cd\u5411\u4f20\u64ad&#034;&#034;&#034;<br \/>\ndef backward(self, X, y):<br \/>\n    seq_len, batch_size, _ &#061; X.shape<br \/>\n    self.dh &#061; np.zeros((seq_len &#043; 2, batch_size, self.hidden_size))<br \/>\n    self.dy &#061; np.zeros((seq_len, batch_size, self.output_size))<br \/>\n    self.dx &#061; np.zeros((seq_len, batch_size, self.input_size))<\/p>\n<p>    # \u521d\u59cb\u5316\u68af\u5ea6<br \/>\n    self.dW_hy &#061; np.zeros_like(self.W_hy)<br \/>\n    self.dW_hh &#061; np.zeros_like(self.W_hh)<br \/>\n    self.dW_xh &#061; np.zeros_like(self.W_xh)<br \/>\n    self.db_h &#061; np.zeros_like(self.b_h)<br \/>\n    self.db_y &#061; np.zeros_like(self.b_y)<\/p>\n<p>    # \u4ece\u6700\u540e\u4e00\u4e2a\u65f6\u95f4\u6b65\u5f00\u59cb\u53cd\u5411\u4f20\u64ad<br \/>\n    for t in reversed(range(seq_len)):<br \/>\n        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u72b6\u6001\u68af\u5ea6<br \/>\n        self.dy[t] &#061; self.y[t] &#8211; y[t]<\/p>\n<p>        # \u8ba1\u7b97\u9690\u85cf\u5c42\u5230\u8f93\u51fa\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n        self.dW_hy &#043;&#061; np.dot(self.h[t &#043; 1].T, self.dy[t])<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u504f\u7f6e\u9879\u68af\u5ea6<br \/>\n        self.db_y &#043;&#061; np.sum(self.dy[t], axis&#061;0, keepdims&#061;True)<\/p>\n<p>        # \u8ba1\u7b97\u5f53\u524d\u8f93\u51fa\u8bef\u5dee\u5f15\u8d77\u7684\u9690\u85cf\u5c42\u68af\u5ea6<br \/>\n        dh_from_output &#061; np.dot(self.dy[t], self.W_hy.T)<\/p>\n<p>        # \u8ba1\u7b97\u4e0b\u4e00\u65f6\u95f4\u6b65\u7684\u9690\u85cf\u5c42\u68af\u5ea6<br \/>\n        dh_from_next &#061; np.dot(self.dh[t &#043; 2], self.W_hh.T) if (t &#043; 1) &lt; seq_len else 0<\/p>\n<p>        # \u5408\u5e76\u68af\u5ea6\u5e76\u5e94\u7528tanh\u7684\u5bfc\u6570<br \/>\n        self.dh[t &#043; 1] &#061; (dh_from_output &#043; dh_from_next) * (1 &#8211; self.h[t &#043; 1] ** 2)<\/p>\n<p>        # \u8ba1\u7b97\u6743\u91cd\u68af\u5ea6<br \/>\n        self.dW_hh &#043;&#061; np.dot(self.h[t].T, self.dh[t &#043; 1])<br \/>\n        self.dW_xh &#043;&#061; np.dot(X[t].T, self.dh[t &#043; 1])<\/p>\n<p>        # \u8ba1\u7b97\u504f\u7f6e\u68af\u5ea6<br \/>\n        self.db_h &#043;&#061; np.sum(self.dh[t &#043; 1], axis&#061;0, keepdims&#061;True)<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u5165\u68af\u5ea6<br \/>\n        self.dx[t] &#061; np.dot(self.dh[t &#043; 1], self.W_xh.T)<\/p>\n<p>    return self.dx<\/p>\n<p>BPTT\u7b97\u6cd5\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u65f6\u95f4\u53cd\u5411\u4f20\u64ad&#xff1a;\u4ece\u6700\u540e\u4e00\u4e2a\u65f6\u95f4\u6b65\u5f00\u59cb&#xff0c;\u5411\u524d\u4f20\u64ad\u68af\u5ea6<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u68af\u5ea6\u94fe\u5f0f\u6cd5\u5219&#xff1a;\u901a\u8fc7\u94fe\u5f0f\u6cd5\u5219\u8ba1\u7b97\u6bcf\u4e2a\u53c2\u6570\u7684\u68af\u5ea6<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u68af\u5ea6\u7d2f\u79ef&#xff1a;RNN\u4e2d\u540c\u4e00\u53c2\u6570\u5728\u4e0d\u540c\u65f6\u95f4\u6b65\u5171\u4eab&#xff0c;\u68af\u5ea6\u9700\u8981\u7d2f\u52a0<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>3.5. \u6743\u91cd\u66f4\u65b0\u4e0e\u8bad\u7ec3<\/h3>\n<p>&#034;&#034;&#034;\u6743\u91cd\u66f4\u65b0&#034;&#034;&#034;<br \/>\ndef update_weights(self):<br \/>\n    # \u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5\u66f4\u65b0\u6240\u6709\u6743\u91cd\u548c\u504f\u7f6e<br \/>\n    self.W_xh -&#061; self.learning_rate * self.dW_xh<br \/>\n    self.W_hh -&#061; self.learning_rate * self.dW_hh<br \/>\n    self.W_hy -&#061; self.learning_rate * self.dW_hy<br \/>\n    self.b_h -&#061; self.learning_rate * self.db_h<br \/>\n    self.b_y -&#061; self.learning_rate * self.db_y<\/p>\n<p>&#034;&#034;&#034;\u6a21\u578b\u8bad\u7ec3&#034;&#034;&#034;<br \/>\ndef train(self, X, y, epochs&#061;100):<br \/>\n    # \u8bad\u7ec3\u6a21\u578b<br \/>\n    for epoch in range(epochs):<br \/>\n        # \u524d\u5411\u4f20\u64ad<br \/>\n        y_pred &#061; self.forward(X)<br \/>\n        # \u53cd\u5411\u4f20\u64ad<br \/>\n        self.backward(X, y)<br \/>\n        # \u66f4\u65b0\u6743\u91cd<br \/>\n        self.update_weights()<\/p>\n<p>        # \u8ba1\u7b97\u635f\u5931<br \/>\n        loss &#061; -np.mean(np.sum(y * np.log(y_pred &#043; 1e-8), axis&#061;1))<br \/>\n        # \u6253\u5370\u635f\u5931<br \/>\n        if epoch % 10 &#061;&#061; 0:<br \/>\n            print(f&#034;Epoch {epoch}, Loss: {loss:.4f}&#034;)<\/p>\n<h2>4. \u6570\u636e\u5904\u7406\u4e0e\u9884\u5904\u7406<\/h2>\n<h3>4.1. \u6570\u636e\u96c6\u521b\u5efa<\/h3>\n<p>def create_dataset():<br \/>\n    &#034;&#034;&#034;\u521b\u5efa\u7b80\u5355\u7684\u60c5\u611f\u5206\u6790\u6570\u636e\u96c6&#034;&#034;&#034;<br \/>\n    # \u6269\u5c55\u8bad\u7ec3\u6570\u636e\u96c6<br \/>\n    data &#061; {<br \/>\n        &#039;text&#039;: [<br \/>\n            &#039;\u8fd9\u4e2a\u4ea7\u54c1\u5f88\u597d&#039;, &#039;\u8d28\u91cf\u592a\u5dee\u4e86&#039;, &#039;\u670d\u52a1\u4e0d\u9519&#039;, &#039;\u4ef7\u683c\u592a\u8d35&#039;,<br \/>\n            &#039;\u7269\u6d41\u5f88\u5feb&#039;, &#039;\u5305\u88c5\u7cbe\u7f8e&#039;, &#039;\u5ba2\u670d\u8010\u5fc3&#039;, &#039;\u5546\u54c1\u6709\u7455\u75b5&#039;,<br \/>\n            &#039;\u6027\u4ef7\u6bd4\u9ad8&#039;, &#039;\u4f7f\u7528\u4e0d\u65b9\u4fbf&#039;, &#039;\u989c\u8272\u5f88\u597d\u770b&#039;, &#039;\u5c3a\u5bf8\u4e0d\u5408\u9002&#039;,<br \/>\n            &#039;\u6750\u8d28\u4e0d\u9519&#039;, &#039;\u5473\u9053\u4e0d\u597d\u95fb&#039;, &#039;\u4ef7\u683c\u5408\u7406&#039;, &#039;\u7269\u6d41\u592a\u6162&#039;,<br \/>\n            &#039;\u5305\u88c5\u7834\u635f&#039;, &#039;\u5ba2\u670d\u6001\u5ea6\u597d&#039;, &#039;\u8fd9\u4e2a\u4e1c\u897f\u5f88\u597d\u7528&#039;, &#039;\u8d28\u91cf\u975e\u5e38\u5dee&#039;,<br \/>\n            &#039;\u670d\u52a1\u6001\u5ea6\u5f88\u597d&#039;, &#039;\u4ef7\u683c\u771f\u7684\u8d35&#039;, &#039;\u7269\u6d41\u901f\u5ea6\u5f88\u5feb&#039;, &#039;\u5305\u88c5\u5f88\u7528\u5fc3&#039;,<br \/>\n            &#039;\u5ba2\u670d\u975e\u5e38\u8010\u5fc3&#039;, &#039;\u5546\u54c1\u8d28\u91cf\u6709\u95ee\u9898&#039;, &#039;\u6027\u4ef7\u6bd4\u975e\u5e38\u9ad8&#039;, &#039;\u4f7f\u7528\u8d77\u6765\u4e0d\u65b9\u4fbf&#039;<br \/>\n        ],<br \/>\n        &#039;label&#039;: [1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0]<br \/>\n    }<\/p>\n<p>    # \u8f6c\u6362\u4e3aDataFrame<br \/>\n    df &#061; pd.DataFrame(data)<br \/>\n    return df<\/p>\n<p><span style=\"color:null\">\u6570\u636e\u96c6\u8bbe\u8ba1\u539f\u5219&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u7c7b\u522b\u5e73\u8861&#xff1a;\u786e\u4fdd\u6b63\u9762\u548c\u8d1f\u9762\u6837\u672c\u6570\u91cf\u63a5\u8fd1<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u591a\u6837\u6027&#xff1a;\u8986\u76d6\u4e0d\u540c\u4e3b\u9898\u548c\u8868\u8fbe\u65b9\u5f0f<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u771f\u5b9e\u6027&#xff1a;\u6a21\u62df\u771f\u5b9e\u573a\u666f\u4e2d\u7684\u8bc4\u8bba\u5185\u5bb9<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>4.2. \u6587\u672c\u9884\u5904\u7406<\/h3>\n<p>\u9884\u5904\u7406\u6b65\u9aa4\u76f4\u63a5\u5f71\u54cd\u6a21\u578b\u6027\u80fd&#xff1a;<\/p>\n<p># \u6587\u672c\u9884\u5904\u7406<br \/>\ndef preprocess_text(text):<br \/>\n    # \u5206\u8bcd<br \/>\n    seg_list &#061; jb.cut(text)<br \/>\n    # \u5408\u5e76\u5206\u8bcd\u7ed3\u679c<br \/>\n    return &#034; &#034;.join(seg_list)<\/p>\n<p>\u4e2d\u6587\u5206\u8bcd\u7684\u91cd\u8981\u6027&#xff0c;\u4e2d\u6587\u6ca1\u6709\u81ea\u7136\u7684\u5206\u9694\u7b26&#xff0c;\u5206\u8bcd\u662f\u5c06\u8fde\u7eed\u5b57\u7b26\u8f6c\u6362\u4e3a\u6709\u610f\u4e49\u7684\u8bcd\u8bed\u7684\u5173\u952e\u6b65\u9aa4\u3002\u51c6\u786e\u7684\u5206\u8bcd\u6709\u52a9\u4e8e\u6a21\u578b\u7406\u89e3\u8bed\u4e49\u3002<\/p>\n<h3>4.3. \u5e8f\u5217\u5316\u4e0e\u7f16\u7801<\/h3>\n<p>\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u6570\u503c\u8868\u793a\u662fNLP\u4efb\u52a1\u7684\u6838\u5fc3&#xff1a;<\/p>\n<p># \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5e8f\u5217<br \/>\ndef text_to_sequence(text, vocab, max_len&#061;5):<br \/>\n    words &#061; text.split()  # \u6309\u7a7a\u683c\u5206\u8bcd<br \/>\n    sequence &#061; []  # \u521d\u59cb\u5316\u5e8f\u5217<br \/>\n    for word in words[:max_len]:  # \u53ea\u53d6\u524dmax_len\u4e2a\u5355\u8bcd<br \/>\n        if word in vocab:<br \/>\n            sequence.append(vocab[word])  # \u83b7\u53d6\u8bcd\u5bf9\u5e94\u7684ID<br \/>\n        else:<br \/>\n            sequence.append(0)  # \u672a\u77e5\u8bcd\u75280\u8868\u793a<\/p>\n<p>    # \u586b\u5145\u5e8f\u5217\u5230\u56fa\u5b9a\u957f\u5ea6(\u4e0d\u8db3\u7684\u90e8\u5206\u75280\u586b\u5145)<br \/>\n    while len(sequence) &lt; max_len:<br \/>\n        sequence.append(0)<\/p>\n<p>    return sequence<\/p>\n<p>One-hot\u7f16\u7801\u539f\u7406&#xff1a;One-hot\u7f16\u7801\u5c06\u6bcf\u4e2a\u8bcd\u8868\u793a\u4e3a\u957f\u5ea6\u4e3a\u8bcd\u6c47\u8868\u5927\u5c0f\u7684\u5411\u91cf&#xff0c;\u5176\u4e2d\u53ea\u6709\u4e00\u4e2a\u4f4d\u7f6e\u4e3a1&#xff0c;\u5176\u4f59\u4e3a0\u3002\u867d\u7136\u7b80\u5355\u76f4\u89c2&#xff0c;\u4f46\u5728\u8bcd\u6c47\u8868\u8f83\u5927\u65f6\u4f1a\u4ea7\u751f\u9ad8\u7ef4\u7a00\u758f\u95ee\u9898\u3002<\/p>\n<h2>5. \u5b9e\u9a8c\u6d41\u7a0b\u4e0e\u7ed3\u679c\u5206\u6790<\/h2>\n<h3>5.1. \u5b8c\u6574\u5b9e\u9a8c\u6d41\u7a0b<\/h3>\n<p>\u4e3b\u51fd\u6570\u6574\u5408\u4e86\u4ece\u6570\u636e\u51c6\u5907\u5230\u6a21\u578b\u8bc4\u4f30\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;<br \/>\n\u00a0<\/p>\n<p>def main():<br \/>\n    # 1.\u6570\u636e\u51c6\u5907<br \/>\n    print(&#034;&#061;&#061;&#061;\u6570\u636e\u51c6\u5907&#061;&#061;&#061;&#034;)<br \/>\n    df &#061; create_dataset()<br \/>\n    print(f&#034;\u6570\u636e\u96c6\u5927\u5c0f&#xff1a;{len(df)}&#034;)<\/p>\n<p>    # 2.\u6587\u672c\u9884\u5904\u7406<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u6587\u672c\u9884\u5904\u7406&#061;&#061;&#061;&#034;)<br \/>\n    df[&#039;processed&#039;] &#061; df[&#039;text&#039;].apply(preprocess_text)<\/p>\n<p>    # 3.\u6784\u5efa\u8bcd\u6c47\u8868<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u6784\u5efa\u8bcd\u6c47\u8868&#061;&#061;&#061;&#034;)<br \/>\n    vocab &#061; {&#039;&lt;PAD&gt;&#039;: 0}<br \/>\n    for text in df[&#039;processed&#039;]:<br \/>\n        for word in text.split():<br \/>\n            if word not in vocab:<br \/>\n                vocab[word] &#061; len(vocab)<\/p>\n<p>    vocab_size &#061; len(vocab)<br \/>\n    print(f&#034;\u8bcd\u6c47\u8868\u5927\u5c0f&#xff1a;{vocab_size}&#034;)<\/p>\n<p>    # 4.\u8f6c\u6362\u4e3a\u5e8f\u5217<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u5e8f\u5217\u8f6c\u6362&#061;&#061;&#061;&#034;)<br \/>\n    max_len &#061; 5<br \/>\n    sequences &#061; []<br \/>\n    for text in df[&#039;processed&#039;]:<br \/>\n        seq &#061; text_to_sequence(text, vocab, max_len)<br \/>\n        sequences.append(seq)<\/p>\n<p>    # \u8f6c\u6362\u4e3a\u72ec\u70ed\u7f16\u7801<br \/>\n    X &#061; np.zeros((len(sequences), max_len, vocab_size))<br \/>\n    for i, seq in enumerate(sequences):<br \/>\n        for j, word_index in enumerate(seq):<br \/>\n            X[i, j, word_index] &#061; 1<\/p>\n<p>    # \u6807\u7b7e\u7f16\u7801<br \/>\n    y &#061; df[&#039;label&#039;].values<br \/>\n    y_onehot &#061; np.zeros((len(y), 2))<br \/>\n    y_onehot[np.arange(len(y)), y] &#061; 1<\/p>\n<p>    # 5.\u6570\u636e\u96c6\u5206\u5272<br \/>\n    X_train, X_test, y_train, y_test &#061; train_test_split(<br \/>\n        X, y_onehot, test_size&#061;0.25, random_state&#061;42<br \/>\n    )<br \/>\n    # \u8c03\u6574\u7ef4\u5ea6\u4e3a(seq_len, batch_size, input_size)<br \/>\n    X_train &#061; X_train.transpose(1, 0, 2)<br \/>\n    X_test &#061; X_test.transpose(1, 0, 2)<\/p>\n<p>    # 6.\u8bad\u7ec3RNN\u6a21\u578b<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u8bad\u7ec3RNN\u6a21\u578b&#061;&#061;&#061;&#034;)<br \/>\n    hidden_size &#061; 32<br \/>\n    output_size &#061; 2<\/p>\n<p>    model &#061; SimpleRNN(vocab_size, hidden_size, output_size, learning_rate&#061;0.01)<br \/>\n    model.train(X_train, y_train, epochs&#061;200)<\/p>\n<p>    # 7.\u6a21\u578b\u8bc4\u4f30<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u6a21\u578b\u8bc4\u4f30&#061;&#061;&#061;&#034;)<br \/>\n    y_pred &#061; model.predict(X_test)<br \/>\n    y_test_labels &#061; np.argmax(y_test, axis&#061;1)<br \/>\n    accuracy &#061; accuracy_score(y_test_labels, y_pred)<br \/>\n    print(f&#034;\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387&#xff1a;{accuracy:.4f}&#034;)<\/p>\n<p>    # 8.\u9884\u6d4b\u65b0\u6587\u672c<br \/>\n    print(&#034;\\\\n&#061;&#061;&#061;\u9884\u6d4b\u65b0\u6587\u672c&#061;&#061;&#061;&#034;)<br \/>\n    new_texts &#061; [<br \/>\n        &#039;\u8fd9\u4e2a\u5546\u54c1\u5f88\u6709\u4ef7\u503c&#039;,<br \/>\n        &#039;\u670d\u52a1\u7cdf\u7cd5&#039;,<br \/>\n        &#039;\u7269\u6d41\u5f88\u53ca\u65f6&#039;,<br \/>\n        &#039;\u5305\u88c5\u5f88\u7cbe\u7f8e&#039;,<br \/>\n        &#039;\u5ba2\u670d\u5f88\u8010\u5fc3&#039;<br \/>\n    ]<\/p>\n<p>    for text in new_texts:<br \/>\n        processed &#061; preprocess_text(text)<br \/>\n        seq &#061; text_to_sequence(processed, vocab, max_len)<\/p>\n<p>        x &#061; np.zeros((max_len, 1, vocab_size))<br \/>\n        for j, word_id in enumerate(seq):<br \/>\n            x[j, 0, word_id] &#061; 1<\/p>\n<p>        result &#061; model.predict(x)[0]<br \/>\n        sentiment &#061; &#034;\u6b63\u9762&#034; if result &#061;&#061; 1 else &#034;\u8d1f\u9762&#034;<br \/>\n        print(f&#034;\u6587\u672c: {text}&#034;)<br \/>\n        print(f&#034;\u9884\u6d4b: {sentiment}&#034;)<br \/>\n        print()<\/p>\n<h3>5.2. \u5b9e\u9a8c\u7ed3\u679c\u5206\u6790<\/h3>\n<p><span style=\"color:#fe2c24\">1.\u00a0\u8bad\u7ec3\u8fc7\u7a0b\u89c2\u5bdf<\/span><\/p>\n<p>\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d&#xff0c;\u635f\u5931\u51fd\u6570\u5e94\u5448\u73b0\u4e0b\u964d\u8d8b\u52bf\u3002\u5982\u679c\u635f\u5931\u6ce2\u52a8\u8f83\u5927\u6216\u4e0b\u964d\u7f13\u6162&#xff0c;\u53ef\u80fd\u9700\u8981\u8c03\u6574\u5b66\u4e60\u7387\u6216\u68c0\u67e5\u6570\u636e\u9884\u5904\u7406\u8fc7\u7a0b\u3002<\/p>\n<p><span style=\"color:#fe2c24\">2.\u00a0\u51c6\u786e\u7387\u8bc4\u4f30<\/span><\/p>\n<p>\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u51c6\u786e\u7387\u53cd\u6620\u4e86\u5176\u6cdb\u5316\u80fd\u529b\u3002\u5bf9\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898&#xff0c;\u51c6\u786e\u7387\u8d85\u8fc7\u968f\u673a\u731c\u6d4b&#xff08;50%&#xff09;\u5373\u8868\u793a\u6a21\u578b\u5b66\u5230\u4e86\u4e00\u4e9b\u6a21\u5f0f\u3002<\/p>\n<h2>6. \u6a21\u578b\u4f18\u5316\u4e0e\u6539\u8fdb\u65b9\u5411<\/h2>\n<h3>6.1. \u5f53\u524d\u5b9e\u73b0\u5c40\u9650\u6027<\/h3>\n<p>\u5f53\u524d\u7684SimpleRNN\u5b9e\u73b0\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u68af\u5ea6\u6d88\u5931\/\u7206\u70b8&#xff1a;\u5728\u5904\u7406\u957f\u5e8f\u5217\u65f6\u5bb9\u6613\u51fa\u73b0<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u8ba1\u7b97\u6548\u7387&#xff1a;One-hot\u7f16\u7801\u5bfc\u81f4\u9ad8\u7ef4\u7a00\u758f\u95ee\u9898<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u6a21\u578b\u5bb9\u91cf&#xff1a;\u7b80\u5355RNN\u96be\u4ee5\u6355\u6349\u590d\u6742\u6a21\u5f0f<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>6.2. \u6539\u8fdb\u65b9\u6848<\/h3>\n<p><span style=\"color:#38d8f0\">1.\u00a0\u4f7f\u7528\u9ad8\u7ea7RNN\u53d8\u4f53<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">LSTM&#xff1a;\u901a\u8fc7\u95e8\u63a7\u673a\u5236\u89e3\u51b3\u957f\u4f9d\u8d56\u95ee\u9898<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">GRU&#xff1a;\u7b80\u5316\u7248LSTM&#xff0c;\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u53cc\u5411RNN&#xff1a;\u540c\u65f6\u8003\u8651\u524d\u540e\u6587\u4fe1\u606f<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#38d8f0\">2.\u00a0\u00a0\u8bcd\u5d4c\u5165\u6280\u672f<\/span><\/p>\n<p>\u7528\u8bcd\u5411\u91cf&#xff08;Word2Vec, GloVe, FastText&#xff09;\u4ee3\u66ffOne-hot\u7f16\u7801&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u964d\u4f4e\u7ef4\u5ea6<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u6355\u6349\u8bed\u4e49\u76f8\u4f3c\u6027<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u5904\u7406\u672a\u767b\u5f55\u8bcd<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#38d8f0\">3.\u00a0\u6ce8\u610f\u529b\u673a\u5236<\/span><\/p>\n<p>\u5f15\u5165\u6ce8\u610f\u529b\u673a\u5236\u4f7f\u6a21\u578b\u80fd\u591f\u5173\u6ce8\u8f93\u5165\u5e8f\u5217\u4e2d\u7684\u91cd\u8981\u90e8\u5206&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u63d0\u9ad8\u6a21\u578b\u89e3\u91ca\u6027<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u6539\u5584\u957f\u5e8f\u5217\u5904\u7406\u80fd\u529b<\/span><\/p>\n<\/li>\n<\/ul>\n<p><span style=\"color:#38d8f0\">4.\u00a0\u6570\u636e\u589e\u5f3a<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u540c\u4e49\u8bcd\u66ff\u6362<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u56de\u8bd1\u6280\u672f<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u968f\u673a\u63d2\u5165\/\u5220\u9664<\/span><\/p>\n<\/li>\n<\/ul>\n<h2>7. \u5b9e\u9645\u5e94\u7528\u5efa\u8bae<\/h2>\n<h3>7.1. \u90e8\u7f72\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u6027\u80fd\u4f18\u5316&#xff1a;\u8003\u8651\u4f7f\u7528\u6279\u91cf\u63a8\u7406\u548c\u6a21\u578b\u91cf\u5316<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u5b9e\u65f6\u6027\u8981\u6c42&#xff1a;\u4f18\u5316\u9884\u6d4b\u5ef6\u8fdf&#xff0c;\u652f\u6301\u9ad8\u5e76\u53d1<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u6a21\u578b\u66f4\u65b0&#xff1a;\u5efa\u7acb\u5b9a\u671f\u66f4\u65b0\u673a\u5236&#xff0c;\u9002\u5e94\u8bed\u8a00\u53d8\u5316<\/span><\/p>\n<\/li>\n<\/ul>\n<h3>7.2. \u9886\u57df\u9002\u5e94<\/h3>\n<p>\u5f53\u5c06\u6a21\u578b\u5e94\u7528\u4e8e\u7279\u5b9a\u9886\u57df\u65f6&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u9886\u57df\u8bcd\u5178&#xff1a;\u6784\u5efa\u9886\u57df\u4e13\u4e1a\u8bcd\u6c47\u8868<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u8fc1\u79fb\u5b66\u4e60&#xff1a;\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u8fdb\u884c\u5fae\u8c03<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u9886\u57df\u6570\u636e&#xff1a;\u6536\u96c6\u9886\u57df\u7279\u5b9a\u8bad\u7ec3\u6570\u636e<\/span><\/p>\n<\/li>\n<\/ul>\n<h2>8. \u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684RNN\u4e2d\u6587\u60c5\u611f\u5206\u6790\u5b9e\u73b0\u4ee3\u7801&#xff1a;<\/p>\n<p># \u5173\u952e\u70b9\u8bf4\u660e&#xff1a;<br \/>\n# \u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684RNN\u5b9e\u73b0&#xff0c;\u7528\u4e8e\u4e2d\u6587\u6587\u672c\u60c5\u611f\u5206\u6790&#xff08;\u6b63\u9762\/\u8d1f\u9762&#xff09;<br \/>\n# \u6570\u636e\u9884\u5904\u7406\u5305\u62ec&#xff1a;\u4e2d\u6587\u5206\u8bcd\u3001\u6784\u5efa\u8bcd\u6c47\u8868\u3001\u5e8f\u5217\u5316\u548cone-hot\u7f16\u7801<br \/>\n# RNN\u7684\u6838\u5fc3\u662f\u524d\u5411\u4f20\u64ad\u90e8\u5206&#xff0c;\u8ba1\u7b97\u9690\u85cf\u72b6\u6001\u548c\u8f93\u51fa<br \/>\n# \u6700\u540e\u5c55\u793a\u4e86\u5982\u4f55\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u9884\u6d4b\u65b0\u6587\u672c\u7684\u60c5\u611f\u503e\u5411<\/p>\n<p># \u8fd9\u4e2a\u4ee3\u7801\u4e3b\u8981\u76ee\u7684\u662f\u5c55\u793aRNN\u7684\u57fa\u672c\u539f\u7406\u548c\u5de5\u4f5c\u6d41\u7a0b&#xff0c;\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981&#xff1a;<br \/>\n# \u5b9e\u73b0\u5b8c\u6574\u7684\u8bad\u7ec3\u8fc7\u7a0b<br \/>\n# \u4f7f\u7528\u66f4\u590d\u6742\u7684RNN\u53d8\u4f53&#xff08;\u5982LSTM\u3001GRU&#xff09;<br \/>\n# \u4f7f\u7528\u8bcd\u5d4c\u5165\u4ee3\u66ffone-hot\u7f16\u7801<br \/>\n# \u589e\u52a0\u66f4\u591a\u7684\u8bad\u7ec3\u6570\u636e<\/p>\n<p># \u5bfc\u5165\u6570\u503c\u8ba1\u7b97\u5e93<br \/>\nimport numpy as np<br \/>\n# \u5bfc\u5165\u6570\u636e\u5904\u7406\u5e93<br \/>\nimport pandas as pd<br \/>\n# \u5bfc\u5165\u4e2d\u6587\u5206\u8bcd\u5e93<br \/>\nimport jieba as jb<br \/>\n# \u5bfc\u5165\u6570\u636e\u96c6\u5212\u5206\u5de5\u5177<br \/>\nfrom sklearn.model_selection import train_test_split<br \/>\n# \u5bfc\u5165\u6a21\u578b\u8bc4\u4f30\u5de5\u5177&#xff0c;\u7528\u4e8e\u8ba1\u7b97\u6a21\u578b\u7684\u51c6\u786e\u7387<br \/>\nfrom sklearn.metrics import accuracy_score<br \/>\n# \u5bfc\u5165\u8b66\u544a\u8fc7\u6ee4\u5de5\u5177<br \/>\nimport warnings<br \/>\n# \u5ffd\u7565\u8b66\u544a\u4fe1\u606f<br \/>\nwarnings.filterwarnings(&#034;ignore&#034;)<\/p>\n<p># \u5b9e\u73b0\u7b80\u5355\u7684RNN\u6a21\u578b<br \/>\nclass SimpleRNN:<\/p>\n<p>    &#034;&#034;&#034;\u7b80\u5355RNN\u6a21\u578b\u521d\u59cb\u5316&#034;&#034;&#034;<br \/>\n    def __init__(self, input_size, hidden_size, output_size, learning_rate&#061;0.01):<br \/>\n      # \u53c2\u6570\u8bf4\u660e&#xff1a;<br \/>\n      # input_size: \u8f93\u5165\u7279\u5f81\u5927\u5c0f<br \/>\n      # hidden_size: \u9690\u85cf\u5c42\u5927\u5c0f<br \/>\n      # output_size: \u8f93\u51fa\u5c42\u5927\u5c0f<br \/>\n      # learning_rate: \u5b66\u4e60\u7387&#xff0c;\u9ed8\u8ba4\u503c\u4e3a0.01<br \/>\n      self.input_size &#061; input_size<br \/>\n      self.hidden_size &#061; hidden_size<br \/>\n      self.output_size &#061; output_size<br \/>\n      self.learning_rate &#061; learning_rate<\/p>\n<p>      # \u521d\u59cb\u5316\u8f93\u5165\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635<br \/>\n      self.W_xh &#061; np.random.randn(input_size, hidden_size) * 0.01<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635<br \/>\n      self.W_hh &#061; np.random.randn(hidden_size, hidden_size) * 0.01<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u5230\u8f93\u51fa\u5c42\u7684\u6743\u91cd\u77e9\u9635<br \/>\n      self.W_hy &#061; np.random.randn(hidden_size, output_size) * 0.01<\/p>\n<p>      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u504f\u7f6e\u9879<br \/>\n      self.b_h &#061; np.zeros((1, hidden_size))<br \/>\n      # \u521d\u59cb\u5316\u8f93\u51fa\u5c42\u504f\u7f6e\u9879<br \/>\n      self.b_y &#061; np.zeros((1, output_size))<\/p>\n<p>    &#034;&#034;&#034;Tanh\u6fc0\u6d3b\u51fd\u6570&#034;&#034;&#034;<br \/>\n    def tanh(self, x):<br \/>\n      return np.tanh(x) # \u9690\u85cf\u5c42\u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>    &#034;&#034;&#034;Softmax\u6fc0\u6d3b\u51fd\u6570&#034;&#034;&#034;<br \/>\n    def softmax(self, x):<br \/>\n      exp_x &#061; np.exp(x &#8211; np.max(x, axis&#061;1, keepdims&#061;True)) # \u9632\u6b62\u6ea2\u51fa<br \/>\n      return exp_x \/ np.sum(exp_x, axis&#061;1, keepdims&#061;True) # \u8f93\u51fa\u5c42\u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>    &#034;&#034;&#034;\u524d\u5411\u4f20\u64ad&#034;&#034;&#034;<br \/>\n    def forward(self, X):<br \/>\n      # \u83b7\u53d6\u8f93\u5165\u5e8f\u5217\u957f\u5ea6\u3001\u6279\u6b21\u5927\u5c0f<br \/>\n      seq_len, batch_size, _ &#061; X.shape<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u72b6\u6001<br \/>\n      self.h &#061; np.zeros((seq_len &#043; 1, batch_size, self.hidden_size))<br \/>\n      # \u521d\u59cb\u5316\u8f93\u51fa\u5c42\u72b6\u6001<br \/>\n      self.y &#061; np.zeros((seq_len, batch_size, self.output_size))<\/p>\n<p>      for t in range(seq_len):<br \/>\n        # \u8ba1\u7b97\u9690\u85cf\u5c42\u72b6\u6001<br \/>\n        # h_t &#061; tanh(W_xh * x_t &#043; W_hh * h_t-1 &#043; b_h)<br \/>\n        self.h[t &#043; 1] &#061; self.tanh(np.dot(X[t], self.W_xh) &#043;<br \/>\n                                  np.dot(self.h[t], self.W_hh) &#043; self.b_h)<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u72b6\u6001<br \/>\n        # y_t &#061; softmax(W_hy * h_t &#043; b_y)<br \/>\n        self.y[t] &#061; self.softmax(np.dot(self.h[t &#043; 1], self.W_hy) &#043; self.b_y)<\/p>\n<p>      return self.y<\/p>\n<p>    &#034;&#034;&#034;\u53cd\u5411\u4f20\u64ad&#034;&#034;&#034;<br \/>\n    def backward(self, X, y):<br \/>\n      # \u83b7\u53d6\u8f93\u5165\u5e8f\u5217\u957f\u5ea6\u3001\u6279\u6b21\u5927\u5c0f<br \/>\n      seq_len, batch_size, _ &#061; X.shape<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u72b6\u6001\u68af\u5ea6<br \/>\n      self.dh &#061; np.zeros((seq_len &#043; 2, batch_size, self.hidden_size))  # \u589e\u52a0\u4e00\u4e2a\u4f4d\u7f6e\u7528\u4e8e\u6700\u540e\u4e00\u4e2a\u9690\u85cf\u72b6\u6001\u7684\u68af\u5ea6<br \/>\n      # \u521d\u59cb\u5316\u8f93\u51fa\u5c42\u72b6\u6001\u68af\u5ea6<br \/>\n      self.dy &#061; np.zeros((seq_len, batch_size, self.output_size))<br \/>\n      # \u521d\u59cb\u5316\u8f93\u5165\u5c42\u72b6\u6001\u68af\u5ea6<br \/>\n      self.dx &#061; np.zeros((seq_len, batch_size, self.input_size))<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u5230\u8f93\u51fa\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n      self.dW_hy &#061; np.zeros_like(self.W_hy)<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n      self.dW_hh &#061; np.zeros_like(self.W_hh)<br \/>\n      # \u521d\u59cb\u5316\u8f93\u5165\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n      self.dW_xh &#061; np.zeros_like(self.W_xh)<br \/>\n      # \u521d\u59cb\u5316\u9690\u85cf\u5c42\u504f\u7f6e\u9879\u68af\u5ea6<br \/>\n      self.db_h &#061; np.zeros_like(self.b_h)<br \/>\n      # \u521d\u59cb\u5316\u8f93\u51fa\u5c42\u504f\u7f6e\u9879\u68af\u5ea6<br \/>\n      self.db_y &#061; np.zeros_like(self.b_y)<\/p>\n<p>      # \u4ece\u6700\u540e\u4e00\u4e2a\u65f6\u95f4\u6b65\u5f00\u59cb\u53cd\u5411\u4f20\u64ad&#xff08;BPTT\u6838\u5fc3&#xff09;<br \/>\n      for t in reversed(range(seq_len)):<br \/>\n        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u72b6\u6001\u68af\u5ea6 (softmax&#043;\u4ea4\u53c9\u71b5\u7684\u8054\u5408\u68af\u5ea6)<br \/>\n        self.dy[t] &#061; self.y[t] &#8211; y[t]<\/p>\n<p>        # \u8ba1\u7b97\u9690\u85cf\u5c42\u5230\u8f93\u51fa\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n        self.dW_hy &#043;&#061; np.dot(self.h[t &#043; 1].T, self.dy[t])<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u51fa\u5c42\u504f\u7f6e\u9879\u68af\u5ea6<br \/>\n        self.db_y &#043;&#061; np.sum(self.dy[t], axis&#061;0, keepdims&#061;True)<\/p>\n<p>        # \u8ba1\u7b97\u5f53\u524d\u8f93\u51fa\u8bef\u5dee\u5f15\u8d77\u7684\u9690\u85cf\u5c42\u68af\u5ea6<br \/>\n        dh_from_output &#061; np.dot(self.dy[t], self.W_hy.T)<\/p>\n<p>        # \u8ba1\u7b97\u4e0b\u4e00\u65f6\u95f4\u6b65\u7684\u9690\u85cf\u5c42\u68af\u5ea6\u901a\u8fc7W_hh\u4f20\u9012\u7684\u90e8\u5206<br \/>\n        # \u6ce8\u610f&#xff1a;t&#043;1\u662f\u5f53\u524d\u9690\u85cf\u5c42\u7684\u7d22\u5f15&#xff0c;\u4e0b\u4e00\u4e2a\u9690\u85cf\u5c42\u662ft&#043;2<br \/>\n        dh_from_next &#061; np.dot(self.dh[t &#043; 2], self.W_hh.T) if (t &#043; 1) &lt; seq_len else 0<\/p>\n<p>        # \u5408\u5e76\u68af\u5ea6\u5e76\u5e94\u7528tanh\u7684\u5bfc\u6570<br \/>\n        self.dh[t &#043; 1] &#061; (dh_from_output &#043; dh_from_next) * (1 &#8211; self.h[t &#043; 1] ** 2)<\/p>\n<p>        # \u8ba1\u7b97\u9690\u85cf\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n        self.dW_hh &#043;&#061; np.dot(self.h[t].T, self.dh[t &#043; 1])<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u5165\u5c42\u5230\u9690\u85cf\u5c42\u7684\u6743\u91cd\u77e9\u9635\u68af\u5ea6<br \/>\n        self.dW_xh &#043;&#061; np.dot(X[t].T, self.dh[t &#043; 1])<\/p>\n<p>        # \u8ba1\u7b97\u9690\u85cf\u5c42\u504f\u7f6e\u9879\u68af\u5ea6<br \/>\n        self.db_h &#043;&#061; np.sum(self.dh[t &#043; 1], axis&#061;0, keepdims&#061;True)<\/p>\n<p>        # \u8ba1\u7b97\u8f93\u5165\u5c42\u72b6\u6001\u68af\u5ea6<br \/>\n        self.dx[t] &#061; np.dot(self.dh[t &#043; 1], self.W_xh.T)<\/p>\n<p>      return self.dx<\/p>\n<p>    &#034;&#034;&#034;\u6743\u91cd\u66f4\u65b0&#034;&#034;&#034;<br \/>\n    def update_weights(self):<br \/>\n      # \u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5\u66f4\u65b0\u6240\u6709\u6743\u91cd\u548c\u504f\u7f6e<br \/>\n      self.W_xh -&#061; self.learning_rate * self.dW_xh<br \/>\n      self.W_hh -&#061; self.learning_rate * self.dW_hh<br \/>\n      self.W_hy -&#061; self.learning_rate * self.dW_hy<br \/>\n      self.b_h -&#061; self.learning_rate * self.db_h<br \/>\n      self.b_y -&#061; self.learning_rate * self.db_y<\/p>\n<p>      # \u91cd\u7f6e\u68af\u5ea6&#xff08;\u53ef\u9009&#xff0c;\u907f\u514d\u68af\u5ea6\u7d2f\u79ef&#xff09;<br \/>\n      self.dW_xh *&#061; 0<br \/>\n      self.dW_hh *&#061; 0<br \/>\n      self.dW_hy *&#061; 0<br \/>\n      self.db_h *&#061; 0<br \/>\n      self.db_y *&#061; 0<\/p>\n<p>    &#034;&#034;&#034;\u6a21\u578b\u8bad\u7ec3&#034;&#034;&#034;<br \/>\n    def train(self, X, y, epochs&#061;100):<br \/>\n      # \u8bad\u7ec3\u6a21\u578b<br \/>\n      for epoch in range(epochs):<br \/>\n        # \u524d\u5411\u4f20\u64ad<br \/>\n        y_pred &#061; self.forward(X)<br \/>\n        # \u53cd\u5411\u4f20\u64ad<br \/>\n        self.backward(X, y)<br \/>\n        # \u66f4\u65b0\u6743\u91cd<br \/>\n        self.update_weights()<\/p>\n<p>        # \u8ba1\u7b97\u635f\u5931<br \/>\n        loss &#061; -np.mean(np.sum(y * np.log(y_pred &#043; 1e-8), axis&#061;1))<br \/>\n        # \u6253\u5370\u635f\u5931<br \/>\n        if epoch % 10 &#061;&#061; 0:<br \/>\n          print(f&#034;Epoch {epoch}, Loss: {loss:.4f}&#034;)<\/p>\n<p>    &#034;&#034;&#034;\u9884\u6d4b\u6a21\u578b&#034;&#034;&#034;<br \/>\n    def predict(self, X):<br \/>\n      y_pred &#061; self.forward(X)<br \/>\n      # \u53d6\u6700\u540e\u4e00\u4e2a\u65f6\u95f4\u6b65\u7684\u8f93\u51fa&#xff0c;\u5e76\u8fd4\u56de\u6982\u7387\u6700\u5927\u7684\u7c7b\u522b<br \/>\n      return np.argmax(y_pred[-1], axis&#061;1)<\/p>\n<p># \u6570\u636e\u5904\u7406\u51fd\u6570<br \/>\ndef create_dataset():<br \/>\n  &#034;&#034;&#034;\u521b\u5efa\u7b80\u5355\u7684\u60c5\u611f\u5206\u6790\u6570\u636e\u96c6&#034;&#034;&#034;<br \/>\n  # \u6269\u5c55\u8bad\u7ec3\u6570\u636e\u96c6<br \/>\n  data &#061; {<br \/>\n    &#039;text&#039;:[<br \/>\n        &#039;\u8fd9\u4e2a\u4ea7\u54c1\u5f88\u597d&#039;, &#039;\u8d28\u91cf\u592a\u5dee\u4e86&#039;, &#039;\u670d\u52a1\u4e0d\u9519&#039;, &#039;\u4ef7\u683c\u592a\u8d35&#039;,<br \/>\n        &#039;\u7269\u6d41\u5f88\u5feb&#039;, &#039;\u5305\u88c5\u7cbe\u7f8e&#039;, &#039;\u5ba2\u670d\u8010\u5fc3&#039;, &#039;\u5546\u54c1\u6709\u7455\u75b5&#039;,<br \/>\n        &#039;\u6027\u4ef7\u6bd4\u9ad8&#039;, &#039;\u4f7f\u7528\u4e0d\u65b9\u4fbf&#039;, &#039;\u989c\u8272\u5f88\u597d\u770b&#039;, &#039;\u5c3a\u5bf8\u4e0d\u5408\u9002&#039;,<br \/>\n        &#039;\u6750\u8d28\u4e0d\u9519&#039;, &#039;\u5473\u9053\u4e0d\u597d\u95fb&#039;, &#039;\u4ef7\u683c\u5408\u7406&#039;, &#039;\u7269\u6d41\u592a\u6162&#039;,<br \/>\n        &#039;\u5305\u88c5\u7834\u635f&#039;, &#039;\u5ba2\u670d\u6001\u5ea6\u597d&#039;, &#039;\u8fd9\u4e2a\u4e1c\u897f\u5f88\u597d\u7528&#039;, &#039;\u8d28\u91cf\u975e\u5e38\u5dee&#039;,<br \/>\n        &#039;\u670d\u52a1\u6001\u5ea6\u5f88\u597d&#039;, &#039;\u4ef7\u683c\u771f\u7684\u8d35&#039;, &#039;\u7269\u6d41\u901f\u5ea6\u5f88\u5feb&#039;, &#039;\u5305\u88c5\u5f88\u7528\u5fc3&#039;,<br \/>\n        &#039;\u5ba2\u670d\u975e\u5e38\u8010\u5fc3&#039;, &#039;\u5546\u54c1\u8d28\u91cf\u6709\u95ee\u9898&#039;, &#039;\u6027\u4ef7\u6bd4\u975e\u5e38\u9ad8&#039;, &#039;\u4f7f\u7528\u8d77\u6765\u4e0d\u65b9\u4fbf&#039;<br \/>\n    ],<br \/>\n    &#039;label&#039;:[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0]<br \/>\n  }<\/p>\n<p>  # \u8f6c\u6362\u4e3aDataFrame<br \/>\n  df &#061; pd.DataFrame(data)<br \/>\n  return df<\/p>\n<p># \u6587\u672c\u9884\u5904\u7406<br \/>\ndef preprocess_text(text):<br \/>\n  # \u5206\u8bcd<br \/>\n  seg_list &#061; jb.cut(text)<br \/>\n  # \u5408\u5e76\u5206\u8bcd\u7ed3\u679c<br \/>\n  return &#034; &#034;.join(seg_list)<\/p>\n<p># \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5e8f\u5217<br \/>\ndef text_to_sequence(text, vocab, max_len&#061;5):<br \/>\n  words &#061; text.split() # \u6309\u7a7a\u683c\u5206\u8bcd<br \/>\n  sequence &#061; [] # \u521d\u59cb\u5316\u5e8f\u5217<br \/>\n  for word in words[:max_len]: # \u53ea\u53d6\u524dmax_len\u4e2a\u5355\u8bcd<br \/>\n    if word in vocab:<br \/>\n      sequence.append(vocab[word]) # \u83b7\u53d6\u8bcd\u5bf9\u5e94\u7684ID<br \/>\n    else:<br \/>\n      sequence.append(0) # \u672a\u77e5\u8bcd\u75280\u8868\u793a<\/p>\n<p>  # \u586b\u5145\u5e8f\u5217\u5230\u56fa\u5b9a\u957f\u5ea6(\u4e0d\u8db3\u7684\u90e8\u5206\u75280\u586b\u5145)<br \/>\n  while len(sequence) &lt; max_len:<br \/>\n    sequence.append(0)<\/p>\n<p>  return sequence<\/p>\n<p># \u4e3b\u51fd\u6570<br \/>\ndef main():<br \/>\n  # 1.\u6570\u636e\u51c6\u5907<br \/>\n  print(&#034;&#061;&#061;&#061;\u6570\u636e\u51c6\u5907&#061;&#061;&#061;&#034;)<br \/>\n  df &#061; create_dataset()<br \/>\n  print(f&#034;\u6570\u636e\u96c6\u5927\u5c0f&#xff1a;{len(df)}&#034;)<\/p>\n<p>  # 2.\u6587\u672c\u9884\u5904\u7406<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u6587\u672c\u9884\u5904\u7406&#061;&#061;&#061;&#034;)<br \/>\n  df[&#039;processed&#039;] &#061; df[&#039;text&#039;].apply(preprocess_text)<\/p>\n<p>  # 3.\u6784\u5efa\u8bcd\u6c47\u8868<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u6784\u5efa\u8bcd\u6c47\u8868&#061;&#061;&#061;&#034;)<br \/>\n  all_words &#061; []<br \/>\n  for text in df[&#039;processed&#039;]:<br \/>\n    all_words.extend(text.split()) # \u6536\u96c6\u6240\u6709\u5206\u8bcd\u540e\u7684\u8bcd<\/p>\n<p>  # \u521b\u5efa\u8bcd\u6c47\u8868&#xff0c;&lt;PAD&gt;\u8868\u793a\u586b\u5145\u8bcd<br \/>\n  vocab &#061; {&#039;&lt;PAD&gt;&#039;: 0}<br \/>\n  for word in set(all_words): # \u53bb\u91cd\u5e76\u904d\u5386\u6240\u6709\u8bcd<br \/>\n    if word not in vocab: # \u5982\u679c\u8bcd\u4e0d\u5728\u8bcd\u6c47\u8868\u4e2d<br \/>\n      vocab[word] &#061; len(vocab) # \u52a0\u5165\u8bcd\u6c47\u8868<\/p>\n<p>  vocab_size &#061; len(vocab)<br \/>\n  print(f&#034;\u8bcd\u6c47\u8868\u5927\u5c0f&#xff1a;{vocab_size}&#034;)<\/p>\n<p>  # 4.\u8f6c\u6362\u4e3a\u5e8f\u5217<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u5e8f\u5217\u8f6c\u6362&#061;&#061;&#061;&#034;)<br \/>\n  max_len &#061; 5 # \u6700\u5927\u5e8f\u5217\u957f\u5ea6<br \/>\n  sequences &#061; [] # \u521d\u59cb\u5316\u5e8f\u5217\u5217\u8868<\/p>\n<p>  for text in df[&#039;processed&#039;]: # \u904d\u5386\u6240\u6709\u6587\u672c<br \/>\n    seq &#061; text_to_sequence(text, vocab, max_len) # \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5e8f\u5217<br \/>\n    sequences.append(seq) # \u52a0\u5165\u5e8f\u5217\u5217\u8868<\/p>\n<p>  # \u8f6c\u6362\u4e3a\u72ec\u70ed\u7f16\u7801<br \/>\n  X &#061; np.zeros((len(sequences), max_len, vocab_size))<br \/>\n  for i, seq in enumerate(sequences):<br \/>\n    for j, word_index in enumerate(seq):<br \/>\n      X[i, j, word_index] &#061; 1<\/p>\n<p>  # \u5c06\u6807\u7b7e\u8f6c\u6362\u4e3a\u72ec\u70ed\u7f16\u7801\u683c\u5f0f<br \/>\n  y &#061; df[&#039;label&#039;].values<br \/>\n  y_onehot &#061; np.zeros((len(y), 2))<br \/>\n  y_onehot[np.arange(len(y)), y] &#061; 1<\/p>\n<p>  # 5.\u6570\u636e\u96c6\u5206\u5272<br \/>\n  X_train, X_test, y_train, y_test &#061; train_test_split(<br \/>\n    X, y_onehot, test_size&#061;0.25, random_state&#061;42<br \/>\n  )<br \/>\n  # \u8c03\u6574\u7ef4\u5ea6\u4e3a(seq_len, batch_size, input_size)<br \/>\n  X_train &#061; X_train.transpose(1, 0, 2)<br \/>\n  X_test &#061; X_test.transpose(1, 0, 2)<\/p>\n<p>  # 6.\u8bad\u7ec3RNN\u6a21\u578b<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u8bad\u7ec3RNN\u6a21\u578b&#061;&#061;&#061;&#034;)<br \/>\n  hidden_size &#061; 32 # \u9690\u85cf\u5c42\u5927\u5c0f<br \/>\n  output_size &#061; 2 # \u8f93\u51fa\u5c42\u5927\u5c0f(\u4e8c\u5206\u7c7b)<\/p>\n<p>  model &#061; SimpleRNN(vocab_size, hidden_size, output_size, learning_rate&#061;0.01)<br \/>\n  model.train(X_train, y_train, epochs&#061;200)<\/p>\n<p>  # 7.\u6a21\u578b\u8bc4\u4f30<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u6a21\u578b\u8bc4\u4f30&#061;&#061;&#061;&#034;)<br \/>\n  y_pred &#061; model.predict(X_test)<br \/>\n  y_test_labels &#061; np.argmax(y_test, axis&#061;1)<br \/>\n  accuracy &#061; accuracy_score(y_test_labels, y_pred)<br \/>\n  print(f&#034;\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387&#xff1a;{accuracy:.4f}&#034;)<\/p>\n<p>  # 8.\u9884\u6d4b\u65b0\u6587\u672c<br \/>\n  print(&#034;\\\\n&#061;&#061;&#061;\u9884\u6d4b\u65b0\u6587\u672c&#061;&#061;&#061;&#034;)<\/p>\n<p>  new_texts &#061; [<br \/>\n    &#039;\u8fd9\u4e2a\u5546\u54c1\u5f88\u6709\u4ef7\u503c&#039;,<br \/>\n    &#039;\u670d\u52a1\u7cdf\u7cd5&#039;,<br \/>\n    &#039;\u7269\u6d41\u5f88\u53ca\u65f6&#039;,<br \/>\n    &#039;\u5305\u88c5\u5f88\u7cbe\u7f8e&#039;,<br \/>\n    &#039;\u5ba2\u670d\u5f88\u8010\u5fc3&#039;<br \/>\n  ]<\/p>\n<p>  for text in new_texts:<br \/>\n    processed &#061; preprocess_text(text) # \u9884\u5904\u7406<br \/>\n    seq &#061; text_to_sequence(processed, vocab, max_len) # \u8f6c\u6362\u4e3a\u5e8f\u5217<\/p>\n<p>    # \u8f6c\u6362\u4e3a\u72ec\u70ed\u7f16\u7801<br \/>\n    x &#061; np.zeros((max_len, 1, vocab_size))<br \/>\n    for j, word_id in enumerate(seq):<br \/>\n      x[j, 0, word_id] &#061; 1<\/p>\n<p>    # \u9884\u6d4b<br \/>\n    result &#061; model.predict(x)[0]<br \/>\n    sentiment &#061; &#034;\u6b63\u9762&#034; if result &#061;&#061; 1 else &#034;\u8d1f\u9762&#034;<br \/>\n    print(f&#034;\u6587\u672c: {text}&#034;)<br \/>\n    print(f&#034;\u9884\u6d4b: {sentiment}&#034;)<br \/>\n    print()<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n  main()<\/p>\n<h2>9. \u603b\u7ed3\u4e0e\u5c55\u671b<\/h2>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u5b8c\u6574\u5b9e\u8df5&#xff0c;\u6211\u4eec\u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8eRNN\u7684\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7cfb\u7edf\u3002\u8fd9\u4e2a\u7cfb\u7edf\u867d\u7136\u7b80\u5355&#xff0c;\u4f46\u6db5\u76d6\u4e86RNN\u7684\u6838\u5fc3\u6982\u5ff5\u3001\u6570\u636e\u9884\u5904\u7406\u3001\u6a21\u578b\u8bad\u7ec3\u548c\u8bc4\u4f30\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<p>\u672a\u6765\u53d1\u5c55\u65b9\u5411\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u6a21\u578b\u4f18\u5316&#xff1a;\u4f7f\u7528LSTM\u3001GRU\u7b49\u9ad8\u7ea7RNN\u53d8\u4f53<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u8bcd\u5d4c\u5165&#xff1a;\u91c7\u7528Word2Vec\u3001BERT\u7b49\u9884\u8bad\u7ec3\u8bcd\u5411\u91cf<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u591a\u4efb\u52a1\u5b66\u4e60&#xff1a;\u540c\u65f6\u8fdb\u884c\u60c5\u611f\u5206\u6790\u548c\u4e3b\u9898\u5206\u7c7b<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u5b9e\u65f6\u7cfb\u7edf&#xff1a;\u6784\u5efa\u4f4e\u5ef6\u8fdf\u7684\u5728\u7ebf\u60c5\u611f\u5206\u6790\u670d\u52a1<\/span><\/p>\n<\/li>\n<\/ul>\n<p>RNN\u4f5c\u4e3a\u5e8f\u5217\u5efa\u6a21\u7684\u57fa\u7840\u67b6\u6784&#xff0c;\u867d\u7136\u5728\u67d0\u4e9b\u4efb\u52a1\u4e0a\u5df2\u88abTransformer\u7b49\u65b0\u67b6\u6784\u8d85\u8d8a&#xff0c;\u4f46\u5176\u6838\u5fc3\u601d\u60f3\u4ecd\u7136\u6df1\u523b\u5f71\u54cd\u4e86\u73b0\u4ee3\u6df1\u5ea6\u5b66\u4e60\u7684\u53d1\u5c55\u3002\u7406\u89e3RNN\u7684\u5de5\u4f5c\u539f\u7406&#xff0c;\u5bf9\u4e8e\u638c\u63e1\u66f4\u590d\u6742\u7684\u5e8f\u5217\u6a21\u578b\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5f15\u8a00&#xff1a;\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u5728\u60c5\u611f\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4ef7\u503c\u5728\u5f53\u4eca\u5927\u6570\u636e\u65f6\u4ee3&#xff0c;\u60c5\u611f\u5206\u6790\u5df2\u6210\u4e3a\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff08;NLP&#xff09;\u9886\u57df\u4e2d\u6700\u91cd\u8981\u7684\u5e94\u7528\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7535\u5546\u5e73\u53f0\u7684\u4ea7\u54c1\u8bc4\u8bba\u5206\u6790&#xff0c;\u8fd8\u662f\u793e\u4ea4\u5a92\u4f53\u7684\u8206\u60c5\u76d1\u63a7&#xff0c;\u51c6\u786e\u8bc6\u522b\u7528\u6237\u60c5\u611f\u503e\u5411\u90fd\u5177\u6709\u5de8\u5927\u5546\u4e1a\u4ef7\u503c\u3002\u7136\u800c&#xff0c;\u4f20\u7edf\u7684\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u5728\u5904\u7406\u5e8f\u5217\u6570\u636e\u65f6\u9762\u4e34\u7740\u5de8\u5927\u6311\u6218&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u5177\u6709\u4e0a\u4e0b\u6587\u4f9d\u8d56\u7684\u6587\u672c\u6570\u636e\u3002\u5faa\u73af\u795e\u7ecf\u7f51\u7edc&#xff08;RNN&#xff09;\u4f5c\u4e3a\u4e00\u79cd<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2068,81,50],"topic":[],"class_list":["post-65874","post","type-post","status-publish","format-standard","hentry","category-server","tag-nlp","tag-python","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \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\/65874.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. \u5f15\u8a00&#xff1a;\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u5728\u60c5\u611f\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4ef7\u503c\u5728\u5f53\u4eca\u5927\u6570\u636e\u65f6\u4ee3&#xff0c;\u60c5\u611f\u5206\u6790\u5df2\u6210\u4e3a\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff08;NLP&#xff09;\u9886\u57df\u4e2d\u6700\u91cd\u8981\u7684\u5e94\u7528\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7535\u5546\u5e73\u53f0\u7684\u4ea7\u54c1\u8bc4\u8bba\u5206\u6790&#xff0c;\u8fd8\u662f\u793e\u4ea4\u5a92\u4f53\u7684\u8206\u60c5\u76d1\u63a7&#xff0c;\u51c6\u786e\u8bc6\u522b\u7528\u6237\u60c5\u611f\u503e\u5411\u90fd\u5177\u6709\u5de8\u5927\u5546\u4e1a\u4ef7\u503c\u3002\u7136\u800c&#xff0c;\u4f20\u7edf\u7684\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u5728\u5904\u7406\u5e8f\u5217\u6570\u636e\u65f6\u9762\u4e34\u7740\u5de8\u5927\u6311\u6218&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u5177\u6709\u4e0a\u4e0b\u6587\u4f9d\u8d56\u7684\u6587\u672c\u6570\u636e\u3002\u5faa\u73af\u795e\u7ecf\u7f51\u7edc&#xff08;RNN&#xff09;\u4f5c\u4e3a\u4e00\u79cd\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/65874.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-25T12:24:56+00:00\" \/>\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=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65874.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/65874.html\",\"name\":\"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-25T12:24:56+00:00\",\"dateModified\":\"2026-01-25T12:24:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/65874.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/65874.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65874.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357\"}]},{\"@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":"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \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\/65874.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. \u5f15\u8a00&#xff1a;\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u5728\u60c5\u611f\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4ef7\u503c\u5728\u5f53\u4eca\u5927\u6570\u636e\u65f6\u4ee3&#xff0c;\u60c5\u611f\u5206\u6790\u5df2\u6210\u4e3a\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff08;NLP&#xff09;\u9886\u57df\u4e2d\u6700\u91cd\u8981\u7684\u5e94\u7528\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u7535\u5546\u5e73\u53f0\u7684\u4ea7\u54c1\u8bc4\u8bba\u5206\u6790&#xff0c;\u8fd8\u662f\u793e\u4ea4\u5a92\u4f53\u7684\u8206\u60c5\u76d1\u63a7&#xff0c;\u51c6\u786e\u8bc6\u522b\u7528\u6237\u60c5\u611f\u503e\u5411\u90fd\u5177\u6709\u5de8\u5927\u5546\u4e1a\u4ef7\u503c\u3002\u7136\u800c&#xff0c;\u4f20\u7edf\u7684\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u5728\u5904\u7406\u5e8f\u5217\u6570\u636e\u65f6\u9762\u4e34\u7740\u5de8\u5927\u6311\u6218&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u5177\u6709\u4e0a\u4e0b\u6587\u4f9d\u8d56\u7684\u6587\u672c\u6570\u636e\u3002\u5faa\u73af\u795e\u7ecf\u7f51\u7edc&#xff08;RNN&#xff09;\u4f5c\u4e3a\u4e00\u79cd","og_url":"https:\/\/www.wsisp.com\/helps\/65874.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-25T12:24:56+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/65874.html","url":"https:\/\/www.wsisp.com\/helps\/65874.html","name":"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-25T12:24:56+00:00","dateModified":"2026-01-25T12:24:56+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/65874.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/65874.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/65874.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff1a\u4e2d\u6587\u60c5\u611f\u5206\u6790\u7684\u5b8c\u6574\u5b9e\u8df5\u6307\u5357"}]},{"@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\/65874","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=65874"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/65874\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=65874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=65874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=65874"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=65874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}