import torch from torch import nn, optim from torch.autograd import Variable import numpy as np import matplotlib.pyplot as plt x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], [9.779], [6.182], [7.59], [2.167], [7.042], [10.791], [5.313], [7.997], [3.1]], dtype=np.float32) y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], [3.366], [2.596], [2.53], [1.221], [2.827], [3.465], [1.65], [2.904], [1.3]], dtype=np.float32) x_train = torch.from_numpy(x_train) y_train = torch.from_numpy(y_train) # Linear Regression Model class LinearRegression(nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear1 = nn.Linear(1, 5) # input and output is 1 dimension self.linear2 = nn.Linear(5, 1) def forward(self, x): out = self.linear1(x) out = self.linear2(out) return out model = LinearRegression() print(model.linear1) # 微调:自定义每一层的学习率 # 定义loss和优化函数 criterion = nn.MSELoss() optimizer = optim.SGD( [{"params": model.linear1.parameters(), "lr": 0.01}, {"params": model.linear2.parameters()}], lr=0.02)
查看更多关于pytorch不同的层设置不同的学习率的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did126297