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()}],