def train(model, x, y, epoch=30): # TODO optimizer = optim.SGD(model.parameters(), lr=0.1) mse_loss = nn.MSELoss() for i in range(1, epoch + 1): model.zero_grad() output = model(x) loss = mse_loss(output, y) print(f"train: epoch {i}, loss {loss}") loss.backward() optimizer.step()
def test_linear(self): input = Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) target = Tensor([[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]) loss = nn.MSELoss() output = loss(input, target) self.assertEqual(output.data.tolist(), 25.)
def test(model, x, y): output = model(x) mse_loss = nn.MSELoss() loss = mse_loss(output, y) print(f"test: loss {loss}")