{"id":100057,"date":"2026-09-03T19:48:57","date_gmt":"2026-09-03T11:48:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/100057.html"},"modified":"2026-09-03T19:48:57","modified_gmt":"2026-09-03T11:48:57","slug":"%e4%bb%8e%e9%9b%b6%e7%90%86%e8%a7%a3-pytorch%ef%bc%9a%e7%94%a8%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e5%ae%8c%e6%88%90-mnist-%e6%89%8b%e5%86%99%e6%95%b0%e5%ad%97%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/100057.html","title":{"rendered":"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b"},"content":{"rendered":"<p>\u6700\u8fd1\u5b66\u4e60\u4e86\u4e00\u4e9b\u6df1\u5ea6\u5b66\u4e60\u548c PyTorch \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6&#xff0c;\u5e76\u5c1d\u8bd5\u4f7f\u7528 MNIST \u624b\u5199\u6570\u5b57\u6570\u636e\u96c6\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002\u867d\u7136\u624b\u5199\u6570\u5b57\u8bc6\u522b\u662f\u6df1\u5ea6\u5b66\u4e60\u5165\u95e8\u4e2d\u975e\u5e38\u7ecf\u5178\u7684\u6848\u4f8b&#xff0c;\u4f46\u5b83\u5305\u542b\u4e86\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;\u6570\u636e\u96c6\u52a0\u8f7d\u3001\u6a21\u578b\u6784\u5efa\u3001\u524d\u5411\u4f20\u64ad\u3001\u635f\u5931\u8ba1\u7b97\u3001\u53cd\u5411\u4f20\u64ad\u3001\u53c2\u6570\u66f4\u65b0\u4ee5\u53ca\u6a21\u578b\u6d4b\u8bd5\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee&#xff0c;\u6211\u9010\u6e10\u7406\u89e3\u4e86\u795e\u7ecf\u7f51\u7edc\u5e76\u4e0d\u662f\u4e00\u4e2a\u795e\u79d8\u7684\u201c\u9ed1\u76d2\u5b50\u201d&#xff0c;\u5b83\u672c\u8d28\u4e0a\u662f\u7531\u8bb8\u591a\u77e9\u9635\u8fd0\u7b97\u3001\u6fc0\u6d3b\u51fd\u6570\u548c\u53c2\u6570\u66f4\u65b0\u7ec4\u6210\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002\u53ea\u8981\u628a\u6bcf\u4e2a\u6b65\u9aa4\u62c6\u5f00\u6765\u770b&#xff0c;\u6574\u4e2a\u6a21\u578b\u5c31\u4f1a\u53d8\u5f97\u6e05\u6670\u5f88\u591a\u3002<\/p>\n<h3>\u4e00\u3001MNIST \u624b\u5199\u6570\u5b57\u6570\u636e\u96c6<\/h3>\n<p>MNIST \u662f\u4e00\u4e2a\u975e\u5e38\u7ecf\u5178\u7684\u624b\u5199\u6570\u5b57\u6570\u636e\u96c6&#xff0c;\u91cc\u9762\u5305\u542b\u5927\u91cf 0 \u5230 9 \u7684\u7070\u5ea6\u56fe\u50cf\u3002\u6bcf\u5f20\u56fe\u7247\u7684\u5927\u5c0f\u90fd\u662f 28\u00d728 \u50cf\u7d20&#xff0c;\u56fe\u7247\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\u90fd\u53ef\u4ee5\u7528\u4e00\u4e2a\u6570\u503c\u8868\u793a\u3002<\/p>\n<p>\u5728\u8bad\u7ec3\u6a21\u578b\u4e4b\u524d&#xff0c;\u9996\u5148\u9700\u8981\u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6570\u636e\u96c6\u3002PyTorch \u7684 torchvision \u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u6570\u636e\u96c6\u63a5\u53e3&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 datasets.MNIST \u52a0\u8f7d\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002<\/p>\n<p>from torchvision import datasets<br \/>\nfrom torchvision.transforms import ToTensor<\/p>\n<p>training_data &#061; datasets.MNIST(<br \/>\n    root&#061;&#034;data&#034;,<br \/>\n    train&#061;True,<br \/>\n    download&#061;True,<br \/>\n    transform&#061;ToTensor()<br \/>\n)<\/p>\n<p>test_data &#061; datasets.MNIST(<br \/>\n    root&#061;&#034;data&#034;,<br \/>\n    train&#061;False,<br \/>\n    download&#061;True,<br \/>\n    transform&#061;ToTensor()<br \/>\n)<\/p>\n<p>\u8fd9\u91cc\u7684 root \u8868\u793a\u6570\u636e\u4fdd\u5b58\u7684\u4f4d\u7f6e&#xff0c;train&#061;True \u8868\u793a\u52a0\u8f7d\u8bad\u7ec3\u96c6&#xff0c;train&#061;False \u8868\u793a\u52a0\u8f7d\u6d4b\u8bd5\u96c6&#xff0c;download&#061;True \u8868\u793a\u5982\u679c\u672c\u5730\u6ca1\u6709\u6570\u636e\u5c31\u81ea\u52a8\u4e0b\u8f7d&#xff0c;transform&#061;ToTensor() \u5219\u4f1a\u628a\u56fe\u7247\u8f6c\u6362\u4e3a PyTorch \u53ef\u4ee5\u5904\u7406\u7684\u5f20\u91cf\u3002<\/p>\n<p>\u56fe\u7247\u539f\u672c\u662f\u4e8c\u7ef4\u7ed3\u6784&#xff0c;\u4f46\u5168\u8fde\u63a5\u795e\u7ecf\u7f51\u7edc\u901a\u5e38\u9700\u8981\u4e00\u7ef4\u5411\u91cf\u4f5c\u4e3a\u8f93\u5165\u3002\u56e0\u6b64&#xff0c;\u4e00\u5f20 28\u00d728 \u7684\u56fe\u7247\u6700\u7ec8\u4f1a\u88ab\u8f6c\u6362\u6210\u957f\u5ea6\u4e3a 784 \u7684\u5411\u91cf\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u4e00\u5f20\u56fe\u7247\u53ef\u4ee5\u8868\u793a\u4e3a&#xff1a;<\/p>\n<p>28 \u00d7 28 &#061; 784<\/p>\n<p>\u8fd9 784 \u4e2a\u6570\u5b57\u5c31\u662f\u56fe\u7247\u7684\u50cf\u7d20\u7279\u5f81\u3002\u6a21\u578b\u8981\u505a\u7684&#xff0c;\u5c31\u662f\u6839\u636e\u8fd9\u4e9b\u50cf\u7d20\u503c\u5224\u65ad\u56fe\u7247\u5bf9\u5e94\u7684\u662f\u54ea\u4e2a\u6570\u5b57\u3002<\/p>\n<h3>\u4e8c\u3001DataLoader \u7684\u4f5c\u7528<\/h3>\n<p>\u6570\u636e\u96c6\u51c6\u5907\u597d\u4e4b\u540e&#xff0c;\u8fd8\u9700\u8981\u4f7f\u7528 DataLoader \u6309\u7167\u6279\u6b21\u8bfb\u53d6\u6570\u636e\u3002\u8bad\u7ec3\u65f6\u901a\u5e38\u4e0d\u4f1a\u4e00\u6b21\u6027\u628a\u6240\u6709\u56fe\u7247\u90fd\u9001\u5165\u6a21\u578b&#xff0c;\u800c\u662f\u5c06\u6570\u636e\u5212\u5206\u6210\u591a\u4e2a batch\u3002<\/p>\n<p>from torch.utils.data import DataLoader<\/p>\n<p>batch_size &#061; 64<\/p>\n<p>train_dataloader &#061; DataLoader(<br \/>\n    training_data,<br \/>\n    batch_size&#061;batch_size<br \/>\n)<\/p>\n<p>test_dataloader &#061; DataLoader(<br \/>\n    test_data,<br \/>\n    batch_size&#061;batch_size<br \/>\n)<\/p>\n<p>\u8fd9\u91cc\u8bbe\u7f6e batch_size&#061;64&#xff0c;\u8868\u793a\u6a21\u578b\u6bcf\u6b21\u8bfb\u53d6 64 \u5f20\u56fe\u7247\u3002\u4f7f\u7528 batch \u8bad\u7ec3\u6709\u5f88\u591a\u597d\u5904\u3002<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u5982\u679c\u4e00\u6b21\u6027\u52a0\u8f7d\u5168\u90e8\u6570\u636e&#xff0c;\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u7b2c\u4e8c&#xff0c;\u4f7f\u7528\u5c0f\u6279\u91cf\u6570\u636e\u53ef\u4ee5\u63d0\u9ad8\u8bad\u7ec3\u6548\u7387\u3002\u7b2c\u4e09&#xff0c;\u6bcf\u4e2a batch \u4ea7\u751f\u7684\u68af\u5ea6\u5177\u6709\u4e00\u5b9a\u968f\u673a\u6027&#xff0c;\u6709\u52a9\u4e8e\u6a21\u578b\u8df3\u51fa\u4e00\u4e9b\u4e0d\u7406\u60f3\u7684\u72b6\u6001\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u8bad\u7ec3\u4e2d&#xff0c;\u6bcf\u4e00\u4e2a batch \u901a\u5e38\u5305\u542b\u4e24\u90e8\u5206\u5185\u5bb9&#xff1a;<\/p>\n<p>X, y<\/p>\n<p>\u5176\u4e2d X \u662f\u8f93\u5165\u56fe\u7247&#xff0c;y \u662f\u5bf9\u5e94\u7684\u771f\u5b9e\u6807\u7b7e\u3002\u4f8b\u5982&#xff0c;y \u53ef\u80fd\u662f\u6570\u5b57 3&#xff0c;\u8868\u793a\u8fd9\u5f20\u56fe\u7247\u771f\u5b9e\u4ee3\u8868\u6570\u5b57 3\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u67e5\u770b\u4e00\u4e2a batch \u7684\u5f62\u72b6&#xff1a;<\/p>\n<p>for X, y in train_dataloader:<br \/>\n    print(&#034;Shape of X:&#034;, X.shape)<br \/>\n    print(&#034;Shape of y:&#034;, y.shape)<br \/>\n    break<\/p>\n<p>\u5982\u679c batch size \u662f 64&#xff0c;\u90a3\u4e48\u8f93\u5165\u6570\u636e\u7684\u5f62\u72b6\u901a\u5e38\u662f&#xff1a;<\/p>\n<p>X: [64, 1, 28, 28]<br \/>\ny: [64]<\/p>\n<p>\u5176\u4e2d 64 \u8868\u793a\u56fe\u7247\u6570\u91cf&#xff0c;1 \u8868\u793a\u7070\u5ea6\u901a\u9053&#xff0c;28 \u548c 28 \u5206\u522b\u8868\u793a\u56fe\u7247\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u3002<\/p>\n<h3>\u4e09\u3001\u795e\u7ecf\u7f51\u7edc\u7684\u57fa\u672c\u7ed3\u6784<\/h3>\n<p>\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u901a\u5e38\u7531\u8f93\u5165\u5c42\u3001\u9690\u85cf\u5c42\u548c\u8f93\u51fa\u5c42\u7ec4\u6210\u3002<\/p>\n<p>\u5bf9\u4e8e MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b\u4efb\u52a1\u6765\u8bf4&#xff0c;\u8f93\u5165\u5c42\u63a5\u6536 784 \u4e2a\u50cf\u7d20\u503c&#xff0c;\u9690\u85cf\u5c42\u8d1f\u8d23\u63d0\u53d6\u548c\u7ec4\u5408\u7279\u5f81&#xff0c;\u8f93\u51fa\u5c42\u8f93\u51fa 10 \u4e2a\u7c7b\u522b\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u6a21\u578b\u7ed3\u6784\u53ef\u4ee5\u7b80\u5355\u8868\u793a\u4e3a&#xff1a;<\/p>\n<p>\u8f93\u5165\u5c42&#xff1a;784 \u4e2a\u8282\u70b9<br \/>\n\u9690\u85cf\u5c42&#xff1a;128 \u4e2a\u8282\u70b9<br \/>\n\u9690\u85cf\u5c42&#xff1a;256 \u4e2a\u8282\u70b9<br \/>\n\u8f93\u51fa\u5c42&#xff1a;10 \u4e2a\u8282\u70b9<\/p>\n<p>\u8fd9\u91cc\u7684 10 \u4e2a\u8f93\u51fa\u8282\u70b9\u5206\u522b\u5bf9\u5e94\u6570\u5b57 0 \u5230 9\u3002\u6a21\u578b\u8f93\u51fa\u7684\u4e0d\u662f\u76f4\u63a5\u7684\u6570\u5b57&#xff0c;\u800c\u662f\u6bcf\u4e2a\u7c7b\u522b\u5bf9\u5e94\u7684\u5206\u6570\u3002\u5206\u6570\u6700\u9ad8\u7684\u90a3\u4e2a\u4f4d\u7f6e&#xff0c;\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u6a21\u578b\u6700\u7ec8\u7684\u9884\u6d4b\u7ed3\u679c\u3002<\/p>\n<p>\u5728 PyTorch \u4e2d&#xff0c;\u81ea\u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u4e00\u822c\u9700\u8981\u7ee7\u627f nn.Module\u3002<\/p>\n<p>import torch<br \/>\nfrom torch import nn<\/p>\n<p>class NeuralNetwork(nn.Module):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<\/p>\n<p>        self.flatten &#061; nn.Flatten()<br \/>\n        self.hidden1 &#061; nn.Linear(28 * 28, 128)<br \/>\n        self.hidden2 &#061; nn.Linear(128, 256)<br \/>\n        self.output &#061; nn.Linear(256, 10)<\/p>\n<p>    def forward(self, x):<br \/>\n        x &#061; self.flatten(x)<br \/>\n        x &#061; self.hidden1(x)<br \/>\n        x &#061; torch.relu(x)<br \/>\n        x &#061; self.hidden2(x)<br \/>\n        x &#061; torch.relu(x)<br \/>\n        x &#061; self.output(x)<br \/>\n        return x<\/p>\n<p>nn.Flatten() \u7684\u4f5c\u7528\u662f\u5c06\u8f93\u5165\u56fe\u7247\u5c55\u5f00\u3002\u8f93\u5165\u5f62\u72b6\u4ece [batch_size, 1, 28, 28] \u53d8\u6210 [batch_size, 784]\u3002<\/p>\n<p>nn.Linear \u8868\u793a\u5168\u8fde\u63a5\u5c42\u3002\u7b2c\u4e00\u5c42\u628a 784 \u4e2a\u8f93\u5165\u7279\u5f81\u6620\u5c04\u5230 128 \u4e2a\u795e\u7ecf\u5143&#xff0c;\u7b2c\u4e8c\u5c42\u628a 128 \u4e2a\u7279\u5f81\u6620\u5c04\u5230 256 \u4e2a\u795e\u7ecf\u5143&#xff0c;\u6700\u540e\u4e00\u5c42\u628a 256 \u4e2a\u7279\u5f81\u6620\u5c04\u5230 10 \u4e2a\u8f93\u51fa\u7c7b\u522b\u3002<\/p>\n<h3>\u56db\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u6fc0\u6d3b\u51fd\u6570&#xff1f;<\/h3>\n<p>\u5982\u679c\u795e\u7ecf\u7f51\u7edc\u7684\u6bcf\u4e00\u5c42\u90fd\u53ea\u662f\u7ebf\u6027\u53d8\u6362&#xff0c;\u90a3\u4e48\u5373\u4f7f\u5806\u53e0\u5f88\u591a\u5c42&#xff0c;\u6700\u7ec8\u5f97\u5230\u7684\u4ecd\u7136\u53ea\u662f\u4e00\u4e2a\u7ebf\u6027\u51fd\u6570\u3002\u8fd9\u6837\u7684\u6a21\u578b\u8868\u8fbe\u80fd\u529b\u975e\u5e38\u6709\u9650&#xff0c;\u65e0\u6cd5\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u7279\u5f81\u3002<\/p>\n<p>\u6fc0\u6d3b\u51fd\u6570\u7684\u4f5c\u7528&#xff0c;\u5c31\u662f\u5728\u7f51\u7edc\u4e2d\u52a0\u5165\u975e\u7ebf\u6027\u80fd\u529b&#xff0c;\u8ba9\u6a21\u578b\u80fd\u591f\u5b66\u4e60\u66f4\u52a0\u590d\u6742\u7684\u89c4\u5f8b\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u6fc0\u6d3b\u51fd\u6570\u5305\u62ec Sigmoid\u3001Tanh\u3001ReLU \u548c Leaky ReLU\u3002<\/p>\n<h4>1. Sigmoid \u51fd\u6570<\/h4>\n<p>Sigmoid \u7684\u8f93\u51fa\u8303\u56f4\u662f 0 \u5230 1&#xff0c;\u8868\u8fbe\u5f0f\u4e3a&#xff1a;<\/p>\n<p>\u03c3(x) &#061; 1 \/ (1 &#043; e^(-x))<\/p>\n<p>\u5b83\u7684\u66f2\u7ebf\u5448\u73b0\u51fa\u7c7b\u4f3c\u201cS\u201d\u5f62\u3002\u5f53\u8f93\u5165\u503c\u7279\u522b\u5927\u6216\u7279\u522b\u5c0f\u65f6&#xff0c;\u51fd\u6570\u4f1a\u9010\u6e10\u8d8b\u4e8e\u9971\u548c&#xff0c;\u5bfc\u6570\u63a5\u8fd1 0\u3002<\/p>\n<p>\u8fd9\u4f1a\u5e26\u6765\u4e00\u4e2a\u95ee\u9898&#xff1a;\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d&#xff0c;\u591a\u4e2a\u63a5\u8fd1 0 \u7684\u5bfc\u6570\u8fde\u7eed\u76f8\u4e58\u540e&#xff0c;\u6700\u7ec8\u7684\u68af\u5ea6\u4f1a\u53d8\u5f97\u975e\u5e38\u5c0f&#xff0c;\u8fd9\u5c31\u662f\u68af\u5ea6\u6d88\u5931\u3002<\/p>\n<h4>2. Tanh \u51fd\u6570<\/h4>\n<p>Tanh \u7684\u8f93\u51fa\u8303\u56f4\u662f -1 \u5230 1&#xff0c;\u4e14\u4ee5 0 \u4e3a\u4e2d\u5fc3\u3002\u5b83\u6bd4 Sigmoid \u66f4\u9002\u5408\u67d0\u4e9b\u9700\u8981\u96f6\u4e2d\u5fc3\u8f93\u51fa\u7684\u573a\u666f&#xff0c;\u4f46\u5f53\u8f93\u5165\u503c\u7edd\u5bf9\u503c\u8f83\u5927\u65f6&#xff0c;\u540c\u6837\u4f1a\u51fa\u73b0\u68af\u5ea6\u53d8\u5c0f\u7684\u95ee\u9898\u3002<\/p>\n<h4>3. ReLU \u51fd\u6570<\/h4>\n<p>ReLU \u7684\u8868\u8fbe\u5f0f\u975e\u5e38\u7b80\u5355&#xff1a;<\/p>\n<p>f(x) &#061; max(0, x)<\/p>\n<p>\u5f53\u8f93\u5165\u5927\u4e8e 0 \u65f6&#xff0c;\u8f93\u51fa\u7b49\u4e8e\u8f93\u5165&#xff1b;\u5f53\u8f93\u5165\u5c0f\u4e8e\u7b49\u4e8e 0 \u65f6&#xff0c;\u8f93\u51fa\u4e3a 0\u3002<\/p>\n<p>ReLU \u5728\u6b63\u6570\u533a\u57df\u7684\u5bfc\u6570\u4e3a 1&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u6709\u6548\u7f13\u89e3\u68af\u5ea6\u6d88\u5931\u95ee\u9898&#xff0c;\u800c\u4e14\u8ba1\u7b97\u901f\u5ea6\u975e\u5e38\u5feb&#xff0c;\u662f\u76ee\u524d\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u4e2d\u6700\u5e38\u7528\u7684\u6fc0\u6d3b\u51fd\u6570\u4e4b\u4e00\u3002<\/p>\n<p>\u4e0d\u8fc7&#xff0c;ReLU \u4e5f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\u3002\u5f53\u67d0\u4e2a\u795e\u7ecf\u5143\u957f\u671f\u63a5\u6536\u5230\u8d1f\u6570\u8f93\u5165\u65f6&#xff0c;\u5b83\u7684\u8f93\u51fa\u53ef\u80fd\u4e00\u76f4\u4e3a 0&#xff0c;\u68af\u5ea6\u4e5f\u4e00\u76f4\u4e3a 0&#xff0c;\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u201c\u795e\u7ecf\u5143\u6b7b\u4ea1\u201d\u3002<\/p>\n<h4>4. Leaky ReLU<\/h4>\n<p>Leaky ReLU \u5bf9 ReLU \u505a\u4e86\u6539\u8fdb\u3002\u5728\u8d1f\u6570\u533a\u57df\u4e0d\u518d\u5b8c\u5168\u8f93\u51fa 0&#xff0c;\u800c\u662f\u4fdd\u7559\u4e00\u4e2a\u5f88\u5c0f\u7684\u659c\u7387\u3002<\/p>\n<p>f(x) &#061; x&#xff0c;x &gt; 0<br \/>\nf(x) &#061; \u03b1x&#xff0c;x \u2264 0<\/p>\n<p>\u8fd9\u6837\u5373\u4f7f\u8f93\u5165\u4e3a\u8d1f\u6570&#xff0c;\u4e5f\u80fd\u4fdd\u7559\u4e00\u90e8\u5206\u68af\u5ea6&#xff0c;\u4ece\u800c\u964d\u4f4e\u795e\u7ecf\u5143\u6b7b\u4ea1\u7684\u6982\u7387\u3002<\/p>\n<h3>\u4e94\u3001\u68af\u5ea6\u6d88\u5931\u548c\u68af\u5ea6\u7206\u70b8<\/h3>\n<p>\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u6838\u5fc3\u662f\u53cd\u5411\u4f20\u64ad\u3002\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d&#xff0c;\u6a21\u578b\u4f1a\u6839\u636e\u94fe\u5f0f\u6cd5\u5219\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5bf9\u6bcf\u4e00\u4e2a\u53c2\u6570\u7684\u68af\u5ea6\u3002<\/p>\n<p>\u5047\u8bbe\u7f51\u7edc\u6709\u591a\u5c42\u7ed3\u6784&#xff0c;\u90a3\u4e48\u524d\u9762\u5c42\u7684\u68af\u5ea6\u901a\u5e38\u9700\u8981\u4e58\u4ee5\u540e\u9762\u591a\u5c42\u7684\u5bfc\u6570\u548c\u6743\u91cd\u3002\u5982\u679c\u8fd9\u4e9b\u4e58\u6570\u5927\u90e8\u5206\u90fd\u5c0f\u4e8e 1&#xff0c;\u8fde\u7eed\u76f8\u4e58\u540e\u68af\u5ea6\u5c31\u4f1a\u8d8a\u6765\u8d8a\u5c0f&#xff0c;\u6700\u7ec8\u5bfc\u81f4\u68af\u5ea6\u6d88\u5931\u3002<\/p>\n<p>\u68af\u5ea6\u6d88\u5931\u4f1a\u5e26\u6765\u4ee5\u4e0b\u95ee\u9898&#xff1a;<\/p>\n<li>\u524d\u9762\u5c42\u7684\u53c2\u6570\u51e0\u4e4e\u4e0d\u518d\u66f4\u65b0&#xff1b;<\/li>\n<li>\u6a21\u578b\u8bad\u7ec3\u901f\u5ea6\u53d8\u6162&#xff1b;<\/li>\n<li>\u6df1\u5c42\u7f51\u7edc\u96be\u4ee5\u5b66\u4e60\u6709\u6548\u7279\u5f81&#xff1b;<\/li>\n<li>\u635f\u5931\u51fd\u6570\u4e0b\u964d\u4e0d\u660e\u663e\u3002<\/li>\n<p>\u76f8\u53cd&#xff0c;\u5982\u679c\u8fde\u7eed\u76f8\u4e58\u7684\u56e0\u7d20\u5927\u90e8\u5206\u5927\u4e8e 1&#xff0c;\u68af\u5ea6\u5c31\u53ef\u80fd\u8d8a\u6765\u8d8a\u5927&#xff0c;\u6700\u7ec8\u51fa\u73b0\u68af\u5ea6\u7206\u70b8\u3002\u68af\u5ea6\u7206\u70b8\u4f1a\u5bfc\u81f4\u53c2\u6570\u6570\u503c\u8fc5\u901f\u53d8\u5927&#xff0c;\u635f\u5931\u51fd\u6570\u51fa\u73b0 NaN&#xff0c;\u8bad\u7ec3\u8fc7\u7a0b\u65e0\u6cd5\u7ee7\u7eed\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 ReLU \u7b49\u6fc0\u6d3b\u51fd\u6570\u3001\u5408\u7406\u521d\u59cb\u5316\u6743\u91cd\u3001\u4f7f\u7528 BatchNorm\u3001\u8c03\u6574\u5b66\u4e60\u7387\u4ee5\u53ca\u8fdb\u884c\u68af\u5ea6\u88c1\u526a\u7b49\u3002<\/p>\n<h3>\u516d\u3001\u635f\u5931\u51fd\u6570\u7684\u4f5c\u7528<\/h3>\n<p>\u6a21\u578b\u8f93\u51fa\u9884\u6d4b\u7ed3\u679c\u540e&#xff0c;\u8fd8\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u7ed3\u679c\u5230\u5e95\u597d\u4e0d\u597d\u3002\u635f\u5931\u51fd\u6570\u5c31\u662f\u7528\u6765\u8861\u91cf\u9884\u6d4b\u7ed3\u679c\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u5dee\u8ddd\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5bf9\u4e8e\u591a\u5206\u7c7b\u95ee\u9898&#xff0c;\u901a\u5e38\u4f7f\u7528\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570&#xff1a;<\/p>\n<p>loss_fn &#061; nn.CrossEntropyLoss()<\/p>\n<p>\u4ea4\u53c9\u71b5\u635f\u5931\u4f1a\u6bd4\u8f83\u6a21\u578b\u5bf9\u6bcf\u4e2a\u7c7b\u522b\u7684\u9884\u6d4b\u5206\u6570\u548c\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u6a21\u578b\u9884\u6d4b\u8d8a\u51c6\u786e&#xff0c;\u635f\u5931\u8d8a\u5c0f&#xff1b;\u6a21\u578b\u9884\u6d4b\u8d8a\u504f\u79bb\u771f\u5b9e\u6807\u7b7e&#xff0c;\u635f\u5931\u8d8a\u5927\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u4f7f\u7528 CrossEntropyLoss \u65f6&#xff0c;\u6a21\u578b\u6700\u540e\u4e00\u5c42\u901a\u5e38\u4e0d\u9700\u8981\u624b\u52a8\u6dfb\u52a0 Softmax\u3002\u56e0\u4e3a\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\u5185\u90e8\u5df2\u7ecf\u5b8c\u6210\u4e86\u76f8\u5173\u8ba1\u7b97\u3002<\/p>\n<h3>\u4e03\u3001\u4f18\u5316\u5668\u4e0e\u68af\u5ea6\u4e0b\u964d<\/h3>\n<p>\u635f\u5931\u51fd\u6570\u53ea\u80fd\u544a\u8bc9\u6211\u4eec\u6a21\u578b\u5f53\u524d\u8868\u73b0\u5982\u4f55&#xff0c;\u4f46\u4e0d\u80fd\u81ea\u52a8\u4fee\u6539\u6a21\u578b\u53c2\u6570\u3002\u4f18\u5316\u5668\u7684\u4f5c\u7528&#xff0c;\u5c31\u662f\u6839\u636e\u68af\u5ea6\u66f4\u65b0\u7f51\u7edc\u4e2d\u7684\u6743\u91cd\u548c\u504f\u7f6e\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u4f7f\u7528 SGD \u4f18\u5316\u5668&#xff1a;<\/p>\n<p>optimizer &#061; torch.optim.SGD(<br \/>\n    model.parameters(),<br \/>\n    lr&#061;1e-3<br \/>\n)<\/p>\n<p>\u8fd9\u91cc\u7684 lr \u662f\u5b66\u4e60\u7387\u3002\u5b83\u51b3\u5b9a\u4e86\u6bcf\u6b21\u53c2\u6570\u66f4\u65b0\u7684\u5e45\u5ea6\u3002<\/p>\n<p>\u53c2\u6570\u66f4\u65b0\u7684\u57fa\u672c\u516c\u5f0f\u4e3a&#xff1a;<\/p>\n<p>\u03b8 &#061; \u03b8 &#8211; \u03b7 \u00d7 \u2207L(\u03b8)<\/p>\n<p>\u5176\u4e2d&#xff0c;\u03b8 \u8868\u793a\u6a21\u578b\u53c2\u6570&#xff0c;\u03b7 \u8868\u793a\u5b66\u4e60\u7387&#xff0c;\u2207L(\u03b8) \u8868\u793a\u635f\u5931\u51fd\u6570\u5173\u4e8e\u53c2\u6570\u7684\u68af\u5ea6\u3002<\/p>\n<p>\u5982\u679c\u5b66\u4e60\u7387\u592a\u5927&#xff0c;\u6a21\u578b\u53ef\u80fd\u4f1a\u8d8a\u8fc7\u6700\u4f18\u70b9&#xff0c;\u751a\u81f3\u5bfc\u81f4\u635f\u5931\u8d8a\u6765\u8d8a\u5927\u3002\u5982\u679c\u5b66\u4e60\u7387\u592a\u5c0f&#xff0c;\u6a21\u578b\u867d\u7136\u80fd\u591f\u6162\u6162\u6536\u655b&#xff0c;\u4f46\u8bad\u7ec3\u65f6\u95f4\u4f1a\u53d8\u5f97\u5f88\u957f\u3002<\/p>\n<p>\u9664\u4e86 SGD&#xff0c;\u8fd8\u53ef\u4ee5\u4f7f\u7528 Adam&#xff1a;<\/p>\n<p>optimizer &#061; torch.optim.Adam(<br \/>\n    model.parameters(),<br \/>\n    lr&#061;1e-3<br \/>\n)<\/p>\n<p>Adam \u4f1a\u6839\u636e\u5386\u53f2\u68af\u5ea6\u81ea\u52a8\u8c03\u6574\u4e0d\u540c\u53c2\u6570\u7684\u5b66\u4e60\u6b65\u957f&#xff0c;\u901a\u5e38\u6536\u655b\u901f\u5ea6\u6bd4\u8f83\u5feb&#xff0c;\u9002\u5408\u5feb\u901f\u5b9e\u9a8c\u3002<\/p>\n<h3>\u516b\u3001\u8bad\u7ec3\u5faa\u73af<\/h3>\n<p>\u8bad\u7ec3\u5faa\u73af\u662f\u6574\u4e2a\u9879\u76ee\u4e2d\u6700\u6838\u5fc3\u7684\u90e8\u5206\u3002\u4e00\u4e2a\u5b8c\u6574\u7684\u8bad\u7ec3\u8fc7\u7a0b\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u4ece DataLoader \u4e2d\u8bfb\u53d6\u4e00\u4e2a batch&#xff1b;<\/li>\n<li>\u5c06\u6570\u636e\u4f20\u5165\u6a21\u578b&#xff1b;<\/li>\n<li>\u8ba1\u7b97\u9884\u6d4b\u7ed3\u679c&#xff1b;<\/li>\n<li>\u6839\u636e\u9884\u6d4b\u7ed3\u679c\u8ba1\u7b97\u635f\u5931&#xff1b;<\/li>\n<li>\u6e05\u7a7a\u4e4b\u524d\u7d2f\u79ef\u7684\u68af\u5ea6&#xff1b;<\/li>\n<li>\u8fdb\u884c\u53cd\u5411\u4f20\u64ad&#xff1b;<\/li>\n<li>\u4f7f\u7528\u4f18\u5316\u5668\u66f4\u65b0\u53c2\u6570\u3002<\/li>\n<p>\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>def train(dataloader, model, loss_fn, optimizer):<br \/>\n    model.train()<\/p>\n<p>    for X, y in dataloader:<br \/>\n        pred &#061; model(X)<br \/>\n        loss &#061; loss_fn(pred, y)<\/p>\n<p>        optimizer.zero_grad()<br \/>\n        loss.backward()<br \/>\n        optimizer.step()<\/p>\n<p>\u8fd9\u91cc\u7684 model.train() \u8868\u793a\u8fdb\u5165\u8bad\u7ec3\u6a21\u5f0f\u3002\u67d0\u4e9b\u7f51\u7edc\u5c42&#xff0c;\u4f8b\u5982 Dropout \u548c BatchNorm&#xff0c;\u5728\u8bad\u7ec3\u6a21\u5f0f\u548c\u6d4b\u8bd5\u6a21\u5f0f\u4e0b\u7684\u884c\u4e3a\u4e0d\u540c&#xff0c;\u6240\u4ee5\u8bad\u7ec3\u524d\u9700\u8981\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<p>optimizer.zero_grad() \u7528\u6765\u6e05\u9664\u4e0a\u4e00\u4e2a batch \u4fdd\u5b58\u7684\u68af\u5ea6\u3002PyTorch \u9ed8\u8ba4\u4f1a\u7d2f\u79ef\u68af\u5ea6&#xff0c;\u5982\u679c\u4e0d\u6e05\u96f6&#xff0c;\u68af\u5ea6\u5c31\u4f1a\u4e0d\u65ad\u53e0\u52a0&#xff0c;\u5f71\u54cd\u53c2\u6570\u66f4\u65b0\u3002<\/p>\n<p>loss.backward() \u4f1a\u81ea\u52a8\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5173\u4e8e\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\u3002<\/p>\n<p>optimizer.step() \u4f1a\u6839\u636e\u8ba1\u7b97\u51fa\u6765\u7684\u68af\u5ea6\u66f4\u65b0\u6a21\u578b\u53c2\u6570\u3002<\/p>\n<h3>\u4e5d\u3001\u5982\u4f55\u8bc4\u4f30\u6a21\u578b&#xff1f;<\/h3>\n<p>\u8bad\u7ec3\u5b8c\u6210\u540e&#xff0c;\u9700\u8981\u4f7f\u7528\u6d4b\u8bd5\u96c6\u8bc4\u4f30\u6a21\u578b\u7684\u51c6\u786e\u7387\u3002<\/p>\n<p>def test(dataloader, model, loss_fn):<br \/>\n    model.eval()<\/p>\n<p>    test_loss &#061; 0<br \/>\n    correct &#061; 0<\/p>\n<p>    with torch.no_grad():<br \/>\n        for X, y in dataloader:<br \/>\n            pred &#061; model(X)<br \/>\n            test_loss &#043;&#061; loss_fn(pred, y).item()<br \/>\n            correct &#043;&#061; (pred.argmax(1) &#061;&#061; y).type(torch.float).sum().item()<\/p>\n<p>    test_loss \/&#061; len(dataloader)<br \/>\n    accuracy &#061; correct \/ len(dataloader.dataset)<\/p>\n<p>    print(&#034;Test Error:&#034;)<br \/>\n    print(f&#034;Accuracy: {accuracy * 100:.2f}%&#034;)<br \/>\n    print(f&#034;Average loss: {test_loss:.4f}&#034;)<\/p>\n<p>model.eval() \u8868\u793a\u8fdb\u5165\u6d4b\u8bd5\u6a21\u5f0f\u3002<\/p>\n<p>torch.no_grad() \u8868\u793a\u6d4b\u8bd5\u65f6\u4e0d\u8ba1\u7b97\u68af\u5ea6\u3002\u56e0\u4e3a\u6d4b\u8bd5\u9636\u6bb5\u4e0d\u9700\u8981\u66f4\u65b0\u53c2\u6570&#xff0c;\u6240\u4ee5\u5173\u95ed\u68af\u5ea6\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u5347\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<p>pred.argmax(1) \u8868\u793a\u5728\u6bcf\u4e00\u884c\u8f93\u51fa\u5206\u6570\u4e2d\u627e\u5230\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u4f4d\u7f6e\u5c31\u662f\u6a21\u578b\u8ba4\u4e3a\u6700\u53ef\u80fd\u7684\u7c7b\u522b\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>\u6a21\u578b\u8f93\u51fa&#xff1a;[1.2, -0.3, 4.5, 0.8, &#8230;]<\/p>\n<p>\u5982\u679c\u6700\u5927\u503c 4.5 \u4f4d\u4e8e\u4e0b\u6807 2&#xff0c;\u90a3\u4e48\u6a21\u578b\u9884\u6d4b\u7684\u6570\u5b57\u5c31\u662f 2\u3002<\/p>\n<h3>\u5341\u3001\u5b8c\u6574\u8bad\u7ec3\u6d41\u7a0b<\/h3>\n<p>\u6a21\u578b\u3001\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\u51c6\u5907\u597d\u4e4b\u540e&#xff0c;\u5c31\u53ef\u4ee5\u5f00\u59cb\u8bad\u7ec3\u3002<\/p>\n<p>model &#061; NeuralNetwork()<br \/>\nloss_fn &#061; nn.CrossEntropyLoss()<br \/>\noptimizer &#061; torch.optim.Adam(<br \/>\n    model.parameters(),<br \/>\n    lr&#061;1e-3<br \/>\n)<\/p>\n<p>epochs &#061; 10<\/p>\n<p>for t in range(epochs):<br \/>\n    print(f&#034;Epoch {t &#043; 1}&#034;)<br \/>\n    train(train_dataloader, model, loss_fn, optimizer)<br \/>\n    test(test_dataloader, model, loss_fn)<\/p>\n<p>print(&#034;Done!&#034;)<\/p>\n<p>epochs \u8868\u793a\u6574\u4e2a\u8bad\u7ec3\u96c6\u88ab\u6a21\u578b\u91cd\u590d\u5b66\u4e60\u591a\u5c11\u904d\u3002\u8bad\u7ec3\u8f6e\u6570\u592a\u5c11&#xff0c;\u6a21\u578b\u53ef\u80fd\u8fd8\u6ca1\u6709\u5b66\u4f1a\u8db3\u591f\u7684\u7279\u5f81&#xff1b;\u8bad\u7ec3\u8f6e\u6570\u592a\u591a&#xff0c;\u53c8\u53ef\u80fd\u51fa\u73b0\u8fc7\u62df\u5408\u3002<\/p>\n<p>\u5728\u6bcf\u4e2a epoch \u7ed3\u675f\u540e&#xff0c;\u53ef\u4ee5\u89c2\u5bdf\u8bad\u7ec3\u635f\u5931\u548c\u6d4b\u8bd5\u51c6\u786e\u7387\u3002\u5982\u679c\u8bad\u7ec3\u635f\u5931\u4e0d\u65ad\u4e0b\u964d&#xff0c;\u8bf4\u660e\u6a21\u578b\u6b63\u5728\u5b66\u4e60\u3002\u5982\u679c\u8bad\u7ec3\u96c6\u51c6\u786e\u7387\u5f88\u9ad8&#xff0c;\u4f46\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387\u6ca1\u6709\u63d0\u5347&#xff0c;\u53ef\u80fd\u8bf4\u660e\u6a21\u578b\u5df2\u7ecf\u8fc7\u62df\u5408\u3002<\/p>\n<h3>\u5341\u4e00\u3001CPU \u548c GPU \u7684\u9009\u62e9<\/h3>\n<p>\u5982\u679c\u8ba1\u7b97\u673a\u4e2d\u6709 NVIDIA GPU&#xff0c;\u53ef\u4ee5\u4f7f\u7528 CUDA \u52a0\u901f\u8bad\u7ec3\u3002<\/p>\n<p>device &#061; (<br \/>\n    &#034;cuda&#034;<br \/>\n    if torch.cuda.is_available()<br \/>\n    else &#034;cpu&#034;<br \/>\n)<\/p>\n<p>model &#061; NeuralNetwork().to(device)<\/p>\n<p>\u8bad\u7ec3\u65f6&#xff0c;\u4e5f\u9700\u8981\u628a\u8f93\u5165\u6570\u636e\u548c\u6807\u7b7e\u79fb\u52a8\u5230\u540c\u4e00\u4e2a\u8bbe\u5907&#xff1a;<\/p>\n<p>X &#061; X.to(device)<br \/>\ny &#061; y.to(device)<\/p>\n<p>\u6a21\u578b\u548c\u6570\u636e\u5fc5\u987b\u4f4d\u4e8e\u540c\u4e00\u4e2a\u8bbe\u5907\u4e0a&#xff0c;\u5426\u5219\u4f1a\u51fa\u73b0\u8bbe\u5907\u4e0d\u5339\u914d\u7684\u9519\u8bef\u3002<\/p>\n<p>\u5728\u4f7f\u7528 GPU \u65f6&#xff0c;\u8bad\u7ec3\u901f\u5ea6\u901a\u5e38\u4f1a\u660e\u663e\u63d0\u5347&#xff0c;\u5c24\u5176\u662f\u5f53\u6a21\u578b\u7ed3\u6784\u8f83\u590d\u6742\u3001\u6570\u636e\u91cf\u8f83\u5927\u65f6\u3002\u4e0d\u8fc7&#xff0c;\u5bf9\u4e8e MNIST \u8fd9\u6837\u7684\u7b80\u5355\u4efb\u52a1&#xff0c;CPU \u4e5f\u53ef\u4ee5\u5b8c\u6210\u8bad\u7ec3\u3002<\/p>\n<h3>\u5341\u4e8c\u3001\u8bad\u7ec3\u4e2d\u5e38\u89c1\u7684\u95ee\u9898<\/h3>\n<h4>1. \u5fd8\u8bb0\u6e05\u96f6\u68af\u5ea6<\/h4>\n<p>\u5982\u679c\u4e0d\u8c03\u7528&#xff1a;<\/p>\n<p>optimizer.zero_grad()<\/p>\n<p>\u90a3\u4e48\u6bcf\u4e2a batch \u7684\u68af\u5ea6\u4f1a\u4e0d\u65ad\u7d2f\u79ef&#xff0c;\u5bfc\u81f4\u53c2\u6570\u66f4\u65b0\u5f02\u5e38\u3002<\/p>\n<h4>2. \u5fd8\u8bb0\u5207\u6362\u6a21\u578b\u6a21\u5f0f<\/h4>\n<p>\u8bad\u7ec3\u65f6\u9700\u8981&#xff1a;<\/p>\n<p>model.train()<\/p>\n<p>\u6d4b\u8bd5\u65f6\u9700\u8981&#xff1a;<\/p>\n<p>model.eval()<\/p>\n<p>\u5982\u679c\u5fd8\u8bb0\u5207\u6362&#xff0c;Dropout \u548c BatchNorm \u7b49\u5c42\u53ef\u80fd\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<h4>3. \u8f93\u51fa\u5c42\u7ef4\u5ea6\u9519\u8bef<\/h4>\n<p>MNIST \u6709 10 \u4e2a\u7c7b\u522b&#xff0c;\u56e0\u6b64\u8f93\u51fa\u5c42\u5fc5\u987b\u662f&#xff1a;<\/p>\n<p>nn.Linear(256, 10)<\/p>\n<p>\u5982\u679c\u8f93\u51fa\u7c7b\u522b\u6570\u91cf\u4e0d\u6b63\u786e&#xff0c;\u6a21\u578b\u5c31\u65e0\u6cd5\u4e0e\u6807\u7b7e\u5bf9\u5e94\u3002<\/p>\n<h4>4. \u8f93\u5165\u5f62\u72b6\u9519\u8bef<\/h4>\n<p>\u5168\u8fde\u63a5\u5c42\u9700\u8981\u4e8c\u7ef4\u8f93\u5165&#xff0c;\u901a\u5e38\u5f62\u72b6\u662f&#xff1a;<\/p>\n<p>[batch_size, 784]<\/p>\n<p>\u5982\u679c\u76f4\u63a5\u628a [batch_size, 1, 28, 28] \u8f93\u5165\u5230\u7ebf\u6027\u5c42&#xff0c;\u5c31\u4f1a\u51fa\u73b0\u7ef4\u5ea6\u4e0d\u5339\u914d\u3002\u56e0\u6b64\u9700\u8981\u4f7f\u7528 Flatten \u5c55\u5f00\u56fe\u7247\u3002<\/p>\n<h4>5. \u5b66\u4e60\u7387\u4e0d\u5408\u9002<\/h4>\n<p>\u5b66\u4e60\u7387\u592a\u5927\u65f6&#xff0c;\u635f\u5931\u53ef\u80fd\u4e0d\u4e0b\u964d&#xff1b;\u5b66\u4e60\u7387\u592a\u5c0f\u65f6&#xff0c;\u8bad\u7ec3\u8fc7\u7a0b\u4f1a\u975e\u5e38\u7f13\u6162\u3002\u9047\u5230\u8fd9\u79cd\u60c5\u51b5&#xff0c;\u53ef\u4ee5\u5c1d\u8bd5\u8c03\u6574\u5b66\u4e60\u7387&#xff0c;\u6216\u8005\u66f4\u6362\u4f18\u5316\u5668\u3002<\/p>\n<h3>\u5341\u4e09\u3001\u4ece\u5168\u8fde\u63a5\u7f51\u7edc\u5230\u5377\u79ef\u795e\u7ecf\u7f51\u7edc<\/h3>\n<p>\u867d\u7136\u5168\u8fde\u63a5\u7f51\u7edc\u53ef\u4ee5\u5b8c\u6210 MNIST \u8bc6\u522b&#xff0c;\u4f46\u5b83\u6ca1\u6709\u5145\u5206\u5229\u7528\u56fe\u50cf\u7684\u7a7a\u95f4\u7ed3\u6784\u3002\u56fe\u7247\u4e2d\u7684\u76f8\u90bb\u50cf\u7d20\u5f80\u5f80\u5177\u6709\u5f88\u5f3a\u7684\u5173\u8054\u6027&#xff0c;\u800c\u5168\u8fde\u63a5\u5c42\u4f1a\u628a\u6240\u6709\u50cf\u7d20\u770b\u6210\u76f8\u4e92\u72ec\u7acb\u7684\u7279\u5f81\u3002<\/p>\n<p>\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u901a\u8fc7\u5377\u79ef\u6838\u63d0\u53d6\u5c40\u90e8\u7279\u5f81&#xff0c;\u4f8b\u5982\u8fb9\u7f18\u3001\u7ebf\u6761\u548c\u7eb9\u7406&#xff0c;\u518d\u901a\u8fc7\u591a\u5c42\u5377\u79ef\u9010\u6e10\u7ec4\u5408\u6210\u66f4\u52a0\u590d\u6742\u7684\u7ed3\u6784\u3002<\/p>\n<p>\u5bf9\u4e8e\u624b\u5199\u6570\u5b57\u6765\u8bf4&#xff0c;\u6a21\u578b\u53ef\u80fd\u5148\u5b66\u4e60\u7b80\u5355\u7684\u6a2a\u7ebf\u548c\u7ad6\u7ebf&#xff0c;\u518d\u5b66\u4e60\u5706\u5f27\u3001\u4ea4\u53c9\u548c\u6570\u5b57\u6574\u4f53\u5f62\u72b6\u3002\u76f8\u6bd4\u5168\u8fde\u63a5\u7f51\u7edc&#xff0c;\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u901a\u5e38\u66f4\u9002\u5408\u56fe\u50cf\u4efb\u52a1&#xff0c;\u4e5f\u80fd\u51cf\u5c11\u53c2\u6570\u6570\u91cf\u3002<\/p>\n<p>\u540e\u7eed\u53ef\u4ee5\u5c06\u6a21\u578b\u6539\u6210\u5982\u4e0b\u7ed3\u6784&#xff1a;<\/p>\n<p>class CNN(nn.Module):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<\/p>\n<p>        self.network &#061; nn.Sequential(<br \/>\n            nn.Conv2d(1, 32, kernel_size&#061;3),<br \/>\n            nn.ReLU(),<br \/>\n            nn.MaxPool2d(2),<br \/>\n            nn.Conv2d(32, 64, kernel_size&#061;3),<br \/>\n            nn.ReLU(),<br \/>\n            nn.MaxPool2d(2),<br \/>\n            nn.Flatten(),<br \/>\n            nn.Linear(64 * 5 * 5, 10)<br \/>\n        )<\/p>\n<p>    def forward(self, x):<br \/>\n        return self.network(x)<\/p>\n<p>\u5377\u79ef\u7f51\u7edc\u7684\u7ed3\u6784\u867d\u7136\u6bd4\u5168\u8fde\u63a5\u7f51\u7edc\u590d\u6742&#xff0c;\u4f46\u6838\u5fc3\u8bad\u7ec3\u6d41\u7a0b\u5e76\u6ca1\u6709\u6539\u53d8&#xff0c;\u4ecd\u7136\u662f\u524d\u5411\u4f20\u64ad\u3001\u635f\u5931\u8ba1\u7b97\u3001\u53cd\u5411\u4f20\u64ad\u548c\u53c2\u6570\u66f4\u65b0\u3002<\/p>\n<h3>\u5341\u56db\u3001\u8fd9\u6b21\u5b66\u4e60\u7684\u6536\u83b7<\/h3>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u624b\u5199\u6570\u5b57\u8bc6\u522b\u9879\u76ee&#xff0c;\u6211\u5bf9\u6df1\u5ea6\u5b66\u4e60\u7684\u7406\u89e3\u4ece\u201c\u77e5\u9053\u4e00\u4e9b\u6982\u5ff5\u201d\u53d8\u6210\u4e86\u201c\u80fd\u591f\u628a\u6982\u5ff5\u5199\u6210\u4ee3\u7801\u201d\u3002<\/p>\n<p>\u4ee5\u524d\u770b\u5230\u795e\u7ecf\u7f51\u7edc\u7ed3\u6784\u56fe\u65f6&#xff0c;\u5e38\u5e38\u53ea\u77e5\u9053\u8f93\u5165\u5c42\u3001\u9690\u85cf\u5c42\u548c\u8f93\u51fa\u5c42\u7684\u540d\u79f0&#xff0c;\u5374\u4e0d\u6e05\u695a\u6bcf\u4e00\u5c42\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u3002\u5b9e\u9645\u7f16\u5199\u6a21\u578b\u540e&#xff0c;\u6211\u53d1\u73b0\u6bcf\u4e00\u5c42\u5176\u5b9e\u90fd\u5bf9\u5e94\u5177\u4f53\u7684\u5f20\u91cf\u53d8\u6362\u3002<\/p>\n<p>\u8f93\u5165\u56fe\u7247\u8fdb\u5165\u6a21\u578b\u540e&#xff0c;\u9996\u5148\u88ab\u5c55\u5e73\u4e3a\u5411\u91cf&#xff1b;\u7ebf\u6027\u5c42\u901a\u8fc7\u6743\u91cd\u77e9\u9635\u548c\u504f\u7f6e\u5b8c\u6210\u7279\u5f81\u53d8\u6362&#xff1b;\u6fc0\u6d3b\u51fd\u6570\u589e\u52a0\u975e\u7ebf\u6027\u80fd\u529b&#xff1b;\u8f93\u51fa\u5c42\u7ed9\u51fa\u5404\u4e2a\u7c7b\u522b\u7684\u5206\u6570&#xff1b;\u635f\u5931\u51fd\u6570\u8861\u91cf\u9884\u6d4b\u7ed3\u679c\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e4b\u95f4\u7684\u5dee\u5f02&#xff1b;\u53cd\u5411\u4f20\u64ad\u5219\u8ba1\u7b97\u6bcf\u4e2a\u53c2\u6570\u5e94\u8be5\u5982\u4f55\u8c03\u6574\u3002<\/p>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u6982\u62ec\u4e3a&#xff1a;<\/p>\n<p>\u6570\u636e\u8f93\u5165 \u2192 \u524d\u5411\u4f20\u64ad \u2192 \u8ba1\u7b97\u635f\u5931 \u2192 \u53cd\u5411\u4f20\u64ad \u2192 \u66f4\u65b0\u53c2\u6570<\/p>\n<p>\u6a21\u578b\u5c31\u662f\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\u4e0d\u65ad\u5b66\u4e60&#xff0c;\u9010\u6e10\u63d0\u9ad8\u9884\u6d4b\u51c6\u786e\u7387\u3002<\/p>\n<h3>\u5341\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>PyTorch \u4e3a\u6df1\u5ea6\u5b66\u4e60\u63d0\u4f9b\u4e86\u975e\u5e38\u7075\u6d3b\u548c\u65b9\u4fbf\u7684\u5f00\u53d1\u65b9\u5f0f\u3002\u901a\u8fc7 Dataset \u548c DataLoader&#xff0c;\u53ef\u4ee5\u5feb\u901f\u51c6\u5907\u8bad\u7ec3\u6570\u636e&#xff1b;\u901a\u8fc7\u7ee7\u627f nn.Module&#xff0c;\u53ef\u4ee5\u81ea\u7531\u8bbe\u8ba1\u7f51\u7edc\u7ed3\u6784&#xff1b;\u901a\u8fc7\u81ea\u52a8\u5fae\u5206\u673a\u5236&#xff0c;\u53ef\u4ee5\u81ea\u52a8\u5b8c\u6210\u68af\u5ea6\u8ba1\u7b97&#xff1b;\u901a\u8fc7\u4f18\u5316\u5668&#xff0c;\u53ef\u4ee5\u65b9\u4fbf\u5730\u66f4\u65b0\u6a21\u578b\u53c2\u6570\u3002<\/p>\n<p>MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b\u867d\u7136\u662f\u4e00\u4e2a\u5165\u95e8\u6848\u4f8b&#xff0c;\u4f46\u5b83\u5b8c\u6574\u5c55\u793a\u4e86\u6df1\u5ea6\u5b66\u4e60\u9879\u76ee\u7684\u57fa\u672c\u6d41\u7a0b\u3002\u7406\u89e3\u8fd9\u4e2a\u6848\u4f8b\u4e4b\u540e&#xff0c;\u518d\u53bb\u5b66\u4e60\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u3001\u76ee\u6807\u68c0\u6d4b\u3001\u56fe\u50cf\u5206\u5272\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff0c;\u5c31\u4f1a\u6709\u66f4\u52a0\u624e\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<p>\u8fd9\u6b21\u5b66\u4e60\u4e5f\u8ba9\u6211\u8ba4\u8bc6\u5230&#xff0c;\u6df1\u5ea6\u5b66\u4e60\u4e0d\u4ec5\u4ec5\u662f\u8c03\u7528\u51e0\u4e2a\u51fd\u6570&#xff0c;\u66f4\u91cd\u8981\u7684\u662f\u7406\u89e3\u6bcf\u4e00\u4e2a\u7ec4\u4ef6\u80cc\u540e\u7684\u539f\u7406\u3002\u53ea\u6709\u77e5\u9053\u6570\u636e\u5982\u4f55\u6d41\u52a8\u3001\u53c2\u6570\u5982\u4f55\u66f4\u65b0\u3001\u635f\u5931\u5982\u4f55\u53d8\u5316&#xff0c;\u624d\u80fd\u771f\u6b63\u7406\u89e3\u6a21\u578b\u4e3a\u4ec0\u4e48\u6709\u6548&#xff0c;\u4ee5\u53ca\u51fa\u73b0\u95ee\u9898\u65f6\u5e94\u8be5\u4ece\u54ea\u91cc\u6392\u67e5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u51c6\u5907\u7ee7\u7eed\u5b66\u4e60\u5377\u79ef\u795e\u7ecf\u7f51\u7edc&#xff0c;\u5e76\u5c1d\u8bd5\u52a0\u5165\u6570\u636e\u589e\u5f3a\u3001BatchNorm\u3001Dropout \u548c\u5b66\u4e60\u7387\u8c03\u5ea6\u7b49\u6280\u672f\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u9a8c\u548c\u8c03\u6574\u6a21\u578b\u7ed3\u6784&#xff0c;\u8ba9\u6a21\u578b\u4ece\u7b80\u5355\u7684\u6570\u5b57\u8bc6\u522b\u4efb\u52a1\u9010\u6b65\u8d70\u5411\u66f4\u52a0\u590d\u6742\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5b66\u4e60\u4e86\u4e00\u4e9b\u6df1\u5ea6\u5b66\u4e60\u548c PyTorch \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6&#xff0c;\u5e76\u5c1d\u8bd5\u4f7f\u7528 MNIST \u624b\u5199\u6570\u5b57\u6570\u636e\u96c6\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002\u867d\u7136\u624b\u5199\u6570\u5b57\u8bc6\u522b\u662f\u6df1\u5ea6\u5b66\u4e60\u5165\u95e8\u4e2d\u975e\u5e38\u7ecf\u5178\u7684\u6848\u4f8b&#xff0c;\u4f46\u5b83\u5305\u542b\u4e86\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;\u6570\u636e\u96c6\u52a0\u8f7d\u3001\u6a21\u578b\u6784\u5efa\u3001\u524d\u5411\u4f20\u64ad\u3001\u635f\u5931\u8ba1\u7b97\u3001\u53cd\u5411\u4f20\u64ad\u3001\u53c2\u6570\u66f4\u65b0\u4ee5\u53ca\u6a21\u578b\u6d4b\u8bd5\u3002\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee&#xff0c;\u6211\u9010\u6e10\u7406\u89e3\u4e86\u795e\u7ecf\u7f51\u7edc\u5e76\u4e0d\u662f\u4e00\u4e2a\u795e\u79d8\u7684\u201c\u9ed1\u76d2\u5b50\u201d&#xff0c;\u5b83\u672c\u8d28\u4e0a\u662f\u7531\u8bb8\u591a\u77e9\u9635\u8fd0\u7b97\u3001\u6fc0\u6d3b\u51fd\u6570\u548c\u53c2\u6570\u66f4\u65b0\u7ec4\u6210\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002\u53ea\u8981\u628a\u6bcf<\/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":[587,152,50],"topic":[],"class_list":["post-100057","post","type-post","status-publish","format-standard","hentry","category-server","tag-587","tag-pytorch","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\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \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\/100057.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6700\u8fd1\u5b66\u4e60\u4e86\u4e00\u4e9b\u6df1\u5ea6\u5b66\u4e60\u548c PyTorch \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6&#xff0c;\u5e76\u5c1d\u8bd5\u4f7f\u7528 MNIST \u624b\u5199\u6570\u5b57\u6570\u636e\u96c6\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002\u867d\u7136\u624b\u5199\u6570\u5b57\u8bc6\u522b\u662f\u6df1\u5ea6\u5b66\u4e60\u5165\u95e8\u4e2d\u975e\u5e38\u7ecf\u5178\u7684\u6848\u4f8b&#xff0c;\u4f46\u5b83\u5305\u542b\u4e86\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;\u6570\u636e\u96c6\u52a0\u8f7d\u3001\u6a21\u578b\u6784\u5efa\u3001\u524d\u5411\u4f20\u64ad\u3001\u635f\u5931\u8ba1\u7b97\u3001\u53cd\u5411\u4f20\u64ad\u3001\u53c2\u6570\u66f4\u65b0\u4ee5\u53ca\u6a21\u578b\u6d4b\u8bd5\u3002\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee&#xff0c;\u6211\u9010\u6e10\u7406\u89e3\u4e86\u795e\u7ecf\u7f51\u7edc\u5e76\u4e0d\u662f\u4e00\u4e2a\u795e\u79d8\u7684\u201c\u9ed1\u76d2\u5b50\u201d&#xff0c;\u5b83\u672c\u8d28\u4e0a\u662f\u7531\u8bb8\u591a\u77e9\u9635\u8fd0\u7b97\u3001\u6fc0\u6d3b\u51fd\u6570\u548c\u53c2\u6570\u66f4\u65b0\u7ec4\u6210\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002\u53ea\u8981\u628a\u6bcf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/100057.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T11:48:57+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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100057.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/100057.html\",\"name\":\"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-03T11:48:57+00:00\",\"dateModified\":\"2026-09-03T11:48:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/100057.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/100057.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100057.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b\"}]},{\"@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\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \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\/100057.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6700\u8fd1\u5b66\u4e60\u4e86\u4e00\u4e9b\u6df1\u5ea6\u5b66\u4e60\u548c PyTorch \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6&#xff0c;\u5e76\u5c1d\u8bd5\u4f7f\u7528 MNIST \u624b\u5199\u6570\u5b57\u6570\u636e\u96c6\u5b8c\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002\u867d\u7136\u624b\u5199\u6570\u5b57\u8bc6\u522b\u662f\u6df1\u5ea6\u5b66\u4e60\u5165\u95e8\u4e2d\u975e\u5e38\u7ecf\u5178\u7684\u6848\u4f8b&#xff0c;\u4f46\u5b83\u5305\u542b\u4e86\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;\u6570\u636e\u96c6\u52a0\u8f7d\u3001\u6a21\u578b\u6784\u5efa\u3001\u524d\u5411\u4f20\u64ad\u3001\u635f\u5931\u8ba1\u7b97\u3001\u53cd\u5411\u4f20\u64ad\u3001\u53c2\u6570\u66f4\u65b0\u4ee5\u53ca\u6a21\u578b\u6d4b\u8bd5\u3002\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee&#xff0c;\u6211\u9010\u6e10\u7406\u89e3\u4e86\u795e\u7ecf\u7f51\u7edc\u5e76\u4e0d\u662f\u4e00\u4e2a\u795e\u79d8\u7684\u201c\u9ed1\u76d2\u5b50\u201d&#xff0c;\u5b83\u672c\u8d28\u4e0a\u662f\u7531\u8bb8\u591a\u77e9\u9635\u8fd0\u7b97\u3001\u6fc0\u6d3b\u51fd\u6570\u548c\u53c2\u6570\u66f4\u65b0\u7ec4\u6210\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002\u53ea\u8981\u628a\u6bcf","og_url":"https:\/\/www.wsisp.com\/helps\/100057.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-03T11:48:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/100057.html","url":"https:\/\/www.wsisp.com\/helps\/100057.html","name":"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-03T11:48:57+00:00","dateModified":"2026-09-03T11:48:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/100057.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/100057.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/100057.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u7406\u89e3 PyTorch\uff1a\u7528\u795e\u7ecf\u7f51\u7edc\u5b8c\u6210 MNIST \u624b\u5199\u6570\u5b57\u8bc6\u522b"}]},{"@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\/100057","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=100057"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100057\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=100057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=100057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=100057"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=100057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}