{"id":2554,"date":"2021-12-17T15:52:43","date_gmt":"2021-12-17T07:52:43","guid":{"rendered":"http:\/\/www.yatenglg.cn\/?p=2554"},"modified":"2022-10-18T17:30:43","modified_gmt":"2022-10-18T09:30:43","slug":"pytorch-%e5%ad%a6%e4%b9%a0%e7%8e%87%e8%b0%83%e6%95%b4%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=2554","title":{"rendered":"pytorch \u5b66\u4e60\u7387\u8c03\u6574\u7b56\u7565"},"content":{"rendered":"\n<p><strong>lr = 0.1<\/strong><\/p>\n\n\n\n<p><strong>epoch = 100<\/strong><\/p>\n\n\n\n<ol><li>LambdaLR<\/li><\/ol>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code>lambda1 = lambda epoch: self.step \/\/ 10\nscheduler = torch.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lambda1)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr.svg\" alt=\"\" class=\"wp-image-2559\" width=\"248\" height=\"150\"\/><\/figure>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code>lambda1 = lambda epoch: math.sqrt(epoch)\nscheduler = torch.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lambda1)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr2.svg\" alt=\"\" class=\"wp-image-2567\" width=\"248\" height=\"150\"\/><\/figure>\n\n\n\n<p>2. StepLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u6bcf\u7ecf\u8fc725\u6b65\uff0c\u5b66\u4e60\u7387\u4e58\u4ee50.1\nscheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=25, gamma=0.1)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr3.svg\" alt=\"\" class=\"wp-image-2573\" width=\"248\" height=\"150\"\/><\/figure>\n\n\n\n<p>3. MultiStepLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u5f53\u6b65\u6570\u5230\u8fbe50\uff0c 75\u65f6\uff0c\u5b66\u4e60\u7387\u4e58\u4ee50.1\nscheduler = torch.optim.lr_scheduler.MultiStepLR(self.optimizer, milestones=[50,75], gamma=0.1)\n<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2585 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr4.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr4.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr4.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>4. ConstantLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u524d30\u6b65\u65f6\uff0c\u5b66\u4e60\u7387\u4e58\u4ee50.1\uff0c\u4e4b\u540e\u6062\u590d\nscheduler = torch.optim.lr_scheduler.ConstantLR(self.optimizer, factor=0.1, total_iters=30)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2591 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr5.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr5.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr5.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>5. LinearLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u521d\u59cb\u5b66\u4e60\u7387\u4e58\u4ee50.1\uff0c\u4e4b\u540e20\u6b65\u7ebf\u6027\u6062\u590d\u521d\u59cb\u5b66\u4e60\u7387\nscheduler = torch.optim.lr_scheduler.LinearLR(self.optimizer, start_factor=0.1, total_iters=20)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2599 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr6.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr6.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr6.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>6. ExponentialLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u521d\u59cb\u5b66\u4e60\u73870.1\uff0c \u6bcf\u6b65\u5b66\u4e60\u7387\u4e58\u4ee50.9\nscheduler = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=0.9)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2606 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr7.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr7.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr7.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>7. CyclicLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u5faa\u73af\u5b66\u4e60\u7387\uff0c\u5faa\u73af\u534a\u5468\u671f20\u6b65\uff0c\u6700\u5c0f\u5b66\u4e60\u73870.01\uff0c \u6700\u5927\u5b66\u4e60\u73870.1\nscheduler = torch.optim.lr_scheduler.CyclicLR(self.optimizer, base_lr=0.01, max_lr=0.1, step_size_up=20)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2639 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr11.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr11.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr11.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>8. CosineAnnealingLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u4f59\u5f26\u8c03\u6574\u5b66\u4e60\u7387\uff0c\u534a\u5468\u671f\u4e3a10\u6b65\uff0c\u6700\u5c0f\u5b66\u4e60\u7387\u4e3a0.05\nscheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.optimizer, T_max=10, eta_min=0.05)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2622 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr9.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr9.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr9.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>9. CosineAnnealingWarmRestarts<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u521d\u59cb\u534a\u5468\u671f\u4e3a5\uff0c\u534a\u5468\u671f\u7cfb\u6570\u4e3a2\uff0c\u6700\u5c0f\u5b66\u4e60\u7387\u4e3a0.05\nscheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(self.optimizer, T_0=10, T_mult=2, eta_min=0.05)<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2647 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr12.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr12.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr12.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>10. SequentialLR<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u5b66\u4e60\u7387\u8c03\u6574\u7ec4\u5408(\u987a\u5e8f)\nscheduler1 = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=0.9)\nscheduler2 = torch.optim.lr_scheduler.MultiStepLR(self.optimizer, milestones=[60, 85], gamma=0.1)\n# \u524d20\u6b65\uff0c\u4f7f\u7528scheduler1\uff0c \u4e4b\u540e\u7528scheduler2\nscheduler = torch.optim.lr_scheduler.SequentialLR(self.optimizer, schedulers=[scheduler1, scheduler2], milestones=[20])<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2632 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr8-1.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr8-1.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr8-1.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>11. ChainedScheduler<\/p>\n\n\n\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-plain\"><code># \u591a\u4e2a\u5b66\u4e60\u7387\u8c03\u6574\u53e0\u52a0\u3002\nscheduler1 = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=0.9)\nscheduler2 = torch.optim.lr_scheduler.ConstantLR(self.optimizer, factor=0.1, total_iters=20)\nscheduler = torch.optim.lr_scheduler.ChainedScheduler([scheduler1, scheduler2])<\/code><\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img class=\"alignnone wp-image-2627 size-medium\" src=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/2021\/12\/lr10.svg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr10.svg 150w, http:\/\/www.yatenglg.cn\/blog\/wp-content\/uploads\/\/2021\/12\/lr10.svg 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>lr = 0.1 epoch = 100 LambdaLR 2. StepLR 3. MultiStepLR &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2554"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2554"}],"version-history":[{"count":91,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2554\/revisions"}],"predecessor-version":[{"id":2867,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2554\/revisions\/2867"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2554"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}