{"id":547,"date":"2026-03-15T13:36:11","date_gmt":"2026-03-15T05:36:11","guid":{"rendered":"https:\/\/icalkzhangzihao.com\/?p=547"},"modified":"2026-03-15T13:36:12","modified_gmt":"2026-03-15T05:36:12","slug":"%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e8%af%be%e7%a8%8b%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/icalkzhangzihao.com\/?p=547","title":{"rendered":"\u6df1\u5ea6\u5b66\u4e60\u8bfe\u7a0b\u4ecb\u7ecd"},"content":{"rendered":"\n<p>\u8fd9\u6bb5\u4ee3\u7801\u9075\u5faa\u4e86\u6df1\u5ea6\u5b66\u4e60\u7684\u6807\u51c6\u6d41\u6c34\u7ebf\uff1a<strong>\u6570\u636e\u5904\u7406 -> \u6a21\u578b\u6784\u5efa -> \u8bad\u7ec3\u5faa\u73af -> \u6d4b\u8bd5\u8bc4\u4f30<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u6570\u636e\u9884\u5904\u7406\u6a21\u5757 (Data Pipeline)<\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transform = transforms.Compose(&#91;\n    transforms.ToTensor(), \n    transforms.Normalize((0.1307,), (0.3081,))\n])\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ToTensor()<\/code><\/strong>: \u8fd9\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\u3002\u5b83\u5c06 PIL \u56fe\u7247\u6216 Numpy \u6570\u7ec4\uff080-255\uff09\u8f6c\u6362\u4e3a <code>FloatTensor<\/code>\uff0c\u5e76\u5c06\u50cf\u7d20\u503c\u7f29\u653e\u5230 <code>[0.0, 1.0]<\/code> \u4e4b\u95f4\u3002\u540c\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5c06\u5f62\u72b6\u4ece <code>(H, W, C)<\/code> \u8f6c\u6362\u4e3a PyTorch \u8981\u6c42\u7684 <code>(C, H, W)<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>Normalize<\/code><\/strong>: \u4f7f\u7528 MNIST \u6570\u636e\u96c6\u7684\u5168\u5c40\u5747\u503c\uff080.1307\uff09\u548c\u6807\u51c6\u5dee\uff080.3081\uff09\u8fdb\u884c\u6807\u51c6\u5316\u3002\u8fd9\u80fd\u8ba9\u6a21\u578b\u6536\u655b\u66f4\u5feb\uff0c\u907f\u514d\u68af\u5ea6\u7206\u70b8\u6216\u6d88\u5931\u3002<\/li>\n\n\n\n<li><strong><code>DataLoader<\/code><\/strong>: \u5b83\u662f\u6570\u636e\u7684\u201c\u4f20\u9001\u5e26\u201d\u3002<code>batch_size=64<\/code> \u610f\u5473\u7740\u6bcf\u6b21\u7ed9\u6a21\u578b\u5582 64 \u5f20\u56fe\uff0c<code>shuffle=True<\/code> \u4fdd\u8bc1\u6a21\u578b\u4e0d\u4f1a\u56e0\u4e3a\u8bb0\u4f4f\u4e86\u56fe\u7247\u7684\u51fa\u73b0\u987a\u5e8f\u800c\u4ea7\u751f\u8fc7\u62df\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u6a21\u5757 (The Architecture)<\/h2>\n\n\n\n<p>\u8fd9\u4e00\u90e8\u5206\u5b9a\u4e49\u4e86\u5927\u8111\u7684\u201c\u7ed3\u6784\u201d\u3002<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TinyCNN(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.net = nn.Sequential(\n            nn.Conv2d(1, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),\n            # ... \u66f4\u591a\u5c42 ...\n            nn.Flatten(),\n            nn.Linear(32 * 7 * 7, 128), nn.ReLU(),\n            nn.Linear(128, 10)\n        )\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Conv2d<\/code> (\u5377\u79ef\u5c42)<\/strong>\uff1a\u8fd9\u662f\u63d0\u53d6\u7279\u5f81\u7684\u6838\u5fc3\u3002\u5b83\u50cf\u4e00\u4e2a\u6ee4\u955c\u5728\u56fe\u7247\u4e0a\u6ed1\u52a8\uff0c\u5bfb\u627e\u8fb9\u7f18\u3001\u5f27\u7ebf\u7b49\u5f62\u72b6\u3002<\/li>\n\n\n\n<li><strong><code>ReLU<\/code> (\u6fc0\u6d3b\u51fd\u6570)<\/strong>\uff1a\u5f15\u5165\u975e\u7ebf\u6027\u53d8\u6362\u3002\u6ca1\u6709\u5b83\uff0c\u65e0\u8bba\u591a\u5c11\u5c42\u795e\u7ecf\u7f51\u7edc\u90fd\u53ea\u662f\u4e00\u4e2a\u590d\u6742\u7684\u7ebf\u6027\u65b9\u7a0b\uff0c\u65e0\u6cd5\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u3002<\/li>\n\n\n\n<li><strong><code>MaxPool2d<\/code> (\u6c60\u5316\u5c42)<\/strong>\uff1a\u901a\u8fc7\u53d6 $2 \\times 2$ \u7a97\u53e3\u5185\u7684\u6700\u5927\u503c\uff0c\u5c06\u56fe\u7247\u5c3a\u5bf8\u51cf\u534a\uff0828->14->7\uff09\u3002\u8fd9\u80fd\u51cf\u5c11\u8ba1\u7b97\u91cf\u5e76\u63d0\u53d6\u6700\u91cd\u8981\u7684\u7279\u5f81\u3002<\/li>\n\n\n\n<li><strong><code>Flatten<\/code><\/strong>: \u5c06\u4e8c\u7ef4\u7684\u7279\u5f81\u56fe\uff08Feature Map\uff09\u644a\u5e73\u4e00\u7ef4\u5411\u91cf\uff0c\u4ee5\u4fbf\u8f93\u5165\u7ed9\u6700\u540e\u7684\u5168\u8fde\u63a5\u5c42\uff08Linear\uff09\u3002<\/li>\n\n\n\n<li><strong><code>Linear(128, 10)<\/code><\/strong>: \u6700\u540e\u4e00\u5c42\u8f93\u51fa 10 \u4e2a\u795e\u7ecf\u5143\uff0c\u5bf9\u5e94\u6570\u5b57 0-9 \u7684\u6982\u7387\u5f97\u5206\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8bad\u7ec3\u6838\u5fc3\u903b\u8f91 (The Training Loop)<\/h2>\n\n\n\n<p>\u8fd9\u662f\u6a21\u578b\u201c\u5b66\u4e60\u201d\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.train() # \u5207\u6362\u5230\u8bad\u7ec3\u6a21\u5f0f\nfor data, target in train_loader:\n    optimizer.zero_grad()            # 1. \u6e05\u7a7a\u65e7\u68af\u5ea6\n    output = model(data)             # 2. \u524d\u5411\u4f20\u64ad\uff1a\u5f97\u5230\u9884\u6d4b\u503c\n    loss = criterion(output, target) # 3. \u8ba1\u7b97\u8bef\u5dee\uff1a\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u5dee\u8ddd\n    loss.backward()                  # 4. \u53cd\u5411\u4f20\u64ad\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u53c2\u6570\u5bf9\u8bef\u5dee\u7684\u201c\u8d21\u732e\u201d\n    optimizer.step()                 # 5. \u66f4\u65b0\u53c2\u6570\uff1a\u6cbf\u7740\u8bef\u5dee\u51cf\u5c0f\u7684\u65b9\u5411\u632a\u4e00\u5c0f\u6b65\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>CrossEntropyLoss<\/code><\/strong>: \u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\u3002\u5bf9\u4e8e\u5206\u7c7b\u95ee\u9898\uff080-9 \u5206\u7c7b\uff09\uff0c\u8fd9\u662f\u6807\u51c6\u914d\u7f6e\u3002<\/li>\n\n\n\n<li><strong><code>Adam<\/code> \u4f18\u5316\u5668<\/strong>: \u4e00\u79cd\u81ea\u9002\u5e94\u5b66\u4e60\u7387\u7684\u4f18\u5316\u7b97\u6cd5\uff0c\u901a\u5e38\u6bd4\u4f20\u7edf\u7684 SGD\uff08\u968f\u673a\u68af\u5ea6\u4e0b\u964d\uff09\u8dd1\u5f97\u66f4\u7a33\u3001\u66f4\u5feb\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u6d4b\u8bd5\u4e0e\u8bc4\u4f30\u6a21\u5757 (Evaluation)<\/h2>\n\n\n\n<p>\u8fd9\u662f\u8003\u8bd5\u9636\u6bb5\uff0c\u9a8c\u8bc1\u6a21\u578b\u5728\u6ca1\u89c1\u8fc7\u7684\u6570\u636e\u4e0a\u7684\u8868\u73b0\u3002<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.eval() # \u5207\u6362\u5230\u9884\u6d4b\u6a21\u5f0f\nwith torch.no_grad(): # \u5173\u952e\uff1a\u9884\u6d4b\u65f6\u4e0d\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\uff0c\u8282\u7701\u5185\u5b58\u548c\u901f\u5ea6\n    for data, target in test_loader:\n        output = model(data)\n        # argmax(1) \u627e\u5230 10 \u4e2a\u8f93\u51fa\u4e2d\u5f97\u5206\u6700\u9ad8\u7684\u90a3\u4e00\u4e2a\u4f5c\u4e3a\u9884\u6d4b\u6570\u5b57\n        correct += (output.argmax(1) == target).sum().item()\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>argmax(1)<\/code><\/strong>: \u6a21\u578b\u8f93\u51fa\u7684\u662f 10 \u4e2a\u6570\uff08\u4f8b\u5982\u6570\u5b57 3 \u7684\u5f97\u5206\u6700\u9ad8\uff09\uff0c\u6211\u4eec\u53d6\u7d22\u5f15\u503c\u4f5c\u4e3a\u9884\u6d4b\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong><code>no_grad()<\/code><\/strong>: \u544a\u8bc9 PyTorch \u6b64\u65f6\u4e0d\u9700\u8981\u8bb0\u5f55\u8ba1\u7b97\u56fe\uff0c\u8fd9\u80fd\u8ba9\u6d4b\u8bd5\u8fc7\u7a0b\u8fd0\u884c\u5f97\u975e\u5e38\u4e1d\u6ed1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6bb5\u4ee3\u7801\u9075\u5faa\u4e86\u6df1\u5ea6\u5b66\u4e60\u7684\u6807\u51c6\u6d41\u6c34\u7ebf\uff1a\u6570\u636e\u5904\u7406 -> \u6a21\u578b\u6784\u5efa -> \u8bad\u7ec3\u5faa\u73af -> \u6d4b\u8bd5\u8bc4\u4f30\u3002 1. \u6570\u636e\u9884\u5904 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=547"}],"version-history":[{"count":3,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=\/wp\/v2\/posts\/547\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icalkzhangzihao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}