1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import torch
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(LinearModel, self).__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(reduction = 'sum') optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
for epoch in range(100): y_pred = model(x_data) loss = criterion(y_pred, y_data) print(epoch, loss.item())
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)
|