hi. i want to compute the gradient with pytorch autograd in a loop (because i have may training set):


dataloader_train = DataLoader(dataset_train, batch_size=4, shuffle=True)
#b=torch.tensor([0.7],dtype=torch.float32, requires_grad=True)
b=Variable(torch.tensor([0.7]), requires_grad=True)
dataloader_train = DataLoader(dataset_train, batch_size=4, shuffle=True)
#w=torch.tensor([0.3,-0.8], dtype=torch.float32, requires_grad=True)
w=Variable(torch.tensor([0.3,-0.8]),requires_grad=True)
alpha=0.01
epochs=1
for epoch in range(epochs):
for i_batch, (x_batch,y_batch) in enumerate(dataloader_train):
print(“i_batch=”,i_batch)
for i in range(4):
w.grad.data.zero_()
b.grad.data.zero_()
print(“i_iteration=”,i)
current_train=x_batch[i,:]
print(“current_train=”,current_train)
print(“old_w=”,w)
z=torch.sum(torch.mul(current_train,w))+b
print(“z=”,z)
y_hat=torch.sigmoid(z)
#y_hat=l(w)
#print(“y_hat_vor round=”,y_hat)
#y_hat=torch.round(y_hat)
print(“y_hat after round=”,y_hat)
print(“has w grad=”,w.requires_grad)
print(“has b grad=”,b.requires_grad)
y_hat.backward()
y_hat_w_grad=w.grad.data.detach().clone()
y_hat_b_grad=b.grad.data.detach().clone()
print(“y_hat_w_grad=”,y_hat_w_grad)
print(“y_hat_b_grad=”,y_hat_b_grad)
print(“y_batch=”,y_batch[i])
w=w+(alpha*(-1)y_hat_w_grad)
b=b+(alpha(-1)*y_hat_b_grad)
print(“new_w=”,w)
print(w)


Basicaly it compute the gradient hust for the first loop.
for the second loop, it show this error :


AttributeError                            Traceback (most recent call last)
 in 
33             print(“has b grad=”,b.requires_grad)
34             y_hat.backward()
—> 35             y_hat_w_grad=w.grad.data.detach().clone()
36
37             y_hat_b_grad=b.grad.data.detach().clone()
AttributeError: ‘NoneType’ object has no attribute ‘data’


please what i am doing wrong or how can i solve it.
thanks