在写代码过程中遇到了RuntimeError: Trying to backward through the graph a second time 问题。我在网络上没有找到合适的解决方案,并且我的原始工程代码比较复杂,难以直接询问chatgpt。为了更方便debug,我写了一个简单的代码复现这个错误。
import torch
import torch.nn as nn
import torch.optim as optim
class A(nn.Module):
def __init__(self):
super(A, self).__init__()
self.fc = nn.Linear(512, 512)
self.v_image = nn.Parameter(torch.randn(1, 512))
self.fuse_image = []
def forward(self, x):
out = self.fc(x)
t = torch.matmul(out, self.v_image.T)
self.fuse_image.append(t)
return out, self.fuse_image
model = A()
criterion = nn.L1Loss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
for i in range(50):
optimizer.zero_grad()
x = torch.randn(2, 512)
out, fuse_image = model(x)
y = torch.mean(fuse_image[0])
loss = criterion(y, torch.tensor(1.1))
loss.backward()