'step_size_min' referenced before assignment

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.

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() #<--- 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 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

Maybe you have done something wrong in Net definition. and it does not have any parameter.

check model.parameters() output.

1 Like

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

what is Net()?
can you replace it with

import torch.nn as nn
model = nn.Sequential(nn.Linear(2,6),
                      nn.Sigmoid(),
                      nn.Linear(6,1),
                      nn.Sigmoid())

and see if it changes anything.

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.

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…

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

What I am trying to do is create a NN with two hidden layers. Each with six hidden neurons. The activation function used is a sigmoid
function in the hidden neurons, and a linear activation
function in the output neuron. Think that’s about right??

Thanks again for your help!!

i see nothing wrong in it.
and i can’t reproduce the error.

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)

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

Found the issue…

layer.weight = torch.nn.Parameter(torch.from_numpy(temp))

should have been

layer.weight.data = torch.nn.Parameter(torch.from_numpy(temp))

Hope this helps someone in the future :slight_smile:

You disappoint me Luke.

1 Like

.data is never a good idea :wink:
In this case, this would be better:

with torch.no_grad():
  layer.weight.copy_(torch.from_numpy(temp))