import torch
x_data=torch.Tensor([[1.], [2.], [3.]])
y_data=torch.Tensor([[2.], [4.], [6.]])
class MyModule(torch.nn.Module):
def __init__(self):
super(MyModule,self).__init__()
self.linear=torch.nn.Linear(1,1)
def forward(self,x):
y_pred=self.linear(x)
return y_pred
model=MyModule()
criter=torch.nn.MSELoss(reduction="sum")
optm=torch.optim.SGD(model.parameters(),lr=0.01)
E=100
for epoch in range(E):
y_pred=model(x_data)
loss=criter(y_data,y_pred)
print("epoch{},loss={}".format(epoch,loss))
optm.zero_grad()
loss.backward()
optm.step()
print(model.linear.weight.data.item(),model.linear.bias.data)