Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法

這篇文章主要介紹“Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法”,在日常操作中,相信很多人在Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)公司專注于永清企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都做商城網(wǎng)站。永清網(wǎng)站建設(shè)公司,為永清等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

https://www.emperinter.info/2020/08/05/change-leaning-rate-by-reducelronplateau-in-pytorch/

Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法

緣由

> 自己之前寫過一個(gè)Pytorch學(xué)習(xí)率更新,其中感覺依據(jù)是否loss升高或降低的次數(shù)來(lái)動(dòng)態(tài)更新學(xué)習(xí)率,感覺是個(gè)挺好玩的東西,自己弄了好久都設(shè)置錯(cuò)誤,今天算是搞出來(lái)了!

解析

說(shuō)明

  • torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10, verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)

> 在發(fā)現(xiàn)loss不再降低或者acc不再提高之后,降低學(xué)習(xí)率。各參數(shù)意義如下:

參數(shù)含義
mode'min'模式檢測(cè)metric是否不再減小,'max'模式檢測(cè)metric是否不再增大;
factor觸發(fā)條件后lr*=factor;
patience不再減小(或增大)的累計(jì)次數(shù);
verbose觸發(fā)條件后print;
threshold只關(guān)注超過閾值的顯著變化;
threshold_mode有rel和abs兩種閾值計(jì)算模式,rel規(guī)則:max模式下如果超過best(1+threshold)為顯著,min模式下如果低于best(1-threshold)為顯著;abs規(guī)則:max模式下如果超過best+threshold為顯著,min模式下如果低于best-threshold為顯著;
cooldown觸發(fā)一次條件后,等待一定epoch再進(jìn)行檢測(cè),避免lr下降過速;
min_lr最小的允許lr;
eps如果新舊lr之間的差異小與1e-8,則忽略此次更新。
  • 例子,如圖所示的y軸為lr,x為調(diào)整的次序,初始的學(xué)習(xí)率為0.0009575 則學(xué)習(xí)率的方程為:lr = 0.0009575 * (0.35)^x Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法

import math 
import matplotlib.pyplot as plt
#%matplotlib inline

x = 0 
o = []
p = []
o.append(0)
p.append(0.0009575)
while(x < 8):
    x += 1
    y = 0.0009575 * math.pow(0.35,x)
    o.append(x)
    p.append(y)
    print('%d:   %.50f' %(x,y))

plt.plot(o,p,c='red',label='test') #分別為x,y軸對(duì)應(yīng)數(shù)據(jù),c:color,label
plt.legend(loc='best')  # 顯示label,loc為顯示位置(best為系統(tǒng)認(rèn)為最好的位置)
plt.show()

難點(diǎn)

> 我感覺這里面最難的時(shí)這幾個(gè)參數(shù)的選擇,第一個(gè)是初始的學(xué)習(xí)率(我目前接觸的miniest和下面的圖像分類貌似都是0.001,我這里訓(xùn)練調(diào)整時(shí)才發(fā)現(xiàn)自己設(shè)置的為0.0009575,這個(gè)值是上一個(gè)實(shí)驗(yàn)忘更改了,但發(fā)現(xiàn)結(jié)果不錯(cuò),第一次運(yùn)行該代碼接近到0.001這么小的損失值),這里面的乘積系數(shù)以及判斷說(shuō)多少次沒有減少(增加)后決定變換學(xué)習(xí)率都是難以估計(jì)的。我自己的最好方法是先按默認(rèn)不變的0.001來(lái)訓(xùn)練一下(結(jié)合**tensoarboard** )觀察從哪里開始出現(xiàn)問題就可以從這里來(lái)確定次數(shù),而乘積系數(shù),個(gè)人感覺還是用上面的代碼來(lái)獲取一個(gè)較為平滑且變化極小的數(shù)字來(lái)作為選擇。建議在做這種測(cè)試時(shí)可以把模型先備份一下以免浪費(fèi)過多的時(shí)間!

例子

  • 該例子初始學(xué)習(xí)率為0.0009575,乘積項(xiàng)系數(shù)為:0.35,在我的例子中x變化的條件是:累計(jì)125次沒有減小則x加1;自己訓(xùn)練在第一次lr變化后(從0.0009575變化到0.00011729)損失值慢慢取向于0.001(如第一張圖所示),準(zhǔn)確率達(dá)到69%;

Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法

Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法

import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from datetime import datetime
from torch.utils.tensorboard import SummaryWriter
from torch.optim import *


PATH = './cifar_net_tensorboard_net_width_200_and_chang_lr_by_decrease_0_35^x.pth'  # 保存模型地址

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=0)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=0)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')


device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Assuming that we are on a CUDA machine, this should print a CUDA device:

print(device)

print("獲取一些隨機(jī)訓(xùn)練數(shù)據(jù)")
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()


# functions to show an image
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
print("**********************")

# 設(shè)置一個(gè)tensorborad
# helper function to show an image
# (used in the `plot_classes_preds` function below)
def matplotlib_imshow(img, one_channel=False):
    if one_channel:
        img = img.mean(dim=0)
    img = img / 2 + 0.5     # unnormalize
    npimg = img.cpu().numpy()
    if one_channel:
        plt.imshow(npimg, cmap="Greys")
    else:
        plt.imshow(np.transpose(npimg, (1, 2, 0)))    

# 設(shè)置tensorBoard
# default `log_dir` is "runs" - we'll be more specific here
writer = SummaryWriter('runs/train')

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# create grid of images
img_grid = torchvision.utils.make_grid(images)

# show images
# matplotlib_imshow(img_grid, one_channel=True)
imshow(img_grid)

# write to tensorboard
# writer.add_image('imag_classify', img_grid)

# Tracking model training with TensorBoard
# helper functions

def images_to_probs(net, images):
    '''
    Generates predictions and corresponding probabilities from a trained
    network and a list of images
    '''
    output = net(images)
    # convert output probabilities to predicted class
    _, preds_tensor = torch.max(output, 1)
    # preds = np.squeeze(preds_tensor.numpy())
    preds = np.squeeze(preds_tensor.cpu().numpy())
    return preds, [F.softmax(el, dim=0)[i].item() for i, el in zip(preds, output)]


def plot_classes_preds(net, images, labels):
    preds, probs = images_to_probs(net, images)
    # plot the images in the batch, along with predicted and true labels
    fig = plt.figure(figsize=(12, 48))
    for idx in np.arange(4):
        ax = fig.add_subplot(1, 4, idx+1, xticks=[], yticks=[])
        matplotlib_imshow(images[idx], one_channel=True)
        ax.set_title("{0}, {1:.1f}%\n(label: {2})".format(
            classes[preds[idx]],
            probs[idx] * 100.0,
            classes[labels[idx]]),
                    color=("green" if preds[idx]==labels[idx].item() else "red"))
    return fig

#

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 200, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(200, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()
# # 把net結(jié)構(gòu)可視化出來(lái)
writer.add_graph(net, images)
net.to(device)

·······
·······
·······

如需了解完整代碼請(qǐng)?zhí)D(zhuǎn)到:

https://www.emperinter.info/2020/08/05/change-leaning-rate-by-reducelronplateau-in-pytorch/

到此,關(guān)于“Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

標(biāo)題名稱:Pytorch使用ReduceLROnPlateau來(lái)更新學(xué)習(xí)率的方法
瀏覽地址:http://bm7419.com/article0/gipcio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、營(yíng)銷型網(wǎng)站建設(shè)面包屑導(dǎo)航、云服務(wù)器服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)