{"id":190,"date":"2025-07-07T16:49:40","date_gmt":"2025-07-07T08:49:40","guid":{"rendered":"https:\/\/mianka.xyz\/?p=190"},"modified":"2025-07-07T16:54:42","modified_gmt":"2025-07-07T08:54:42","slug":"pytorch","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=190","title":{"rendered":"\u57fa\u4e8ePyTorch\u7684\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u5b9e\u6218\uff1a\u4ece\u6570\u636e\u9884\u5904\u7406\u5230\u6a21\u578b\u4f18\u5316"},"content":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e00\u3001\u6570\u636e\u51c6\u5907\u4e0e\u9884\u5904\u7406<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1. \u6570\u636e\u96c6\u9009\u62e9\u4e0e\u52a0\u8f7d<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4ee5CIFAR-10\u6570\u636e\u96c6\u4e3a\u4f8b\uff0c\u8be5\u6570\u636e\u96c6\u5305\u542b60,000\u5f2032\u00d732\u5f69\u8272\u56fe\u50cf\uff0c\u5206\u4e3a10\u4e2a\u7c7b\u522b\uff08\u98de\u673a\u3001\u6c7d\u8f66\u3001\u9e1f\u7b49\uff09\uff0c\u5176\u4e2d50,000\u5f20\u7528\u4e8e\u8bad\u7ec3\uff0c10,000\u5f20\u7528\u4e8e\u6d4b\u8bd5\u3002PyTorch\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">torchvision.datasets.CIFAR10<\/code>\u5b9e\u73b0\u4e00\u952e\u52a0\u8f7d\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torchvision.transforms&nbsp;as&nbsp;transforms\nfrom&nbsp;torchvision.datasets&nbsp;import&nbsp;CIFAR10\nfrom&nbsp;torch.utils.data&nbsp;import&nbsp;DataLoader\n\ntransform&nbsp;=&nbsp;transforms.Compose([\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.ToTensor(),&nbsp;&nbsp;#&nbsp;\u8f6c\u6362\u4e3aTensor\u5e76\u5f52\u4e00\u5316\u81f3[0,1]\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Normalize((0.4914,&nbsp;0.4822,&nbsp;0.4465),&nbsp;(0.247,&nbsp;0.243,&nbsp;0.261))&nbsp;&nbsp;#&nbsp;\u5747\u503c\u65b9\u5dee\u6807\u51c6\u5316\n])\n\ntrain_dataset&nbsp;=&nbsp;CIFAR10(root=&#39;.\/data&#39;,&nbsp;train=True,&nbsp;download=True,&nbsp;transform=transform)\ntest_dataset&nbsp;=&nbsp;CIFAR10(root=&#39;.\/data&#39;,&nbsp;train=False,&nbsp;download=True,&nbsp;transform=transform)\n\ntrain_loader&nbsp;=&nbsp;DataLoader(train_dataset,&nbsp;batch_size=64,&nbsp;shuffle=True)\ntest_loader&nbsp;=&nbsp;DataLoader(test_dataset,&nbsp;batch_size=1000,&nbsp;shuffle=False)<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2. \u6570\u636e\u589e\u5f3a\u6280\u672f<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e3a\u63d0\u5347\u6a21\u578b\u6cdb\u5316\u80fd\u529b\uff0c\u9700\u5bf9\u8bad\u7ec3\u6570\u636e\u8fdb\u884c\u968f\u673a\u53d8\u6362\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">train_transform&nbsp;=&nbsp;transforms.Compose([\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.RandomHorizontalFlip(p=0.5),&nbsp;&nbsp;#&nbsp;\u6c34\u5e73\u7ffb\u8f6c\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.RandomRotation(15),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u968f\u673a\u65cb\u8f6c\u00b115\u5ea6\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.ColorJitter(brightness=0.2,&nbsp;contrast=0.2),&nbsp;&nbsp;#&nbsp;\u4eae\u5ea6\/\u5bf9\u6bd4\u5ea6\u8c03\u6574\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.ToTensor(),\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Normalize(mean,&nbsp;std)\n])<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6548\u679c\u9a8c\u8bc1<\/span>\uff1a\u5728Kaggle\u7ec6\u80de\u5206\u7c7b\u7ade\u8d5b\u4e2d\uff0c\u91c7\u7528\u6570\u636e\u589e\u5f3a\u540e\u6a21\u578b\u51c6\u786e\u7387\u4ece89%\u63d0\u5347\u81f394%\u3002<\/p>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e8c\u3001\u6a21\u578b\u6784\u5efa\u4e0e\u4f18\u5316<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1. \u57fa\u7840CNN\u6a21\u578b\u5b9e\u73b0<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4ee53\u5c42\u5377\u79ef\u7f51\u7edc\u4e3a\u4f8b\uff0c\u5c55\u793a\u4ece\u8f93\u5165\u5230\u8f93\u51fa\u7684\u5b8c\u6574\u6d41\u7a0b\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torch.nn&nbsp;as&nbsp;nn\nimport&nbsp;torch.nn.functional&nbsp;as&nbsp;F\n\nclass&nbsp;SimpleCNN(nn.Module):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(SimpleCNN,&nbsp;self).__init__()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.conv1&nbsp;=&nbsp;nn.Conv2d(3,&nbsp;32,&nbsp;kernel_size=3,&nbsp;padding=1)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.conv2&nbsp;=&nbsp;nn.Conv2d(32,&nbsp;64,&nbsp;kernel_size=3,&nbsp;padding=1)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.pool&nbsp;=&nbsp;nn.MaxPool2d(2,&nbsp;2)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.fc1&nbsp;=&nbsp;nn.Linear(64&nbsp;*&nbsp;8&nbsp;*&nbsp;8,&nbsp;512)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.fc2&nbsp;=&nbsp;nn.Linear(512,&nbsp;10)\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;forward(self,&nbsp;x):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;self.pool(F.relu(self.conv1(x)))&nbsp;&nbsp;#&nbsp;\u8f93\u51fa\u5c3a\u5bf8:&nbsp;[batch,32,16,16]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;self.pool(F.relu(self.conv2(x)))&nbsp;&nbsp;#&nbsp;\u8f93\u51fa\u5c3a\u5bf8:&nbsp;[batch,64,8,8]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;x.view(-1,&nbsp;64&nbsp;*&nbsp;8&nbsp;*&nbsp;8)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5c55\u5e73\u64cd\u4f5c\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;F.relu(self.fc1(x))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;self.fc2(x)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;x<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u5173\u952e\u70b9<\/span>\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u5377\u79ef\u5c42\u8d1f\u8d23\u7279\u5f81\u63d0\u53d6\uff0c\u5168\u8fde\u63a5\u5c42\u5b8c\u6210\u5206\u7c7b<\/p>\n<\/li>\n<li>\n<p>ReLU\u6fc0\u6d3b\u51fd\u6570\u5f15\u5165\u975e\u7ebf\u6027\uff0c\u907f\u514d\u68af\u5ea6\u6d88\u5931<\/p>\n<\/li>\n<li>\n<p>\u6c60\u5316\u5c42\u964d\u4f4e\u7279\u5f81\u7ef4\u5ea6\uff0c\u51cf\u5c11\u8ba1\u7b97\u91cf<\/p>\n<\/li>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2. \u8fc1\u79fb\u5b66\u4e60\u5b9e\u6218<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5229\u7528\u9884\u8bad\u7ec3\u7684ResNet50\u6a21\u578b\u8fdb\u884c\u5fae\u8c03\uff08Fine-tuning\uff09\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torchvision.models&nbsp;as&nbsp;models\n\nmodel&nbsp;=&nbsp;models.resnet50(pretrained=True)\nnum_ftrs&nbsp;=&nbsp;model.fc.in_features\nmodel.fc&nbsp;=&nbsp;nn.Linear(num_ftrs,&nbsp;10)&nbsp;&nbsp;#&nbsp;\u66ff\u6362\u6700\u540e\u7684\u5168\u8fde\u63a5\u5c42\n\n#&nbsp;\u51bb\u7ed3\u524d4\u4e2aResNet\u5757\u53c2\u6570\uff08\u53ef\u9009\uff09\nfor&nbsp;param&nbsp;in&nbsp;model.parameters():\n&nbsp;&nbsp;&nbsp;&nbsp;param.requires_grad&nbsp;=&nbsp;False\nfor&nbsp;param&nbsp;in&nbsp;model.fc.parameters():\n&nbsp;&nbsp;&nbsp;&nbsp;param.requires_grad&nbsp;=&nbsp;True<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u4f18\u52bf<\/span>\uff1a<\/p>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p>\u5728ImageNet\u4e0a\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u5df2\u5b66\u4e60\u5230\u901a\u7528\u7279\u5f81\uff08\u5982\u8fb9\u7f18\u3001\u7eb9\u7406\uff09<\/p>\n<\/li>\n<li>\n<p>\u4ec5\u9700\u5c11\u91cf\u6570\u636e\u5373\u53ef\u8fbe\u5230\u9ad8\u7cbe\u5ea6\uff0c\u5c24\u5176\u9002\u5408\u533b\u5b66\u5f71\u50cf\u7b49\u6807\u6ce8\u6210\u672c\u9ad8\u7684\u9886\u57df<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e09\u3001\u8bad\u7ec3\u7b56\u7565\u4e0e\u8c03\u4f18<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1. \u635f\u5931\u51fd\u6570\u4e0e\u4f18\u5316\u5668\u9009\u62e9<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u4ea4\u53c9\u71b5\u635f\u5931<\/span>\uff1a\u9002\u7528\u4e8e\u591a\u5206\u7c7b\u4efb\u52a1\uff0c\u81ea\u52a8\u5904\u7406Softmax\u6982\u7387\u5206\u5e03<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">Adam\u4f18\u5316\u5668<\/span>\uff1a\u7ed3\u5408\u52a8\u91cf\u4e0e\u81ea\u9002\u5e94\u5b66\u4e60\u7387\uff0c\u6536\u655b\u901f\u5ea6\u5feb\u4e8eSGD<\/p>\n<\/li>\n<\/ul>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torch.optim&nbsp;as&nbsp;optim\n\ncriterion&nbsp;=&nbsp;nn.CrossEntropyLoss()\noptimizer&nbsp;=&nbsp;optim.Adam(model.parameters(),&nbsp;lr=0.001,&nbsp;weight_decay=1e-5)&nbsp;&nbsp;#&nbsp;L2\u6b63\u5219\u5316<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2. \u5b66\u4e60\u7387\u8c03\u5ea6<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u91c7\u7528\u4f59\u5f26\u9000\u706b\u7b56\u7565\u52a8\u6001\u8c03\u6574\u5b66\u4e60\u7387\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">scheduler&nbsp;=&nbsp;optim.lr_scheduler.CosineAnnealingLR(optimizer,&nbsp;T_max=50,&nbsp;eta_min=1e-6)\n\nfor&nbsp;epoch&nbsp;in&nbsp;range(100):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8bad\u7ec3\u4ee3\u7801...\n&nbsp;&nbsp;&nbsp;&nbsp;scheduler.step()<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 14px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6548\u679c<\/span>\uff1a\u5728CIFAR-10\u5b9e\u9a8c\u4e2d\uff0c\u8be5\u7b56\u7565\u4f7f\u6a21\u578b\u5728\u540e\u671f\u8bad\u7ec3\u4e2d\u8df3\u51fa\u5c40\u90e8\u6700\u4f18\uff0c\u6700\u7ec8\u51c6\u786e\u7387\u63d0\u53472.3%\u3002<\/p>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u56db\u3001\u8bc4\u4f30\u4e0e\u90e8\u7f72<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1. \u8bc4\u4f30\u6307\u6807<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">Top-1\u51c6\u786e\u7387<\/span>\uff1a\u9884\u6d4b\u6982\u7387\u6700\u9ad8\u7684\u7c7b\u522b\u662f\u5426\u6b63\u786e<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6df7\u6dc6\u77e9\u9635<\/span>\uff1a\u5206\u6790\u5404\u7c7b\u522b\u7684\u8bef\u5206\u7c7b\u60c5\u51b5<\/p>\n<\/li>\n<\/ul>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;sklearn.metrics&nbsp;import&nbsp;confusion_matrix\nimport&nbsp;matplotlib.pyplot&nbsp;as&nbsp;plt\n\ndef&nbsp;plot_confusion_matrix(y_true,&nbsp;y_pred,&nbsp;classes):\n&nbsp;&nbsp;&nbsp;&nbsp;cm&nbsp;=&nbsp;confusion_matrix(y_true,&nbsp;y_pred)\n&nbsp;&nbsp;&nbsp;&nbsp;plt.imshow(cm,&nbsp;interpolation=&#39;nearest&#39;,&nbsp;cmap=plt.cm.Blues)\n&nbsp;&nbsp;&nbsp;&nbsp;plt.xticks(range(len(classes)),&nbsp;classes,&nbsp;rotation=45)\n&nbsp;&nbsp;&nbsp;&nbsp;plt.yticks(range(len(classes)),&nbsp;classes)\n&nbsp;&nbsp;&nbsp;&nbsp;plt.show()<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2. \u6a21\u578b\u5bfc\u51fa\u4e0e\u90e8\u7f72<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8f6c\u6362\u4e3aTorchScript\u683c\u5f0f\uff0c\u652f\u6301C++\/Java\u7b49\u8bed\u8a00\u8c03\u7528\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">traced_script_module&nbsp;=&nbsp;torch.jit.trace(model,&nbsp;torch.rand(1,&nbsp;3,&nbsp;32,&nbsp;32))\ntraced_script_module.save(&quot;model.pt&quot;)<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=190"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/190\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}