'step_size_min' referenced before assignment

priceluke: <p>I am using the Rprop optimizer and MSE for my loss function but am finding zero_grad isn’t setting the values for grad to zero for the network and the MSE.backward() doesn’t update the values either. I must be doing something very silly.</p>
<pre><code class="lang-auto">model = Net(feature_number=2, hidden_number=6, output_number=1)
search_method = torch.optim.Rprop(model.parameters(), lr=0.02)
## ommitted
def local_search(weights, network, loss_function, search_method , iterations, x_train, y_train, approach=LEARNING_METHOD):
    """ Perform local search for a given number of iterations """
    weights_into_network(network, weights)
    for i in range(iterations):
        output = network(x_train)
        loss = loss_function(output, y_train)
        search_method .zero_grad()
        loss.backward()
        search_method .step() #&lt;--- here thrown
        learned_loss = loss.item() 
## ommitted
</code></pre>
<pre><code class="lang-auto">  File "C:\Users\user\nn_ga\main.py", line 381, in local_search
    search_method.step()
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\optim\optimizer.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\optim\rprop.py", line 109, in step
    step_size_min=step_size_min,
UnboundLocalError: local variable 'step_size_min' referenced before assignment
</code></pre>
<pre><code class="lang-auto">Versions of relevant libraries:
[pip3] numpy==1.21.1
[pip3] torch==1.10.1+cu102
[pip3] torchaudio==0.10.1+cu102
[pip3] torchvision==0.11.2+cu102
</code></pre>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/70352">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/70352" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/70352" target="_blank" rel="noopener nofollow ugc">Rprop Optimizer: UnboundLocalError: local variable 'step_size_min' referenced before assignment</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-12-23" data-time="16:11:58" data-timezone="UTC">04:11PM - 23 Dec 21 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/priceluke" target="_blank" rel="noopener nofollow ugc">
          <img alt="priceluke" src="https://avatars.githubusercontent.com/u/44372546?v=4" class="onebox-avatar-inline" width="20" height="20">
          priceluke
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: optimizer
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

When using the optimiser's step method, I get an error r<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">egarding step minimums. I believe this value should default to 1e-6 if not explicitly set via params.

```
model = Net(feature_number=2, hidden_number=6, output_number=1)
search_method = torch.optim.Rprop(model.parameters(), lr=0.02)
## ommitted
def local_search(weights, network, loss_function, search_method , iterations, x_train, y_train, approach=LEARNING_METHOD):
    """ Perform local search for a given number of iterations """
    weights_into_network(network, weights)
    for i in range(iterations):
        output = network(x_train)
        loss = loss_function(output, y_train)
        search_method .zero_grad()
        loss.backward()
        search_method .step() #&lt;--- here thrown
        learned_loss = loss.item() 
## ommitted
```

```
  File "C:\Users\user\nn_ga\main.py", line 381, in local_search
    search_method.step()
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\optim\optimizer.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\optim\rprop.py", line 109, in step
    step_size_min=step_size_min,
UnboundLocalError: local variable 'step_size_min' referenced before assignment
```


### Versions

Versions of relevant libraries:
[pip3] numpy==1.21.1
[pip3] torch==1.10.1+cu102
[pip3] torchaudio==0.10.1+cu102
[pip3] torchvision==0.11.2+cu102

cc @vincentqb @jbschlosser @albanD</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


mMagmer: <p>Maybe you have done something wrong in <code>Net</code> definition. and it does not have any parameter.</p>
<aside class="quote no-group" data-username="priceluke" data-post="1" data-topic="140275">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/5e9695/48.png" class="avatar"> priceluke:</div>
<blockquote>
<p><code>model = Net(feature_number=2, hidden_number=6, output_number=1)</code></p>
</blockquote>
</aside>
<p>check <code>model.parameters()</code> output.</p>

