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
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??
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