I am trying to minimize the results of some operations that involve the output of a model inside my training loop.
First I use the outputs of the model to compute a set of equations. Then I compute new values base on the equation results. I calculate the loss per these new values and sum it to send it to the backward ()
For some reason, I get the following error: element 0 of tensors does not require grad and does not have a grad_fn
Can anyone spot what I am doing wrong? I am doing a forbidden operation inside training loop?
Here is an example of what I am doing:
x = {'input data with multiple columns'}
y = {'targets'}
t = {'values useful for futur computation'}
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.linear_init = nn.Linear(in_features = 9, out_features = 20)
self.linear1 = nn.Linear(in_features = 20, out_features = 3)
self.linear2 = nn.Linear(in_features = 20, out_features = 3)
self.linear3 = nn.Linear(in_features = 20, out_features = 3)
def forward(self, x):
x = torch.relu(self.linear_init(x))
output1 = self.linear1(x)
output2 = self.linear2(x)
output3 = self.linear3(x)
return output1, output2, output3
model = NeuralNetwork()
def formula1(param1,param2,parm3):
resolve = {'equation that involves params and some columns of the input data'}
return resolve
def formula2(param1,param2,parm3):
resolve = {'equation that involves params and some columns of the input data'}
return resolve
def formula3(param1,param2,parm3):
resolve = {'equation that involves params and some columns of the input data'}
return resolve
criterion = nn.MSELoss(reduction='mean')
optimiser = nn.optim.SGD(model.parameters(), lr=1e-5)
input_tensor = torch.from_numpy(x).type(torch.Tensor)
target_tensor = torch.from_numpy(y).type(torch.Tensor)
step = torch.from_numpy(t).type(torch.Tensor)
n= len(input_tensor)
for t in range(10):
val1=0
val2=0
val3=0
res1 = torch.zeros(n, 1)
res2 = torch.zeros(n, 1)
res3 = torch.zeros(n, 1)
out1, out2, out3 = model(input_tensor)
inter1 = formula1(out1[:,0], out2[:,0], out3[:,0])
inter2 = formula2(out1[:,1], out2[:,1], out3[:,1])
inter3 = formula3(out1[:,2], out2[:,2], out3[:,2])
for i in range(len(input_tensor)):
#there is a condition on the value when i=0 and these are really arbitrary operations, just an example!
A=((step[i]-step[i-1])/(inter1[i]+inter1[i-1]))*0.3
B=((step[i]-step[i-1])/(inter2[i]+inter2[i-1]))*0.3
C=((step[i]-step[i-1])/(inter3[i]+inter3[i-1]))*0.3
val1 = val1 + A
val2 = val2 + B
val3 = val3 + C
res1[i] = val1
res2[i] = val2
res3[i] = val3
loss1 = criterion(res1, 'a column of taget tensor')
loss2 = criterion(res2, 'a column of taget tensor')
loss3 = criterion(res3, 'a column of taget tensor')
cum_loss = loss1+loss2+loss3
optimiser.zero_grad()
cum_loss.backward()
optimiser.step()