@Ricky_Han you have wrong code. Here’s a corrected training loop:
if __name__ == "__main__":
    x = Variable(torch.ones(3, 4))
    model = LinearMulti(3, 4, 1)
    y = model(x, Variable(torch.LongTensor([[1,2,1]])))
    target = Variable(torch.FloatTensor([[3], [10], [3]]))
    print(target)
    print(y)

    learning_rate = 1e-1
    optimizer = torch.optim.Adagrad(model.parameters(), lr=learning_rate)
    loss_fn = torch.nn.MSELoss(size_average=False)

    for i in range(100):
        optimizer.zero_grad()
        y = model(x, Variable(torch.LongTensor([[1,2,1]])))
        loss = loss_fn(y, target)
        loss.backward(retain_graph=True)
        optimizer.step()
        print(loss)