{"id":2592,"date":"2025-01-20T21:55:55","date_gmt":"2025-01-20T13:55:55","guid":{"rendered":"https:\/\/www.gnn.club\/?p=2592"},"modified":"2025-03-12T15:07:09","modified_gmt":"2025-03-12T07:07:09","slug":"tutorial-01-%e5%b8%88%e5%be%92%e7%9b%b8%e6%8e%88%ef%bc%9a%e6%9c%89%e7%9b%91%e7%9d%a3%e5%ad%a6%e4%b9%a0%ef%bc%88supervised-learning%ef%bc%89","status":"publish","type":"post","link":"http:\/\/gnn.club\/?p=2592","title":{"rendered":"Tutorial 01 &#8211; \u5e08\u5f92\u76f8\u6388\uff1a\u6709\u76d1\u7763\u5b66\u4e60\uff08Supervised Learning\uff09"},"content":{"rendered":"<h1>Learning Methods of Deep Learning<\/h1>\n<hr \/>\n<p>create by Deepfinder<\/p>\n<h3><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/bubbles\/50\/000000\/checklist.png\" style=\"height:50px;display:inline\"> Agenda<\/h3>\n<hr \/>\n<ol>\n<li><strong>\u5e08\u5f92\u76f8\u6388\uff1a\u6709\u76d1\u7763\u5b66\u4e60\uff08Supervised Learning\uff09<\/strong><\/li>\n<li>\u89c1\u5fae\u77e5\u8457\uff1a\u65e0\u76d1\u7763\u5b66\u4e60\uff08Un-supervised Learning\uff09<\/li>\n<li>\u65e0\u5e08\u81ea\u901a\uff1a\u81ea\u76d1\u7763\u5b66\u4e60\uff08Self-supervised Learning\uff09<\/li>\n<li>\u4ee5\u70b9\u5e26\u9762\uff1a\u534a\u76d1\u7763\u5b66\u4e60\uff08Semi-supervised learning\uff09<\/li>\n<li>\u660e\u8fa8\u662f\u975e\uff1a\u5bf9\u6bd4\u5b66\u4e60\uff08Contrastive Learning\uff09<\/li>\n<li>\u4e3e\u4e00\u53cd\u4e09\uff1a\u8fc1\u79fb\u5b66\u4e60\uff08Transfer Learning\uff09<\/li>\n<li>\u9488\u950b\u76f8\u5bf9\uff1a\u5bf9\u6297\u5b66\u4e60\uff08Adversarial Learning\uff09<\/li>\n<li>\u4f17\u5fd7\u6210\u57ce\uff1a\u96c6\u6210\u5b66\u4e60(Ensemble Learning) <\/li>\n<li>\u6b8a\u9014\u540c\u5f52\uff1a\u8054\u90a6\u5b66\u4e60\uff08Federated Learning\uff09<\/li>\n<li>\u767e\u6298\u4e0d\u6320\uff1a\u5f3a\u5316\u5b66\u4e60\uff08Reinforcement Learning\uff09<\/li>\n<li>\u6c42\u77e5\u82e5\u6e34\uff1a\u4e3b\u52a8\u5b66\u4e60\uff08Active Learning\uff09<\/li>\n<li>\u4e07\u6cd5\u5f52\u5b97\uff1a\u5143\u5b66\u4e60\uff08Meta-Learning\uff09<\/li>\n<\/ol>\n<h2>Tutorial 01 - \u5e08\u5f92\u76f8\u6388\uff1a\u6709\u76d1\u7763\u5b66\u4e60\uff08Supervised Learning\uff09<\/h2>\n<h3><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/plasticine\/100\/000000\/mind-map.png\" style=\"height:50px;display:inline\"> The Perceptron<\/h3>\n<hr \/>\n<ul>\n<li>\n<p>\u7b2c\u4e00\u4e2a\u4e5f\u662f\u6700\u7b80\u5355\u7684\u7ebf\u6027\u6a21\u578b\u4e4b\u4e00\u3002<\/p>\n<\/li>\n<li>\n<p>\u57fa\u4e8e <em>\u7ebf\u6027\u9608\u503c\u5355\u5143<\/em> (LTU)\uff1a\u8f93\u5165\u548c\u8f93\u51fa\u662f\u6570\u5b57\uff0c\u6bcf\u4e2a\u8fde\u63a5\u90fd\u4e0e\u4e00\u4e2a\u6743\u91cd\u76f8\u5173\u8054\u3002<\/p>\n<\/li>\n<li>\n<p>LTU \u8ba1\u7b97\u5176\u8f93\u5165\u7684\u52a0\u6743\u548c\uff1a$z = w_1x_1 + w_2x_2 +....+w_nx_n = w^Tx$\uff0c\u7136\u540e\u5bf9\u8be5\u548c\u5e94\u7528 <strong>\u9636\u8dc3\u51fd\u6570<\/strong> \u5e76\u8f93\u51fa\u7ed3\u679c\uff1a$$ h_w(x) = step(z) = step(w^Tx) $$<\/p>\n<p align=\"center\">\n<img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120213020784.png\n\" style=\"height:400px\">\n<\/p>\n<\/li>\n<li>\n<p>Illustration:<\/p>\n<\/li>\n<li>\n<p><strong>Pseudocode<\/strong>:<\/p>\n<ul>\n<li><strong>Require<\/strong>: Learning rate $\\eta$<\/li>\n<li><strong>Require<\/strong>: Initial parameter $w$<\/li>\n<li><strong>While<\/strong> stopping criterion not met <strong>do<\/strong>\n<ul>\n<li>For $i=1,...,m$:\n<ul>\n<li>$ w_{t+1} \\leftarrow w_t +\\eta(y_i -sign(w_t^Tx_i))x_i $<\/li>\n<\/ul>\n<\/li>\n<li>$t \\leftarrow t + 1$<\/li>\n<\/ul>\n<\/li>\n<li><strong>end while<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/dusk\/64\/000000\/layers.png\" style=\"height:50px;display:inline\"> Multi-Layer Perceptron (MLP)<\/h3>\n<hr \/>\n<ul>\n<li>\n<p>MLP \u7531\u4e00\u4e2a\u8f93\u5165\u5c42\u3001\u4e00\u4e2a\u6216\u591a\u4e2a\u9690\u85cf\u5c42\u548c\u4e00\u4e2a\u6700\u7ec8\u8f93\u51fa\u5c42\u7ec4\u6210\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u9690\u85cf\u5c42\u7684\u6570\u91cf\u5927\u4e8e 2 \u65f6\uff0c\u7f51\u7edc\u901a\u5e38\u79f0\u4e3a\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc (DNN)\uff0c\u5c0f\u4e8e2\u6210\u4e3aMLP\uff08\u4e00\u822c\u60c5\u51b5\u4e0b\u7684\u4e00\u79cd\u4e60\u60ef\uff0c\u4e0d\u662f\u5b9a\u4e49\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120213139558.png\n\" style=\"height:300px\">\n<\/p>\n<p><strong>\u57fa\u4e8eMLP\u53ef\u4ee5\u884d\u751f\u51fa\u5404\u79cd\u5404\u6837\u7684\u6df1\u5ea6\u5b66\u4e60\u7b97\u6cd5\u4e0e\u6a21\u578b<\/strong><\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120213226356.png\n\" style=\"height:300px\">\n<\/p>\n<h3><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/dusk\/64\/000000\/popular-topic.png\" style=\"height:50px;display:inline\">  \u6df1\u5ea6\u5b66\u4e60\u7b97\u6cd5\u4e0e\u6a21\u578b\u7684\u8bad\u7ec3\u903b\u8f91<\/h3>\n<hr \/>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120213434957.png\n\" style=\"height:200px\">\n<\/p>\n<h2><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/dusk\/64\/000000\/lego-head.png\" style=\"height:50px;display:inline\"> Forward calculation<\/h2>\n<hr \/>\n<ul>\n<li>\n<p>\u5728 <em>\u524d\u5411\u4f20\u9012<\/em> \u4e2d\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u8bad\u7ec3\u5b9e\u4f8b\uff0c\u7b97\u6cd5\u5c06\u5176\u9988\u9001\u5230\u7f51\u7edc\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u8fde\u7eed\u5c42\u4e2d\u6bcf\u4e2a\u795e\u7ecf\u5143\u7684\u8f93\u51fa<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7f51\u7edc\u8fdb\u884c\u9884\u6d4b\u53ea\u662f\u8fdb\u884c\u524d\u5411\u4f20\u9012\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120214702414.gif\n\" style=\"height:500px\">\n<\/p>\n<p><a href=\"https:\/\/medium.com\/the-feynman-journal\/the-linear-and-nonlinear-nature-of-feedforward-84199eb3edea\">Image Source<\/a><\/p>\n<h3><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/plasticine\/100\/000000\/serial-tasks.png\" style=\"height:50px;display:inline\"> Backpropagation<\/h3>\n<hr \/>\n<p>\u53cd\u5411\u4f20\u64ad\u662f\u4e00\u79cd\u6709\u6548\u7684\u8ba1\u7b97\u68af\u5ea6\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u7f51\u7edc\u4e2d\u6bcf\u4e2a\u795e\u7ecf\u5143\u7684\u504f\u5bfc\u6570\u3002\u53cd\u5411\u4f20\u64ad\u901a\u8fc7\u5148\u6b63\u5411\u4f20\u64ad\u8ba1\u7b97\u7f51\u7edc\u7684\u8f93\u51fa\uff0c\u7136\u540e\u4ece\u8f93\u51fa\u5c42\u5230\u8f93\u5165\u5c42\u53cd\u5411\u4f20\u64ad\u8bef\u5dee\uff0c\u6700\u540e\u6839\u636e\u8bef\u5dee\u8ba1\u7b97\u6bcf\u4e2a\u795e\u7ecf\u5143\u7684\u504f\u5bfc\u6570\u3002\u53cd\u5411\u4f20\u64ad\u7b97\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u662f\u901a\u8fc7\u94fe\u5f0f\u6cd5\u5219\u5c06\u8bef\u5dee\u5411\u540e\u4f20\u9012\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u795e\u7ecf\u5143\u5bf9\u8bef\u5dee\u7684\u8d21\u732e\u3002<\/p>\n<p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>\u521d\u59cb\u5316\u7f51\u7edc\uff0c\u6784\u5efa\u4e00\u4e2a\u53ea\u6709\u4e00\u5c42\u7684\u795e\u7ecf\u7f51\u7edc<\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120215221798.png\n\" style=\"height:300px\">\n<\/p>\n<p>\uff081\uff09\u521d\u59cb\u5316\u7f51\u7edc\u53c2\u6570\uff1a<\/p>\n<p>\u5047\u8bbe\u795e\u7ecf\u7f51\u7edc\u7684\u8f93\u5165\u548c\u8f93\u51fa\u7684\u521d\u59cb\u5316\u4e3a: $x_1=0.5, x_2=1.0, y=0.8$ \u3002<\/p>\n<p>\u53c2\u6570\u7684\u521d\u59cb\u5316\u4e3a: $w_1=1.0, w_2=0.5, w_3=0.5, w_4=0.7, w_5=1.0, w_6=2.0$ \u3002<\/p>\n<p>\uff082\uff09\u524d\u5411\u8ba1\u7b97, \u5982\u4e0b\u56fe<\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120215250810.png\n\" style=\"height:300px\">\n<\/p>\n<p>\u540c\u7406, \u8ba1\u7b97 $h_2$ \u7b49\u4e8e 0.95 \u3002\u5c06 $h_1$ \u548c $h_2$ \u76f8\u4e58\u6c42\u548c\u5230\u524d\u5411\u4f20\u64ad\u7684\u8ba1\u7b97\u7ed3\u679c, \u5982\u4e0b\u56fe<\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120215324612.png\n\" style=\"height:300px\">\n<\/p>\n<p>$$<br \/>\n\\begin{aligned}<br \/>\ny^{\\prime} &amp; =w_5 \\cdot h_1^{(1)}+w_6 \\cdot h_2^{(1)} \\\\<br \/>\n&amp; =1.0 \\cdot 1.0+2.0 \\cdot 0.95 \\\\<br \/>\n&amp; =2.9<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n<p>\uff083\uff09\u8ba1\u7b97\u635f\u5931: \u6839\u636e\u6570\u636e\u771f\u5b9e\u503c $y=0.8$ \u548c\u5e73\u65b9\u5dee\u635f\u5931\u51fd\u6570\u6765\u8ba1\u7b97\u635f\u5931<\/p>\n<p>$$<br \/>\n\\begin{aligned}<br \/>\n\\delta &amp; =\\frac{1}{2}\\left(y-y^{\\prime}\\right)^2 \\\\<br \/>\n&amp; =0.5(0.8-2.9)^2 \\\\<br \/>\n&amp; =2.205<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n<p>\uff084\uff09\u8ba1\u7b97\u68af\u5ea6: \u6b64\u8fc7\u7a0b\u5b9e\u9645\u4e0a\u5c31\u662f\u8ba1\u7b97\u504f\u5fae\u5206\u7684\u8fc7\u7a0b, \u4ee5\u53c2\u6570 $w_5$ \u7684\u504f\u5fae\u5206\u8ba1\u7b97\u4e3a\u4f8b, \u5982\u4e0b\u56fe<\/p>\n<p align=\"center\">\n  <img decoding=\"async\" src=\"https:\/\/gnnclub-1311496010.cos.ap-beijing.myqcloud.com\/wp-content\/uploads\/2025\/01\/20250120215359860.png\n\" style=\"height:300px\">\n<\/p>\n<p>\u6839\u636e\u94fe\u5f0f\u6cd5\u5219:<br \/>\n$$<br \/>\n\\frac{\\partial \\delta}{\\partial w_5}=\\frac{\\partial \\delta}{\\partial y^{\\prime}} \\cdot \\frac{\\partial y^{\\prime}}{\\partial w_5}<br \/>\n$$<\/p>\n<p>\u5176\u4e2d:<br \/>\n$$<br \/>\n\\begin{aligned}<br \/>\n\\frac{\\partial \\delta}{\\partial y^{\\prime}} &amp; =2 \\cdot \\frac{1}{2} \\cdot\\left(y-y^{\\prime}\\right)(-1) \\\\<br \/>\n&amp; =y^{\\prime}-y \\\\<br \/>\n&amp; =2.9-0.8 \\\\<br \/>\n&amp; =2.1 \\\\<br \/>\ny^{\\prime} &amp; =w_5 \\cdot h_1^{(1)}+w_6 \\cdot h_2^{(1)} \\\\<br \/>\n\\frac{\\partial y^{\\prime}}{\\partial w_5} &amp; =h_1^{(1)}+0 \\\\<br \/>\n&amp; =1.0<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n<p>\u6240\u4ee5:<br \/>\n$$<br \/>\n\\frac{\\partial \\delta}{\\partial w_5}=\\frac{\\partial \\delta}{\\partial y^{\\prime}} \\cdot \\frac{\\partial y^{\\prime}}{\\partial w_5}=2.1 \\times 1.0=2.1<br \/>\n$$<\/p>\n<p>\u7c7b\u4f3c\u7684\uff0c\u5982\u679c\u4ee5\u53c2\u6570 $w_1$ \u4e3a\u4f8b\u5b50, \u5b83\u7684\u504f\u5fae\u5206\u8ba1\u7b97\u5c31\u4e5f\u7528\u5230\u94fe\u5f0f\u6cd5\u5219, \u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<p>$$<br \/>\n\\begin{gathered}<br \/>\n\\frac{\\partial \\delta}{\\partial w_1}=\\frac{\\partial \\delta}{\\partial y^{\\prime}} \\cdot \\frac{\\partial y^{\\prime}}{\\partial h_1^{(1)}} \\cdot \\frac{\\partial h_1^{(1)}}{\\partial w_1} \\\\<br \/>\ny^{\\prime}=w_5 \\cdot h_1^{(1)}+w_6 \\cdot h_2^{(1)} \\\\<br \/>\n\\frac{\\partial y^{\\prime}}{\\partial h_1^{(1)}}=w_5+0 \\\\<br \/>\n=1.0 \\\\<br \/>\nh_1^{(1)}=w_1 \\cdot x_1+w_2 \\cdot x_2 \\\\<br \/>\n\\frac{\\partial h_1^{(1)}}{\\partial w_1}=x_1+0 \\\\<br \/>\n\\frac{\\partial \\delta}{\\partial w_1}=\\frac{\\partial \\delta}{\\partial y^{\\prime}} \\cdot \\frac{\\partial y^{\\prime}}{\\partial h_1^{(1)}} \\cdot \\frac{\\partial h_1^{(1)}}{\\partial w_1}=2.1 \\times 1.0 \\times 0.5=1.05<br \/>\n\\end{gathered}<br \/>\n$$<\/p>\n<p>\uff085\uff09\u68af\u5ea6\u4e0b\u964d\u66f4\u65b0\u7f51\u7edc\u53c2\u6570\uff1a\u5047\u8bbe\u8fd9\u91cc\u7684\u8d85\u53c2\u6570 \u201c\u5b66\u4e60\u901f\u7387\u201d \u7684\u521d\u59cb\u503c\u4e3a 0.1 , \u6839\u636e\u68af\u5ea6\u4e0b\u964d\u7684\u66f4\u65b0\u516c\u5f0f, $w_1$ \u53c2\u6570\u7684\u66f4\u65b0\u8ba1\u7b97\u5982\u4e0b\u6240\u793a:<br \/>\n$$<br \/>\nw_1^{\\text {(update) }}=w_1-\\eta \\cdot \\frac{\\partial \\delta}{\\partial w_1}=1.0-0.1 \\times 1.05=0.895<br \/>\n$$<\/p>\n<p>\u540c\u7406, \u53ef\u4ee5\u8ba1\u7b97\u5f97\u5230\u5176\u4ed6\u7684\u66f4\u65b0\u540e\u7684\u53c2\u6570:<br \/>\n$$<br \/>\nw_1=0.895, w_2=0.895, w_3=0.29, w_4=0.28, w_5=0.79, w_6=1.8005<br \/>\n$$<\/p>\n<p>\u5230\u6b64\u4e3a\u6b62, \u6211\u4eec\u5c31\u5b8c\u6210\u4e86\u53c2\u6570\u8fed\u4ee3\u7684\u5168\u90e8\u8fc7\u7a0b\u3002\u53ef\u4ee5\u8ba1\u7b97\u4e00\u4e0b\u635f\u5931\u770b\u770b\u662f\u5426\u6709\u51cf\u5c0f, \u8ba1\u7b97\u5982\u4e0b:<br \/>\n$$<br \/>\n\\begin{aligned}<br \/>\n\\delta &amp; =\\frac{1}{2}\\left(y-y^{\\prime}\\right)^2 \\\\<br \/>\n&amp; =0.5(0.8-1.3478)^2 \\\\<br \/>\n&amp; =0.15<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n<p>\u6b64\u7ed3\u679c\u76f8\u6bd4\u8f83\u4e8e\u4e4b\u95f4\u8ba1\u7b97\u7684\u524d\u5411\u4f20\u64ad\u7684\u7ed3\u679c 2.205 , \u662f\u6709\u660e\u663e\u7684\u51cf\u5c0f\u7684\u3002<\/p>\n<pre><code class=\"language-python\">import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\n\n# 1. \u8d85\u53c2\u6570\u8bbe\u7f6e\nbatch_size = 64\nlearning_rate = 0.001\nnum_epochs = 2  # \u8fd9\u91cc\u4e3a\u4e86\u793a\u4f8b\uff0c\u8bad\u7ec3\u8f6e\u6570\u8bbe\u5f97\u8f83\u5c0f\uff0c\u53ef\u81ea\u884c\u589e\u5927\n\n# 2. \u6570\u636e\u52a0\u8f7d\u4e0e\u9884\u5904\u7406\n# MNIST \u662f 28x28 \u7684\u7070\u5ea6\u56fe\u50cf\uff0c\u8fd9\u91cc\u4f7f\u7528\u968f\u673a\u88c1\u526a\u3001\u5f52\u4e00\u5316\u7b49\u7b80\u5355\u53d8\u6362\ntransform = transforms.Compose([\n    transforms.ToTensor(),  # \u5c06 PIL Image \u6216 numpy.ndarray \u8f6c\u6362\u4e3a\u5f20\u91cf\n    transforms.Normalize((0.1307,), (0.3081,))  # \u4f7f\u7528 MNIST \u5b98\u65b9\u63a8\u8350\u5747\u503c\u548c\u65b9\u5dee\u8fdb\u884c\u5f52\u4e00\u5316\n])\n\ntrain_dataset = torchvision.datasets.MNIST(\n    root=&#039;.\/data&#039;,\n    train=True,\n    transform=transform,\n    download=True\n)\ntest_dataset = torchvision.datasets.MNIST(\n    root=&#039;.\/data&#039;,\n    train=False,\n    transform=transform,\n    download=True\n)\n\ntrain_loader = torch.utils.data.DataLoader(\n    dataset=train_dataset,\n    batch_size=batch_size,\n    shuffle=True\n)\ntest_loader = torch.utils.data.DataLoader(\n    dataset=test_dataset,\n    batch_size=batch_size,\n    shuffle=False\n)\n\n# 3. \u5b9a\u4e49\u7f51\u7edc\u7ed3\u6784\nclass SimpleNN(nn.Module):\n    def __init__(self):\n        super(SimpleNN, self).__init__()\n        # MNIST \u8f93\u5165\u56fe\u50cf\u5927\u5c0f 1x28x28\uff0c\u6211\u4eec\u5148\u5c06\u5176\u94fa\u5e73\u4e3a (batch_size, 784)\n        self.flatten = nn.Flatten()\n\n        self.fc1 = nn.Linear(28 * 28, 128)\n        self.relu1 = nn.ReLU()\n\n        self.fc2 = nn.Linear(128, 64)\n        self.relu2 = nn.ReLU()\n\n        self.fc3 = nn.Linear(64, 10)  # 10 \u7c7b\uff08\u6570\u5b57 0~9\uff09\n\n    def forward(self, x):\n        x = self.flatten(x)\n        x = self.fc1(x)\n        x = self.relu1(x)\n        x = self.fc2(x)\n        x = self.relu2(x)\n        x = self.fc3(x)\n        return x\n\n# 4. \u521d\u59cb\u5316\u6a21\u578b\u3001\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\ndevice = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)\nmodel = SimpleNN().to(device)\ncriterion = nn.CrossEntropyLoss()  # \u4ea4\u53c9\u71b5\u635f\u5931\u5e38\u7528\u4e8e\u5206\u7c7b\u4efb\u52a1\noptimizer = optim.Adam(model.parameters(), lr=learning_rate)\n\n# 5. \u8bad\u7ec3\u51fd\u6570\ndef train_one_epoch(model, dataloader, criterion, optimizer, device):\n    model.train()  # \u8bbe\u7f6e\u4e3a\u8bad\u7ec3\u6a21\u5f0f\n    running_loss = 0.0\n    correct = 0\n    total = 0\n\n    for images, labels in dataloader:\n        images, labels = images.to(device), labels.to(device)\n\n        # \u524d\u5411\u4f20\u64ad\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        # \u53cd\u5411\u4f20\u64ad\u548c\u4f18\u5316\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        # \u7edf\u8ba1\u635f\u5931\u548c\u51c6\u786e\u7387\n        running_loss += loss.item() * images.size(0)\n        _, predicted = torch.max(outputs, 1)\n        correct += (predicted == labels).sum().item()\n        total += labels.size(0)\n\n    epoch_loss = running_loss \/ total\n    epoch_acc = correct \/ total\n    return epoch_loss, epoch_acc\n\n# 6. \u6d4b\u8bd5\u51fd\u6570\ndef evaluate(model, dataloader, criterion, device):\n    model.eval()  # \u8bbe\u7f6e\u4e3a\u8bc4\u4f30\u6a21\u5f0f\n    running_loss = 0.0\n    correct = 0\n    total = 0\n\n    # \u8bc4\u4f30\u9636\u6bb5\u4e0d\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\n    with torch.no_grad():\n        for images, labels in dataloader:\n            images, labels = images.to(device), labels.to(device)\n\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n\n            running_loss += loss.item() * images.size(0)\n            _, predicted = torch.max(outputs, 1)\n            correct += (predicted == labels).sum().item()\n            total += labels.size(0)\n\n    epoch_loss = running_loss \/ total\n    epoch_acc = correct \/ total\n    return epoch_loss, epoch_acc\n\n# 7. \u8bad\u7ec3\u4e0e\u9a8c\u8bc1\nfor epoch in range(num_epochs):\n    train_loss, train_acc = train_one_epoch(model, train_loader, criterion, optimizer, device)\n    test_loss, test_acc = evaluate(model, test_loader, criterion, device)\n\n    print(f&quot;Epoch [{epoch+1}\/{num_epochs}] &quot;\n          f&quot;Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}, &quot;\n          f&quot;Test Loss: {test_loss:.4f}, Test Acc: {test_acc:.4f}&quot;)\n<\/code><\/pre>\n<pre><code>\/home\/arwin\/anaconda3\/envs\/dl\/lib\/python3.8\/site-packages\/tqdm\/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https:\/\/ipywidgets.readthedocs.io\/en\/stable\/user_install.html\n  from .autonotebook import tqdm as notebook_tqdm\n\nEpoch [1\/2] Train Loss: 0.2726, Train Acc: 0.9208, Test Loss: 0.1360, Test Acc: 0.9585\nEpoch [2\/2] Train Loss: 0.1135, Train Acc: 0.9654, Test Loss: 0.0978, Test Acc: 0.9690<\/code><\/pre>\n<h2><img decoding=\"async\" src=\"https:\/\/img.icons8.com\/dusk\/64\/000000\/prize.png\" style=\"height:50px;display:inline\"> Credits<\/h2>\n<hr \/>\n<ul>\n<li>Icons made by <a href=\"https:\/\/www.flaticon.com\/authors\/becris\" title=\"Becris\">Becris<\/a> from <a href=\"https:\/\/www.flaticon.com\/\" title=\"Flaticon\">www.flaticon.com<\/a><\/li>\n<li>Icons from <a href=\"https:\/\/icons8.com\/\">Icons8.com<\/a> - <a href=\"https:\/\/icons8.com\">https:\/\/icons8.com<\/a><\/li>\n<li>Datasets from <a href=\"https:\/\/www.kaggle.com\/\">Kaggle<\/a> - <a href=\"https:\/\/www.kaggle.com\/\">https:\/\/www.kaggle.com\/<\/a><\/li>\n<li><a href=\"https:\/\/machinelearningmastery.com\/why-initialize-a-neural-network-with-random-weights\/\">Jason Brownlee - Why Initialize a Neural Network with Random Weights?<\/a><\/li>\n<li><a href=\"https:\/\/openai.com\/blog\/deep-double-descent\/\">OpenAI - Deep Double Descent<\/a><\/li>\n<li><a href=\"https:\/\/taldatech.github.io\">Tal Daniel<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learning Methods of Deep Learning create by Deepfinder  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2597,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,28],"tags":[],"class_list":["post-2592","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-18","category-28"],"_links":{"self":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/2592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2592"}],"version-history":[{"count":7,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/2592\/revisions"}],"predecessor-version":[{"id":2678,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/posts\/2592\/revisions\/2678"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=\/wp\/v2\/media\/2597"}],"wp:attachment":[{"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2592"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/gnn.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}