.Backward() error

Gabriele_Furnari: <p>Hi everyone. my code is:<br>
class CNN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(CNN, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3)<br>
self.pool1 = nn.MaxPool2d(kernel_size=2)<br>
self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=3)<br>
self.pool2 = nn.MaxPool2d(kernel_size=2)<br>
self.conv3 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3)<br>
self.pool3 = nn.MaxPool2d(kernel_size=2)<br>
self.conv4 = nn.Conv2d(in_channels=24, out_channels=48, kernel_size=3)<br>
self.pool4 = nn.MaxPool2d(kernel_size=2)<br>
self.conv5 = nn.Conv2d(in_channels=48, out_channels=96, kernel_size=3)<br>
self.fc1 = nn.Linear(in_features=55296, out_features=512)<br>
self.fc2 = nn.Linear(in_features=512, out_features=128)<br>
self.fc3 = nn.Linear(in_features=128, out_features=12)<br>
def forward(self, x):<br>
x = self.conv1(x)<br>
x = F.relu(x)<br>
x = self.pool1(x)<br>
x = self.conv2(x)<br>
x = F.relu(x)<br>
x = self.pool2(x)<br>
x = self.conv3(x)<br>
<span class="hashtag">#x</span> = F.relu(x)<br>
x = self.pool3(x)<br>
x = self.conv4(x)<br>
x = self.pool4(x)<br>
x = self.conv5(x)<br>
<span class="hashtag">#print</span>(x.shape)<br>
x = torch.flatten(x, start_dim=0)<br>
x = self.fc1(x)<br>
<span class="hashtag">#print</span>(x)<br>
<span class="hashtag">#print</span>(x.shape)<br>
<span class="hashtag">#x</span> = F.relu(x)<br>
<span class="hashtag">#x</span> = self.fc2(x)<br>
<span class="hashtag">#x</span> = F.relu(x)<br>
<span class="hashtag">#x</span> = self.fc3(x)<br>
<span class="hashtag">#print</span>(x)<br>
return x.unsqueeze(0).unsqueeze(0)<br>
class SGRNN(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size):<br>
super(SGRNN, self).<strong>init</strong>()<br>
self.hidden_size = hidden_size<br>
self.rnn = nn.RNN(input_size, hidden_size, num_layers=1, batch_first=True)<br>
self.fc = nn.Linear(hidden_size, output_size)<br>
def forward(self, input, h):<br>
out, h = self.rnn(input, h)<br>
<span class="hashtag">#print</span>(out.shape)<br>
out1 = self.fc(out)<br>
<span class="hashtag">#print</span>(out1.shape)<br>
return out1<br>
window = 3</p>
<p>input_size = 524*window<br>
hidden_size = 200<br>
num_layers = 1<br>
output_size = 12</p>
<p>rnn_model = SGRNN(input_size, hidden_size)<br>
cnn_model = CNN()<br>
<span class="hashtag">#resnet_model</span> = ResNet18()</p>
<p><span class="hashtag">#train</span></p>
<p><span class="hashtag">#optimizer</span> = optim.Adam(list(cnn_model.parameters()) + list(rnn_model.parameters()), lr=0.01) #–&gt; backward problem</p>
<p>optimizer = optim.Adam(rnn_model.parameters(), lr = 0.01)</p>
<p>num_epochs = 100<br>
best_loss = float(‘inf’)</p>
<p>rnn_model.train()<br>
cnn_model.train()</p>
<p>for epoch in range(num_epochs):</p>
<p>tot_loss = 0</p>
<p>for i in range(len(x_train)):<br>
hidden = torch.zeros(1, 1, hidden_size)</p>
<pre><code>loss = 0

output = torch.zeros(1, window, output_size)

output =  torch.cat((output, y_train[i][1][0].unsqueeze(0).unsqueeze(0)),dim=1)    
outputs = output

prev_features = torch.zeros(1, window, 512)
prev_features = torch.cat((prev_features, cnn_model(x_train[i][1][0])), dim=1)

seq_loss = 0
for j in range(len(x_train[i][1])-1):
  with torch.autograd.detect_anomaly(): 
    optimizer.zero_grad()

    features = cnn_model(x_train[i][1][j].clone())
    prev_features = torch.cat((prev_features, features), dim=1)

    if j &lt; window-1:  
      input = torch.cat((torch.zeros(1,window-j-1,512), prev_features[:,0:j+1,:]),dim=1)
      input = torch.cat((outputs[:,-window:,:], input),dim=2)
      input = torch.flatten(input, start_dim= 0) 
    else:
      input = torch.cat((outputs[:,-window:,:], prev_features[:, -window:,:]),dim=2)
      input = torch.flatten(input, start_dim = 0)

    target = y_train[i][1][j+1].detach()
    
    
    output = rnn_model(input.unsqueeze(0).unsqueeze(0), hidden)

    outputs = torch.cat((outputs, target.unsqueeze(0).unsqueeze(0)), dim=1)
    #outputs = torch.cat((outputs, output), dim=1)

    loss = F.l1_loss(output, target.unsqueeze(0).unsqueeze(0))
    
    '''if epoch % 5 == 0 and epoch != 0:
      print('target: ', target)
      print('output: ', output)'''
    
    
    
    seq_loss += loss
    loss.backward()
    #torch.nn.utils.clip_grad_norm_(list(cnn_model.parameters()) + list(rnn_model.parameters()), 1.0)
    optimizer.step()
  
seq_loss /= len(x_train[i][1])
tot_loss += seq_loss
</code></pre>
<p>print(“Epoch [{}/{}], Loss: {:.4f}”.format(epoch+1, num_epochs, tot_loss/len(x_train)))</p>
<p>when i run it, i get the error ‘RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.’.</p>
<p>Using with torch.autograd.detect_anomaly(): it tells me thee problem is line 136 which is ‘features = cnn_model(x_train[i][1][j].clone())’. i tried different things but the error still remains.</p>
<p>Can someone help me?? thank you very much to everyone.</p>