Beispiel #1
0
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()
Beispiel #2
0
 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.)
Beispiel #3
0
def test(model, x, y):
    output = model(x)
    mse_loss = nn.MSELoss()
    loss = mse_loss(output, y)
    print(f"test: loss {loss}")