Hi! I got some errors from my hybrid model code.
From the result of loss value between target and prediction, I checked that the forward process in my model has no problem.
However, the error from the backward process has occured.
The code for my hybrid model is as below:
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv1d(1, 8, 30, 2)
self.conv2 = nn.Conv1d(8, 16, 20, 2)
self.conv3 = nn.Conv1d(16, 32, 10, 2)
self.dropout = nn.Dropout1d()
self.fc1 = nn.Linear(160, 64)
self.fc2 = nn.Linear(64, 16)
def forward(self, x):
x = nn.functional.tanh(self.conv1(x))
x = nn.functional.tanh(self.conv2(x))
x = nn.functional.tanh(self.conv3(x))
x = self.dropout(x)
x = x.view(1, -1)
x = nn.functional.tanh(self.fc1(x))
x = self.fc2(x)
x = qlayer(x)
#print("output of qlayer:", x)
#return torch.cat((x, 1 - x), -1)
return x
RuntimeError Traceback (most recent call last)
Cell In[17], line 16
13 train_loader = DataLoader(tr_dataset,batch_size=1,shuffle=True,drop_last=True)
14 test_loader = DataLoader(te_dataset,batch_size=1,shuffle=False,drop_last=False)
—> 16 loss_list_train = train(train_loader=train_loader, epochs=epochs)
18 train_loss_df = pd.DataFrame(loss_list_train)
19 train_loss_df.to_csv(‘F:/Student/CJG/220916_QC/3_IBM_QLAB/2_result/0_hybrid/1_loss/Train_loss(ROI_’+str(ROI_info)+‘).csv’, index=False, header=None)
Cell In[16], line 16, in train(epochs, train_loader)
14 print(“loss:”,loss)
15 # Backward pass
—> 16 loss.backward()
17 # Optimize the weights
18 optimizer.step()
File ~\anaconda3\envs\pennylane\lib\site-packages\torch_tensor.py:488, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)
478 if has_torch_function_unary(self):
479 return handle_torch_function(
480 Tensor.backward,
481 (self,),
(…)
486 inputs=inputs,
487 )
→ 488 torch.autograd.backward(
489 self, gradient, retain_graph, create_graph, inputs=inputs
490 )
File ~\anaconda3\envs\pennylane\lib\site-packages\torch\autograd_init_.py:197, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
192 retain_graph = create_graph
194 # The reason we repeat same the comment below is that
195 # some Python versions print out the first line of a multi-line function
196 # calls in the traceback and some print out the last line
→ 197 Variable.execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
198 tensors, grad_tensors, retain_graph, create_graph, inputs,
199 allow_unreachable=True, accumulate_grad=True)
RuntimeError: function ExecuteTapesBackward returned a gradient different than None at position 26, but the corresponding forward input was not a Variable
Has anyone encountered a similar error?