import torch.nn
import torch.optim
x = torch.tensor([[1., 1., 1.], [2., 3., 1.], [3., 5., 1.], [4., 2., 1.], [5., 4., 1.]])
y = torch.tensor([-10., 12., 14., 16., 18.])
w = torch.zeros(3, requires_grad=True)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam([w, ], )
for step in range(30001):
if step:
optimizer.zero_grad()
loss.backward()
optimizer.step()
pred = torch.mv(x, w)
loss = criterion(pred, y) + 1e-5 * 1/3 * torch.sum(w ** 2)
print('step={}, loss={}'.format(step, loss.data))