```
def linear_regression_func(inputs,targets,iteration,learning_rate):
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
#initializing weights and bias
w = torch.randn(2,3, requires_grad=True)
b = torch.randn(2, requires_grad= True)
for i in range(iteration):
predicts = model(inputs)
loss = mse(targets,predicts)
loss.backward()
with torch.no_grad():
w -= w.grad * learning_rate
b -= b.grad * learning_rate
w.grad.zero_()
b.grad.zero_()
return w,b
```

**Does anyone know, why w.grad is giving NoneType while applying it inside a function? This exact same code will run as expected if it is not implemented through function.**

**error message:**

```
16 with torch.no_grad():
---> 17 w -= w.grad * learning_rate
18 b -= b.grad * learning_rate
19 w.grad.zero_()
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
```