optimizer.step() Not updating Model Weights/Parameters

username: Marko
cooked: <p>I’m currently working on a solution via PyTorch. I’m not going to share the exact solution but I will provide code that reproduces the issue I’m having.</p>
<p>I have a model defined as follows:</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.fc1 = nn.Linear(10,4)

    def foward(self,x):
        return nn.functional.relu(self.fc1(x))
</code></pre>
<p>Then I create a instance: <code>my_model = Net()</code>. Next I create an <code>Adam</code> optimizer as such:</p>
<pre><code class="lang-auto">optim = Adam(my_model.parameters())

# create a random input
inputs = torch.tensor(np.array([1,1,1,1,1,2,2,2,2,2]),dtype=torch.float32,requires_grad=True)

# get the outputs
outputs = my_model(inputs)

# compute gradients / backprop via
outputs.backward(gradient=torch.tensor([1.,1.,1.,5.]))

# store parameters before optimizer step
before_step = list(my_model.parameters())[0].detach().numpy()

# update parameters via
optim.step()

# collect parameters again
after_step = list(my_model.parameters())[0].detach().numpy()

# Print if parameters are the same or not
print(np.array_equal(before_step,after_step)) # Prints True
</code></pre>
<p>I provided my models parameters to the Adam optimizer, so I’m not exactly sure why the parameters aren’t updating. I know in most cases one uses a loss function, however I cannot do that in my case but I assumed if I specified model paramters to the optimizers, it would know to connect the two.</p>
<p>Anyone know why the parameters aren’t getting updated?</p>

username: ptrblck
cooked: <p>You need to <code>.clone()</code> the parameters as otherwise you would still compare the references.</p>

username: Chihoub_Chiheb_Eddin
cooked: <p>how exactly should we use it? my model also does not update its parameters once load it, always has an error (optim.step() needs args and kwargs)</p>

username: ptrblck
cooked: <p>Could you post the full stacktrace and a minimal and executable code snippet to reproduce the issue?</p>

username: Chihoub_Chiheb_Eddin
cooked: <p>of course, here you are.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/6/9612a4261d15b95f6dd52bb55be890601108e9c1.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/9612a4261d15b95f6dd52bb55be890601108e9c1" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/6/9612a4261d15b95f6dd52bb55be890601108e9c1_2_626x500.jpeg" alt="error" data-base62-sha1="lpByFrrHSG0Lw7piH3Er5NnzyGl" width="626" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/6/9612a4261d15b95f6dd52bb55be890601108e9c1_2_626x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/9/6/9612a4261d15b95f6dd52bb55be890601108e9c1_2_939x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/6/9612a4261d15b95f6dd52bb55be890601108e9c1.jpeg 2x" data-dominant-color="505151"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error</span><span class="informations">1237×987 223 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p><code>torch.optim.Adam</code> does not require any input arguments to its <code>step</code> method:</p>
<pre data-code-wrap="python"><code class="lang-python">model = nn.Linear(1, 1)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)

model(torch.randn(1, 1)).mean().backward()
optimizer.step()
</code></pre>
<p>so I guess you might be using another implementation?</p>