{"id":64519,"date":"2026-01-23T17:40:29","date_gmt":"2026-01-23T09:40:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/64519.html"},"modified":"2026-01-23T17:40:29","modified_gmt":"2026-01-23T09:40:29","slug":"pytorch%e8%bf%9b%e9%98%b6%e8%ae%ad%e7%bb%83%e6%8a%80%e5%b7%a7%ef%bc%88%e4%b8%89%ef%bc%89%e4%b9%8b%e5%ad%a6%e4%b9%a0%e7%8e%87%e8%b0%83%e5%ba%a6%e4%b8%8e%e8%ae%ad%e7%bb%83%e8%8a%82%e5%a5%8f%e6%8e%a7","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/64519.html","title":{"rendered":"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236"},"content":{"rendered":"<p>\u5728\u524d\u9762\u7684\u5185\u5bb9 Pytorch\u6df1\u5165\u6d45\u51fa&#xff08;\u4e03&#xff09;\u4e4b\u4f18\u5316\u5668&#xff08;Optimizer&#xff09;\u4ee5\u53caPytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7&#xff08;\u4e8c&#xff09;\u4e4b\u68af\u5ea6\u5c42\u9762\u7684\u4f18\u5316\u7b56\u7565\u4e2d&#xff0c;\u6211\u4eec\u5206\u522b\u89e3\u51b3\u4e86 \u201c\u600e\u4e48\u66f4\u65b0\u201d \u548c \u201c\u5982\u4f55\u7a33\u4f4f\u68af\u5ea6\u201d \u7684\u95ee\u9898\u3002<\/p>\n<p>\u4f46\u5728\u771f\u5b9e\u8bad\u7ec3\u4e2d&#xff0c;\u5373\u4fbf\u4f18\u5316\u5668\u9009\u5f97\u5bf9&#xff0c;\u68af\u5ea6\u4e5f\u7a33\u5b9a&#xff0c;\u6a21\u578b\u4f9d\u7136\u53ef\u80fd\u51fa\u73b0\u524d\u671f\u4e0d\u964d\u53cd\u5347\u3001\u540e\u671f\u6536\u655b\u7f13\u6162\u6216\u9a8c\u8bc1\u96c6\u5267\u70c8\u9707\u8361\u7684\u60c5\u51b5\u3002\u8fd9\u65f6&#xff0c;\u95ee\u9898\u901a\u5e38\u51fa\u5728 \u201c\u66f4\u65b0\u7684\u8282\u594f\u201d\u3002<\/p>\n<p>\u672c\u7bc7\u4ecb\u7ecd\u51e0\u79cd\u5b66\u4e60\u7387&#xff08;LR&#xff09;\u8c03\u5ea6\u7b56\u7565&#xff0c;\u5e2e\u52a9\u7cbe\u51c6\u63a7\u5236\u8bad\u7ec3\u8282\u594f\u3002<\/p>\n<ul>\n<li>Warmup<\/li>\n<li>\u6309\u9636\u6bb5\u8870\u51cf&#xff08;Step \/ MultiStep&#xff09;<\/li>\n<li>\u4f59\u5f26\u9000\u706b&#xff08;Cosine Annealing&#xff09;<\/li>\n<li>\u81ea\u9002\u5e94\u8c03\u5ea6&#xff08;ReduceLROnPlateau&#xff09;<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e00\u3001Warmup&#xff08;\u5b66\u4e60\u7387\u9884\u70ed&#xff09;<\/h3>\n<h4>1. \u4e3a\u4ec0\u4e48\u9700\u8981 Warmup&#xff1f;<\/h4>\n<p>\u5728\u8bad\u7ec3\u521d\u671f&#xff0c;\u6a21\u578b\u53c2\u6570\u5904\u4e8e\u968f\u673a\u521d\u59cb\u5316\u72b6\u6001&#xff0c;\u68af\u5ea6\u65b9\u5411\u6781\u5176\u4e0d\u7a33\u5b9a\u3002\u5982\u679c\u4e00\u5f00\u59cb\u5c31\u4f7f\u7528\u8f83\u5927\u7684\u5b66\u4e60\u7387&#xff0c;\u5bb9\u6613\u5bfc\u81f4&#xff1a;<\/p>\n<ul>\n<li>Loss \u77ac\u95f4\u53d1\u6563&#xff1a;\u521d\u59cb\u68af\u5ea6\u8fc7\u5927\u76f4\u63a5\u628a\u6a21\u578b\u201c\u5e26\u504f\u201d\u3002<\/li>\n<li>\u8bad\u7ec3\u5d29\u6e83&#xff1a;\u5728 Transformer \u6216\u5927 Batch \u8bad\u7ec3\u4e2d\u6781\u5176\u5e38\u89c1\u3002<\/li>\n<\/ul>\n<h4>2. \u6838\u5fc3\u601d\u60f3<\/h4>\n<p>\u5148\u793c\u540e\u5175&#xff1a;\u5b66\u4e60\u7387\u4ece\u5c0f\u5230\u5927&#xff0c;\u9010\u6b65\u8fdb\u5165\u6b63\u5e38\u8bad\u7ec3\u533a\u95f4\u3002 Warmup \u5e76\u4e0d\u662f\u201c\u8870\u51cf\u201d\u7b56\u7565&#xff0c;\u800c\u662f\u4e00\u79cd\u8bad\u7ec3\u521d\u671f\u7684\u542f\u52a8\u8c03\u5ea6&#xff0c;\u901a\u5e38\u4e0e\u540e\u7eed\u7684\u8870\u51cf\u7b56\u7565\u7ec4\u5408\u4f7f\u7528\u3002 \u6700\u5e38\u89c1\u7684\u662f\u7ebf\u6027 Warmup&#xff1a;\u5728\u8bad\u7ec3\u7684\u524d <span class=\"katex--inline\"><span class=\"katex\"><span class=\"katex-mathml\"> <\/p>\n<p>         N <\/p>\n<p>        N <\/p>\n<p>    <\/span><span class=\"katex-html\"><span class=\"base\"><span class=\"strut\" style=\"height: 0.6833em\"><\/span><span class=\"mord mathnormal\" style=\"margin-right: 0.109em\">N<\/span><\/span><\/span><\/span><\/span> \u4e2a Step&#xff08;\u6216\u5728\u5c11\u91cf Epoch \u5185\u6309 Step \u7ebf\u6027\u589e\u957f&#xff09;&#xff0c;\u8ba9\u5b66\u4e60\u7387\u4ece\u4e00\u4e2a\u6781\u5c0f\u503c\u7ebf\u6027\u589e\u957f\u5230\u8bbe\u5b9a\u7684\u521d\u59cb\u5b66\u4e60\u7387\u3002<\/p>\n<h4>3. PyTorch \u4e2d\u7684\u5178\u578b\u5b9e\u73b0\u65b9\u5f0f&#xff08;LambdaLR&#xff09;<\/h4>\n<p>optimizer <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>AdamW<span class=\"token punctuation\">(<\/span>model<span class=\"token punctuation\">.<\/span>parameters<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> lr<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1e-3<\/span><span class=\"token punctuation\">)<\/span><br \/>\nwarmup_steps <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1000<\/span><\/p>\n<p><span class=\"token comment\"># \u5b9a\u4e49\u7ebf\u6027\u589e\u957f\u903b\u8f91<\/span><br \/>\nlr_lambda <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">lambda<\/span> step<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">min<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1.0<\/span><span class=\"token punctuation\">,<\/span> step <span class=\"token operator\">\/<\/span> warmup_steps<span class=\"token punctuation\">)<\/span><br \/>\nscheduler <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>lr_scheduler<span class=\"token punctuation\">.<\/span>LambdaLR<span class=\"token punctuation\">(<\/span>optimizer<span class=\"token punctuation\">,<\/span> lr_lambda<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># &#8212;&#8212;&#8211; \u8bad\u7ec3\u5faa\u73af&#xff08;\u6309 Step \u66f4\u65b0&#xff09; &#8212;&#8212;&#8211;<\/span><br \/>\n<span class=\"token keyword\">for<\/span> images<span class=\"token punctuation\">,<\/span> labels <span class=\"token keyword\">in<\/span> train_loader<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># Forward &amp; Backward &#8230;<\/span><br \/>\n    optimizer<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    scheduler<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>    <span class=\"token comment\"># Warmup \u901a\u5e38\u662f Step \u7ea7\u7684&#xff0c;\u6bcf\u4e00\u6b65\u90fd\u66f4\u65b0\u6b65\u5e45<\/span><br \/>\n    optimizer<span class=\"token punctuation\">.<\/span>zero_grad<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>&#x1f449; \u5f62\u8c61\u6bd4\u55bb&#xff1a;\u5c31\u50cf\u51b7\u8f66\u542f\u52a8\u3002\u4e0d\u80fd\u4e00\u6253\u706b\u5c31\u8e29\u6b7b\u6cb9\u95e8&#xff0c;\u5f97\u5148\u8ba9\u53d1\u52a8\u673a\u6020\u901f\u70ed\u4e00\u70ed&#xff0c;\u8f6c\u901f\u7a33\u4e86\u518d\u52a0\u901f\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u9636\u6bb5\u6027\u8870\u51cf&#xff08;Step \/ MultiStep&#xff09;<\/h3>\n<h4>1. \u4e3a\u4ec0\u4e48\u9700\u8981\u9636\u6bb5\u6027\u8870\u51cf&#xff1f;<\/h4>\n<p>\u968f\u7740\u8bad\u7ec3\u8fdb\u884c&#xff0c;\u6a21\u578b\u9010\u6e10\u63a5\u8fd1\u6700\u4f18\u89e3\u533a\u57df\u3002\u6b64\u65f6\u5927\u6b65\u957f\u4f1a\u5bfc\u81f4\u5728\u201c\u8c37\u5e95\u201d\u53cd\u590d\u6a2a\u8df3&#xff0c;\u65e0\u6cd5\u7cbe\u51c6\u964d\u843d\u3002<\/p>\n<ul>\n<li>\u524d\u671f&#xff1a;\u9700\u8981\u5927\u6b65\u8d70&#xff0c;\u5feb\u901f\u6536\u655b\u3002<\/li>\n<li>\u540e\u671f&#xff1a;\u9700\u8981\u6362\u5c0f\u6b65&#xff0c;\u7cbe\u7ec6\u5fae\u8c03\u3002<\/li>\n<\/ul>\n<h4>2. \u6838\u5fc3\u601d\u60f3<\/h4>\n<p>\u5728\u9884\u8bbe\u7684\u65f6\u95f4\u70b9&#xff08;Step \u6216 Epoch&#xff09;&#xff0c;\u5c06\u5b66\u4e60\u7387\u4e58\u4ee5\u4e00\u4e2a\u8870\u51cf\u56e0\u5b50 \u03b3 \u5e38\u89c1\u5f62\u5f0f\u662f StepLR&#xff1a;\u56fa\u5b9a\u95f4\u9694\u8870\u51cf&#xff1b;MultiStepLR&#xff1a;\u6307\u5b9a\u82e5\u5e72\u5173\u952e\u8282\u70b9\u8870\u51cf<\/p>\n<h4>3. PyTorch \u4e2d\u7684\u6807\u51c6\u7528\u6cd5<\/h4>\n<p>StepLR<\/p>\n<p><span class=\"token comment\"># \u6bcf 30 \u4e2a Epoch&#xff0c;\u5b66\u4e60\u7387\u7f29\u5c0f\u4e3a\u539f\u6765\u7684 1\/10<\/span><br \/>\nscheduler <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>lr_scheduler<span class=\"token punctuation\">.<\/span>StepLR<span class=\"token punctuation\">(<\/span>optimizer<span class=\"token punctuation\">,<\/span> step_size<span class=\"token operator\">&#061;<\/span><span class=\"token number\">30<\/span><span class=\"token punctuation\">,<\/span> gamma<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0.1<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> epoch <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    train_one_epoch<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    scheduler<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>    <span class=\"token comment\"># \u9636\u6bb5\u6027\u8870\u51cf\u901a\u5e38\u5728\u6bcf\u4e2a Epoch \u7ed3\u675f\u65f6\u66f4\u65b0<\/span><\/p>\n<p>MultiStepLR<\/p>\n<p><span class=\"token comment\"># \u6216\u8005\u6307\u5b9a\u91cc\u7a0b\u7891\u70b9&#xff1a;\u5728\u7b2c 30, 80, 120 Epoch \u8870\u51cf<\/span><br \/>\nscheduler <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>lr_scheduler<span class=\"token punctuation\">.<\/span>MultiStepLR<span class=\"token punctuation\">(<\/span>optimizer<span class=\"token punctuation\">,<\/span> milestones<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">30<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">80<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">120<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> gamma<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0.1<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> epoch <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    train_one_epoch<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    scheduler<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>    <span class=\"token comment\"># \u9636\u6bb5\u6027\u8870\u51cf\u901a\u5e38\u5728\u6bcf\u4e2a Epoch \u7ed3\u675f\u65f6\u66f4\u65b0<\/span><\/p>\n<p>&#x1f449; \u5f62\u8c61\u6bd4\u55bb&#xff1a;\u5c31\u50cf\u4e0b\u5c71\u6362\u6321\u3002\u5761\u5ea6\u9661\u65f6&#xff08;\u521d\u671f&#xff09;\u9700\u8981\u5feb\u901f\u6ed1\u884c&#xff0c;\u63a5\u8fd1\u5e73\u5730&#xff08;\u540e\u671f&#xff09;\u65f6\u8981\u6362\u4f4e\u6863\u6162\u884c&#xff0c;\u9632\u6b62\u5239\u4e0d\u4f4f\u8f66\u649e\u5230\u5c71\u5934\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u4f59\u5f26\u9000\u706b&#xff08;Cosine Annealing&#xff09;<\/h3>\n<h4>1. \u4e3a\u4ec0\u4e48\u9700\u8981\u4f59\u5f26\u9000\u706b&#xff1f;<\/h4>\n<p>\u76f8\u6bd4\u201c\u79bb\u6563\u5f0f\u201d\u7684\u9636\u6bb5\u7a81\u53d1\u8870\u51cf&#xff0c;\u4f59\u5f26\u9000\u706b\u63d0\u4f9b\u4e86\u4e00\u79cd\u8fde\u7eed\u3001\u5e73\u6ed1\u7684\u8282\u594f\u63a7\u5236\u65b9\u5f0f\u3002<\/p>\n<p>\u4f59\u5f26\u9000\u706b\u662f\u76ee\u524d SOTA \u6a21\u578b&#xff08;\u5982 ResNet, Transformer&#xff09;\u7684\u6807\u914d&#xff0c;\u5b83\u8ba9\u5b66\u4e60\u7387\u6309\u7167\u4f59\u5f26\u66f2\u7ebf\u5e73\u6ed1\u4e0b\u964d\u3002\u5b9e\u9645\u5de5\u7a0b\u4e2d\u5e38\u4e0e Warmup \u7ec4\u5408\u4f7f\u7528&#xff0c;\u5f62\u6210 Warmup &#043; Cosine \u7684\u4e24\u9636\u6bb5\u8c03\u5ea6\u7b56\u7565\u3002<\/p>\n<h4>2. \u6838\u5fc3\u601d\u60f3<\/h4>\n<p>\u5229\u7528\u4f59\u5f26\u51fd\u6570\u7684\u5355\u8c03\u9012\u51cf\u6bb5&#xff0c;\u8ba9\u5b66\u4e60\u7387\u5e73\u6ed1\u3001\u4f18\u96c5\u5730\u4ece\u6700\u5927\u503c\u964d\u5230\u6700\u5c0f\u503c\u3002<\/p>\n<h4>3. PyTorch \u4e2d\u7684\u6807\u51c6\u7528\u6cd5<\/h4>\n<p><span class=\"token comment\"># T_max \u662f\u4e0b\u964d\u5468\u671f&#xff0c;\u4e00\u822c\u8bbe\u4e3a\u603b Epoch \u6570<\/span><br \/>\nscheduler <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>lr_scheduler<span class=\"token punctuation\">.<\/span>CosineAnnealingLR<span class=\"token punctuation\">(<\/span>optimizer<span class=\"token punctuation\">,<\/span> T_max<span class=\"token operator\">&#061;<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">,<\/span> eta_min<span class=\"token operator\">&#061;<\/span><span class=\"token number\">1e-6<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> epoch <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    train_one_epoch<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    scheduler<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>&#x1f449; \u5f62\u8c61\u6bd4\u55bb&#xff1a;\u5c31\u50cf\u98de\u673a\u7684\u964d\u843d\u8fc7\u7a0b\u3002\u4e0d\u662f\u4e00\u964d\u5230\u5e95&#xff0c;\u800c\u662f\u5e73\u6ed1\u5730\u51cf\u901f\u5e76\u63a5\u89e6\u8dd1\u9053&#xff0c;\u4f53\u9a8c\u6781\u5176\u4e1d\u6ed1\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u81ea\u9002\u5e94\u8c03\u5ea6&#xff08;ReduceLROnPlateau&#xff09;<\/h3>\n<h4>1. \u4e3a\u4ec0\u4e48\u9700\u8981\u81ea\u9002\u5e94\u8c03\u5ea6&#xff1f;<\/h4>\n<p>\u6709\u65f6\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u9884\u77e5\u6a21\u578b\u4ec0\u4e48\u65f6\u5019\u4f1a\u201c\u5b66\u4e0d\u52a8\u201d\u3002<\/p>\n<ul>\n<li>\u63d0\u524d\u964d\u901f\u53ef\u80fd\u5bfc\u81f4\u6536\u655b\u4e0d\u8db3\u3002<\/li>\n<li>\u964d\u901f\u592a\u665a\u5219\u6d6a\u8d39\u8ba1\u7b97\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<h4>2. \u6838\u5fc3\u601d\u60f3<\/h4>\n<p>\u6307\u6807\u5bfc\u5411\u3002\u76d1\u63a7\u9a8c\u8bc1\u96c6\u7684 Loss \u6216 Accuracy\u3002\u5982\u679c\u6307\u6807\u5728\u4e00\u6bb5\u65f6\u95f4\u5185&#xff08;Patience&#xff09;\u4e0d\u518d\u6539\u5584&#xff0c;\u81ea\u52a8\u964d\u4f4e\u5b66\u4e60\u7387\u3002<\/p>\n<h4>3. PyTorch \u6807\u51c6\u7528\u6cd5<\/h4>\n<p>scheduler <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>optim<span class=\"token punctuation\">.<\/span>lr_scheduler<span class=\"token punctuation\">.<\/span>ReduceLROnPlateau<span class=\"token punctuation\">(<\/span><br \/>\n    optimizer<span class=\"token punctuation\">,<\/span><br \/>\n    mode<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;min&#039;<\/span><span class=\"token punctuation\">,<\/span>    <span class=\"token comment\"># \u76d1\u63a7 Loss \u65f6\u9009 min&#xff0c;\u76d1\u63a7 Acc \u65f6\u9009 max<\/span><br \/>\n    factor<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0.1<\/span><span class=\"token punctuation\">,<\/span>    <span class=\"token comment\"># \u7f29\u653e\u56e0\u5b50<\/span><br \/>\n    patience<span class=\"token operator\">&#061;<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span>    <span class=\"token comment\"># \u5bb9\u5fcd 5 \u4e2a Epoch \u6ca1\u8fdb\u6b65\u5c31\u964d\u901f<\/span><br \/>\n    verbose<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span>   <span class=\"token comment\"># \u6253\u5370\u964d\u901f\u65e5\u5fd7<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">for<\/span> epoch <span class=\"token keyword\">in<\/span> <span class=\"token builtin\">range<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    val_loss <span class=\"token operator\">&#061;<\/span> validate<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    scheduler<span class=\"token punctuation\">.<\/span>step<span class=\"token punctuation\">(<\/span>val_loss<span class=\"token punctuation\">)<\/span> <span class=\"token comment\"># \u6ce8\u610f&#xff1a;\u8fd9\u91cc\u5fc5\u987b\u4f20\u5165\u76d1\u63a7\u7684\u6307\u6807<\/span><\/p>\n<p>&#x1f449; \u5f62\u8c61\u6bd4\u55bb&#xff1a;\u5c31\u50cf\u4e00\u4e2a\u8d34\u8eab\u6559\u7ec3\u3002\u5f53\u4f60\u8bad\u7ec3\u9047\u5230\u74f6\u9888&#xff08;Plateau&#xff09;\u4e14\u5c1d\u8bd5\u4e86 patience \u6b21\u90fd\u6ca1\u7a81\u7834\u65f6&#xff0c;\u4ed6\u624d\u4f1a\u8981\u6c42\u4f60\u653e\u6162\u6b65\u8c03&#xff0c;\u91cd\u65b0\u5bfb\u627e\u7a81\u7834\u53e3\u3002<\/p>\n<h3>&#x1f4a1; \u603b\u7ed3&#xff1a;\u8bad\u7ec3\u8282\u594f\u914d\u7f6e<\/h3>\n<p>\u4e0d\u540c\u8c03\u5ea6\u5668\u7684\u66f4\u65b0\u7c92\u5ea6\u4e0d\u540c&#xff1a;Warmup \u901a\u5e38\u6309 Step \u66f4\u65b0&#xff0c;Step\/Cosine \u591a\u6309 Epoch \u66f4\u65b0&#xff0c;\u800c Plateau \u5219\u7531\u9a8c\u8bc1\u6307\u6807\u89e6\u53d1\u3002<\/p>\n<table>\n<tr>\u7b56\u7565\u9002\u7528\u573a\u666f\u6838\u5fc3\u4ef7\u503c<\/tr>\n<tbody>\n<tr>\n<td>Warmup<\/td>\n<td>\u5927\u6a21\u578b\u3001AdamW\u3001\u5927 Batch<\/td>\n<td>\u9632\u6b62\u8bad\u7ec3\u521d\u671f\u5d29\u6e83<\/td>\n<\/tr>\n<tr>\n<td>Cosine Annealing<\/td>\n<td>\u7edd\u5927\u591a\u6570\u5206\u7c7b\u3001\u68c0\u6d4b\u4efb\u52a1<\/td>\n<td>\u8ffd\u6c42\u6781\u9650\u7cbe\u5ea6&#xff0c;\u8fc7\u7a0b\u4e1d\u6ed1\u5e73\u7a33<\/td>\n<\/tr>\n<tr>\n<td>Step \/ MultiStep<\/td>\n<td>\u6570\u636e\u89c4\u6a21\u5c0f\u3001\u7ecf\u5178 CNN<\/td>\n<td>\u903b\u8f91\u7b80\u5355&#xff0c;\u5b9e\u9a8c\u53ef\u590d\u73b0\u6027\u6781\u5f3a<\/td>\n<\/tr>\n<tr>\n<td>ReduceLROnPlateau<\/td>\n<td>\u8c03\u53c2\u7ecf\u9a8c\u4e0d\u8db3\u3001\u6307\u6807\u9707\u8361<\/td>\n<td>\u6307\u6807\u9a71\u52a8\u3001\u51cf\u5c11\u624b\u52a8\u8c03\u53c2&#xff0c;\u4f46\u4f9d\u8d56\u9a8c\u8bc1\u6307\u6807\u8d28\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u6700\u7ec8\u5b9e\u6218\u5efa\u8bae&#xff1a;<\/h4>\n<p>1\ufe0f\u20e3 \u5728\u73b0\u4ee3\u6a21\u578b\u4e2d&#xff0c;\u6700\u5f3a\u7684\u7ec4\u5408\u901a\u5e38\u662f&#xff1a;Linear Warmup (\u524d 5 Epoch) &#043; Cosine Annealing (\u540e\u671f)\u3002\u8fd9\u5957\u7ec4\u5408\u62f3\u51e0\u4e4e\u80fd\u641e\u5b9a 90% \u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u548c NLP \u4efb\u52a1\u3002 2\ufe0f\u20e3 ReduceLROnPlateau \u66f4\u7a33&#xff0c;\u4f46\u5bf9\u6307\u6807\u8d28\u91cf\u548c\u9a8c\u8bc1\u9891\u7387\u8f83\u654f\u611f&#xff0c;\u4f7f\u7528\u65f6\u4ecd\u9700\u6ce8\u610f\u9a8c\u8bc1\u566a\u58f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u9762\u7684\u5185\u5bb9 Pytorch\u6df1\u5165\u6d45\u51fa&#xff08;\u4e03&#xff09;\u4e4b\u4f18\u5316\u5668&#xff08;Optimizer&#xff09;\u4ee5\u53caPytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7&#xff08;\u4e8c&#xff09;\u4e4b\u68af\u5ea6\u5c42\u9762\u7684\u4f18\u5316\u7b56\u7565\u4e2d&#xff0c;\u6211\u4eec\u5206\u522b\u89e3\u51b3\u4e86 \u201c\u600e\u4e48\u66f4\u65b0\u201d \u548c \u201c\u5982\u4f55\u7a33\u4f4f\u68af\u5ea6\u201d \u7684\u95ee\u9898\u3002<br \/>\n\u4f46\u5728\u771f\u5b9e\u8bad\u7ec3\u4e2d&#xff0c;\u5373\u4fbf\u4f18\u5316\u5668\u9009\u5f97\u5bf9&#xff0c;\u68af\u5ea6\u4e5f\u7a33\u5b9a&#xff0c;\u6a21\u578b\u4f9d\u7136\u53ef\u80fd\u51fa\u73b0\u524d\u671f\u4e0d\u964d\u53cd\u5347\u3001\u540e\u671f\u6536\u655b\u7f13\u6162\u6216\u9a8c\u8bc1\u96c6\u5267\u70c8\u9707\u8361\u7684\u60c5\u51b5\u3002\u8fd9\u65f6&#xff0c;\u95ee\u9898\u901a\u5e38\u51fa\u5728<\/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":[152,50,371,86],"topic":[],"class_list":["post-64519","post","type-post","status-publish","format-standard","hentry","category-server","tag-pytorch","tag-50","tag-371","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \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\/64519.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728\u524d\u9762\u7684\u5185\u5bb9 Pytorch\u6df1\u5165\u6d45\u51fa&#xff08;\u4e03&#xff09;\u4e4b\u4f18\u5316\u5668&#xff08;Optimizer&#xff09;\u4ee5\u53caPytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7&#xff08;\u4e8c&#xff09;\u4e4b\u68af\u5ea6\u5c42\u9762\u7684\u4f18\u5316\u7b56\u7565\u4e2d&#xff0c;\u6211\u4eec\u5206\u522b\u89e3\u51b3\u4e86 \u201c\u600e\u4e48\u66f4\u65b0\u201d \u548c \u201c\u5982\u4f55\u7a33\u4f4f\u68af\u5ea6\u201d \u7684\u95ee\u9898\u3002 \u4f46\u5728\u771f\u5b9e\u8bad\u7ec3\u4e2d&#xff0c;\u5373\u4fbf\u4f18\u5316\u5668\u9009\u5f97\u5bf9&#xff0c;\u68af\u5ea6\u4e5f\u7a33\u5b9a&#xff0c;\u6a21\u578b\u4f9d\u7136\u53ef\u80fd\u51fa\u73b0\u524d\u671f\u4e0d\u964d\u53cd\u5347\u3001\u540e\u671f\u6536\u655b\u7f13\u6162\u6216\u9a8c\u8bc1\u96c6\u5267\u70c8\u9707\u8361\u7684\u60c5\u51b5\u3002\u8fd9\u65f6&#xff0c;\u95ee\u9898\u901a\u5e38\u51fa\u5728\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/64519.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-23T09:40:29+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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64519.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/64519.html\",\"name\":\"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-23T09:40:29+00:00\",\"dateModified\":\"2026-01-23T09:40:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/64519.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/64519.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64519.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236\"}]},{\"@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":"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \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\/64519.html","og_locale":"zh_CN","og_type":"article","og_title":"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728\u524d\u9762\u7684\u5185\u5bb9 Pytorch\u6df1\u5165\u6d45\u51fa&#xff08;\u4e03&#xff09;\u4e4b\u4f18\u5316\u5668&#xff08;Optimizer&#xff09;\u4ee5\u53caPytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7&#xff08;\u4e8c&#xff09;\u4e4b\u68af\u5ea6\u5c42\u9762\u7684\u4f18\u5316\u7b56\u7565\u4e2d&#xff0c;\u6211\u4eec\u5206\u522b\u89e3\u51b3\u4e86 \u201c\u600e\u4e48\u66f4\u65b0\u201d \u548c \u201c\u5982\u4f55\u7a33\u4f4f\u68af\u5ea6\u201d \u7684\u95ee\u9898\u3002 \u4f46\u5728\u771f\u5b9e\u8bad\u7ec3\u4e2d&#xff0c;\u5373\u4fbf\u4f18\u5316\u5668\u9009\u5f97\u5bf9&#xff0c;\u68af\u5ea6\u4e5f\u7a33\u5b9a&#xff0c;\u6a21\u578b\u4f9d\u7136\u53ef\u80fd\u51fa\u73b0\u524d\u671f\u4e0d\u964d\u53cd\u5347\u3001\u540e\u671f\u6536\u655b\u7f13\u6162\u6216\u9a8c\u8bc1\u96c6\u5267\u70c8\u9707\u8361\u7684\u60c5\u51b5\u3002\u8fd9\u65f6&#xff0c;\u95ee\u9898\u901a\u5e38\u51fa\u5728","og_url":"https:\/\/www.wsisp.com\/helps\/64519.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-23T09:40:29+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/64519.html","url":"https:\/\/www.wsisp.com\/helps\/64519.html","name":"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-23T09:40:29+00:00","dateModified":"2026-01-23T09:40:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/64519.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/64519.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/64519.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Pytorch\u8fdb\u9636\u8bad\u7ec3\u6280\u5de7\uff08\u4e09\uff09\u4e4b\u5b66\u4e60\u7387\u8c03\u5ea6\u4e0e\u8bad\u7ec3\u8282\u594f\u63a7\u5236"}]},{"@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\/64519","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=64519"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/64519\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=64519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=64519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=64519"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=64519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}