Different between torch.tensor and torch.tensor.data?

while running this code

for e in range(10): 
  y_ = linear_model(x_train)
  loss = get_loss(y_, y_train)
  w.grad.zero_() 
  b.grad.zero_() 
  loss.backward()
  
  w = w - 1e-2 * w.grad 
  b = b - 1e-2 * b.grad 

  print('epoch: {}, loss: {}'.format(e, loss.item()))

my notebook raise an error that the w and b’s grad is None

and in this code

w.data = w.data - 1e-2 * w.grad.data 
b.data = b.data - 1e-2 * b.grad.data

the grad didn’t disappear.
why former one doesn’t work?Thanks for anyone pointing out the reason.

The error is raised, since the .grad attribute is set to None be default. The first backward() call populates the .grad attribute of all parameters used in the computation of the loss, which require gradients.
If you gut w.grad.zero_() after the loss.backward() call, the error should disappear.

Also, have a look at this example to see how to manually update the parameters. E.g. in your example the torch.no_grad() guard is missing.