priceluke: <p>I traced the model.parameters() output during the execution of the program and it refers to the same gi_code throughout :’(</p>

mMagmer: <p>what is <code>Net()</code>?<br>
can you replace it with</p>
<pre><code class="lang-auto">import torch.nn as nn
model = nn.Sequential(nn.Linear(2,6),
                      nn.Sigmoid(),
                      nn.Linear(6,1),
                      nn.Sigmoid())
</code></pre>
<p>and see if it changes anything.</p>

priceluke: <aside class="quote no-group" data-username="mMagmer" data-post="4" data-topic="140275">
<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/mmagmer/48/43283_2.png" class="avatar"> mMagmer:</div>
<blockquote>
<pre><code class="lang-auto">nn.Sequential(nn.Linear(2,6),
                      nn.Sigmoid(),
                      nn.Linear(6,1),
                      nn.Sigmoid())
</code></pre>
</blockquote>
</aside>
<p>Hey, thanks for your help. The file is a few hundred lines long and does a lot with the model object which breaks if I change it to this sequential method, mostly regarding getting weights into the network. I will try and get it to work with this method but think I have narrowed the problem down to the loss.backwards() not updating the gradients being the problem.</p>

priceluke: <p>I’ve rewritten the bits that broke, and now it is running without error. Will paste my Net class as can’t pin down the cause…</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self, feature_number, hidden_number, output_number):
        super(Net, self).__init__()
        self.hidden1 = nn.Linear(feature_number, hidden_number)
        self.hidden2 = nn.Linear(hidden_number, hidden_number)
        self.out = nn.Linear(hidden_number, output_number)

    def forward(self, x):
        x = self.hidden1(x)
        x = self.hidden2(x)
        x = torch.sigmoid(x)
        x = self.out(x)
        return x
</code></pre>
<p>What I am <em>trying</em> to do is create a NN with two hidden layers. Each with six hidden neurons. The activation function used is a sigmoid<br>
function in the hidden neurons, and a linear activation<br>
function in the output neuron. Think that’s about right??</p>
<p>Thanks again for your help!!</p>

mMagmer: <p>i see nothing wrong in it.<br>
and i can’t reproduce the error.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

class Net(nn.Module):
    def __init__(self, feature_number, hidden_number, output_number):
        super(Net, self).__init__()
        self.hidden1 = nn.Linear(feature_number, hidden_number)
        self.hidden2 = nn.Linear(hidden_number, hidden_number)
        self.out = nn.Linear(hidden_number, output_number)

    def forward(self, x):
        x = self.hidden1(x)
        x = self.hidden2(x)
        x = torch.sigmoid(x)
        x = self.out(x)
        return x



model = Net(feature_number=2, hidden_number=6, output_number=1)


eta = torch.nn.functional.softmax(10*torch.randn(1000,2),-1)
dl = torch.utils.data.DataLoader(eta,batch_size=200,shuffle=True)



optim = torch.optim.Rprop(model.parameters(), lr=0.02)



for i in range(10):
    totalLoss = 0
    for x in dl:
        optim.zero_grad()
        out=model(x)
        loss=((out-1)**2).mean()
        loss.backward()
        optim.step()
        totalLoss += loss.item()
    print(i,totalLoss)
</code></pre>

priceluke: <p>hmm so when i rewrote how i was putting the weights into the network it started working. however, I’m not actually sure why the previous method didn’t work. I’ve pasted both methods into a gist if you wouldn’t mind taking a look</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/priceluke/3086ebb7c2e286eb74d52608bf8c8526">
  <header class="source">

      <a href="https://gist.github.com/priceluke/3086ebb7c2e286eb74d52608bf8c8526" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/priceluke/3086ebb7c2e286eb74d52608bf8c8526" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/priceluke/3086ebb7c2e286eb74d52608bf8c8526</a></h4>

  <h5>weights into network</h5>
  <pre><code class="">def my_weights_into_network(network, weights):
    # # hidden1 = 6,2
    # # hidden1.bias 6,
    # # hidden2 = 6,6
    # # hidden2.bias = 6,
    # # out = 1,6
    # # out.bias = 1,
    weights = np.asarray(weights)

    # print(torch.nn.Parameter(torch.from_numpy(weights.reshape(6, 2))))</code></pre>
    This file has been truncated. <a href="https://gist.github.com/priceluke/3086ebb7c2e286eb74d52608bf8c8526" target="_blank" rel="noopener nofollow ugc">show original</a>

<p>
</p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


priceluke: <p>Found the issue…</p>
<pre><code class="lang-auto">layer.weight = torch.nn.Parameter(torch.from_numpy(temp))
</code></pre>
<p>should have been</p>
<pre><code class="lang-auto">layer.weight.data = torch.nn.Parameter(torch.from_numpy(temp))
</code></pre>
<p>Hope this helps someone in the future <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

FrankGuerin: <p>You disappoint me Luke.</p>

albanD: <p><code>.data</code> is never a good idea <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=10" title=":wink:" class="emoji" alt=":wink:"><br>
In this case, this would be better:</p>
<pre><code class="lang-auto">with torch.no_grad():
  layer.weight.copy_(torch.from_numpy(temp))
</code></pre>