Parametres not updating after optimizer.step()

isaka_lorry: <p>Hi, I read many articles in ‘<a href="http://discuss.pytorch.org">discuss.pytorch.org</a>’ community on why parameters are not getting updated. I found many similar challenges like - <a href="https://discuss.pytorch.org/t/optimizer-step-not-updating-correctly/71315" class="inline-onebox">Optimizer.step() not updating correctly</a>. But, I didn’t know where my network went wrong? Training is not happening in my network. Any inputs and suggestions please.<br>
I am working on JupyNotebook in Conda env.</p>
<p>from sklearn import datasets                 # to get regression data for model<br>
import torch<br>
from torch.nn import Linear, Sequential, Flatten, MSELoss<br>
from torch.optim import SGD, Adam, LBFGS     # stochastic optimizer and gradient descent optimizer<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
X = torch.FloatTensor(120, 1).uniform_(0, 2)<br>
Y = 0.3<em>X.pow(3)-0.2</em>X.pow(2)+0.6*X.pow(1)+0.9<br>
degree = 3<br>
p = torch.linspace(0, degree , degree + 1 )<br>
XX = X.pow(p)<br>
model = Sequential(<br>
Linear(degree + 1, 1),<br>
Flatten(0,1)<br>
)<br>
critereon = MSELoss()<br>
optim = Adam(model.parameters(), lr=0.1)<br>
no_of_epochs = 800    # number of iterations that has to be done<br>
for epoch in range(no_of_epochs):<br>
pred = model(XX).unsqueeze(1)<br>
loss = critereon(pred, Y)<br>
loss.backward<br>
optim.step()<br>
optim.zero_grad()<br>
if epoch % 300 == 0:<br>
print('epoch : ', epoch , ‘loss :’, loss.item())<br>
print('polynomial_coefficients : ', list(model.parameters())[0].data.numpy())<code>Preformatted text</code></p>

KFrank: <p>Hi Surya!</p>
<aside class="quote no-group quote-modified" data-username="isaka_lorry" data-post="1" data-topic="152630" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/isaka_lorry/48/49972_2.png" class="avatar"> isaka_lorry:</div>
<blockquote>
<p>parameters are not getting updated.<br>
…<br>
loss.backward</p>
</blockquote>
</aside>
<p>As posted, your code – <code>loss.backward</code> – doesn’t actually <em>call</em> the<br>
<code>.backward()</code> method of the <code>loss</code> <code>Tensor</code>.</p>
<p>Try <code>loss.backward()</code>.  (Note the parentheses.)</p>
<p>Also, check whether your gradients are not <code>None</code> and are non-zero<br>
before calling <code>optim.step()</code>, e.g., <code>print (model[0].weight.grad)</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

isaka_lorry: <p>Thanks <a class="mention" href="/u/kfrank">@KFrank</a> . It worked for me. network is training now. I wonder why it didn’t throw me an error when I missed the paranthesis at loss.backward.</p>

KFrank: <p>Hi Surya!</p>
<aside class="quote no-group" data-username="isaka_lorry" data-post="3" data-topic="152630" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/isaka_lorry/48/49972_2.png" class="avatar"> isaka_lorry:</div>
<blockquote>
<p>I wonder why it didn’t throw me an error when I missed the paranthesis at loss.backward.</p>
</blockquote>
</aside>
<p>This is a python thing that is not specific to pytorch.</p>
<p>At issue is that functions and function-like things are first-class objects<br>
in python.  The expression <code>loss.backward</code> evaluates to the <code>backward</code><br>
method of your <code>loss</code> tensor, but doesn’t call that method.  Consider:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
print (torch.__version__)

def twoX (x):
    return 2 * x

print ('twoX:', twoX)                 # a python function is a "first-class" object
print ('type (twoX):', type (twoX))
print ('twoX (3):', twoX (3))         # evaluates twoX (3)

func = twoX                           # python variables can refer to functions
print ('func:', func)
print ('func (3):', func (3))         # evaluates func (3), which is twoX (3)

# loss.backward is a method of Tensor
# it is a callable object, similar to a function

t = torch.tensor ([5.0], requires_grad = True)
loss = t.pow (2)
print ('t.grad:', t.grad)                               # t.grad starts out None
print ('loss.backward:', loss.backward)                 # does not call loss.backward
print ('type (loss.backward):', type (loss.backward))   # it's a method, rather than a function
print ('t.grad:', t.grad)                               # t.grad is still None
callable = loss.backward                                # assign loss.backward to a variable
print ('t.grad:', t.grad)                               # t.grad is still None
print ('callable():', callable())                       # call loss.backward() (returns None)
print ('t.grad:', t.grad)                               # now t.grad has been computed
</code></pre>
<p>And its output:</p>
<pre><code class="lang-plaintext">1.10.2
twoX: &lt;function twoX at 0x00000279BB1797B8&gt;
type (twoX): &lt;class 'function'&gt;
twoX (3): 6
func: &lt;function twoX at 0x00000279BB1797B8&gt;
func (3): 6
t.grad: None
loss.backward: &lt;bound method Tensor.backward of tensor([25.], grad_fn=&lt;PowBackward0&gt;)&gt;
type (loss.backward): &lt;class 'method'&gt;
t.grad: None
t.grad: None
callable(): None
t.grad: tensor([10.])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

isaka_lorry: <p><a class="mention" href="/u/kfrank">@KFrank</a> Thanks. I understood it. I am thankful for the time you have put into this.</p>