Hello, I got an error when writing a simple linear regression model using autograd. My model is
y = w1 * x1 + w2*x2 + b and I need to learn parameters w1, w2 and b. My code is as below:
x1 = [0.0, 2.0]
y1 = 1.0
x2 = [1.0, 3.0]
y2 = 1.0
x3 = [1.0, 0.0]
y3 = 0.0
x4 = [2.0, 1.0]
y4 = 0.0
X = np.array([x1, x2, x3, x4])
Y = np.array([y1, y2, y3, y4])

feature = torch.from_numpy(X)
label = torch.from_numpy(Y).view(-1, 1)

epochs = 100
lr = 0.01
weight = torch.randn((2, 1), requires_grad=True, dtype=torch.float64)
bias = torch.randn(1, requires_grad=True, dtype=torch.float64)

for i in range(epochs):
    predict = torch.mm(feature, weight) + bias.item()
    loss = torch.sum(predict - label, dim=0)
    loss.backward()
    weight = weight - weight.grad*lr
    bias = bias - bias*lr
    weight.grad.zero_()
    bias.grad.zero_()

Error occurs at line weight.grad.zero_() :
AttributeError: 'NoneType' object has no attribute 'zero_'

I want to know how to fix it? Thanks