Params used in chapter 4 not working

lr = 1e-5
params.data -= lr * params.grad.data
params.grad = None

the compiler shows error that "AttributeError: ‘NoneType’ object has no attribute ‘data’ "

also what is params. data & params.grad.data

plz help

Parameters are the actual neural network layer elements (aka Weights)

In chapter 4, it is allocate in:

Step 1: Initialize the parameters

First, we initialize the parameters to random values, and tell PyTorch that we want to track their gradients, using requires_grad_:
In [ ]:
params = torch.randn(3).requires_grad_()

  • All the cells need to be re-run in order if you restart the notebook

thanks @meanpenguin but I tried to rerun cells 2-3 times but I get the same error & also can’t find anything about this param.data.grad , really this is confusing !

This works without and error

from fastai.vision.all import *

time = torch.arange(0,20).float(); time
speed = torch.randn(20)*3 + 0.75*(time-9.5)**2 + 1

def f(t, params):
    a,b,c = params
    return a*(t**2) + (b*t) + c
def mse(preds, targets): return ((preds-targets)**2).mean().sqrt()

params = torch.randn(3).requires_grad_()
orig_params = params.clone()
preds = f(time, params)
loss = mse(preds, speed)
loss.backward()

lr = 1e-5
params.data -= lr * params.grad.data
params.grad = None

`
Params is a Torch object (pytorch.org)
so params.grad is Tensor.grad
https://pytorch.org/docs/stable/autograd.html#tensor-autograd-functions

hey thanks @meanpenguin for help but one more thing how you generated speed formula
speed = torch.randn(20)3 + 0.75(time-9.5)**2 + 1

Hi,

Everything is copied directly from the chapter 4 of the book.