第一次接触pytorch,本贴仅记录学习过程,侵删
在B站看完了视频的P5 05.用pytorch实现线性回归。 附上视频地址:《PyTorch深度学习实践》完结合集_05. 用pytorch实现线性回归
先记录一些笔记。
import torch.nn
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])
class LinearModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
criterion = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('w=', model.linear.weight.item())
print('b=', model.linear.bias.item())
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred=', y_test.data)
作业: 其中LBFGS会多次重新计算函数,这样就要使用一个闭包(closure)来支持多次计算model的操作。
import matplotlib.pyplot as plt
import torch.nn
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])
class LinearModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
criterion = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.LBFGS(model.parameters(), lr=0.01)
loss_list = []
time = []
for epoch in range(1000):
def closure():
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss)
loss_list.append(loss.item())
time.append(epoch)
optimizer.zero_grad()
loss.backward()
return loss
optimizer.step(closure)
print('w=', model.linear.weight.item())
print('b=', model.linear.bias.item())
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred=', y_test.data)
plt.plot(time, loss_list)
plt.title('LBFGS')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
图: 其余的Optimizer代码不一定需要使用闭包:
import matplotlib.pyplot as plt
import torch.nn
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])
class LinearModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
criterion = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.Adagrad(model.parameters(), lr=0.01)
loss_list = []
time = []
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss)
loss_list.append(loss.item())
time.append(epoch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('w=', model.linear.weight.item())
print('b=', model.linear.bias.item())
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred=', y_test.data)
plt.plot(time, loss_list)
plt.title('Adagrad')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
图:
|