Pytorch 当我使用clone()时,仍然存在“就地”操作错误
我尝试在任何我认为必要的地方添加Pytorch 当我使用clone()时,仍然存在“就地”操作错误,pytorch,Pytorch,我尝试在任何我认为必要的地方添加.clone() 但我仍然得到了就地操作错误 这个错误发生在第二个纪元,而不是第一个纪元,当我得到这个错误的时候 class Model(torch.nn.Module): def __init__(self, user_M, item_M, feature_M, embedding_M): super(Model, self).__init__() self.dimension = embedding_M + featur
.clone()
但我仍然得到了就地操作
错误
这个错误发生在第二个纪元,而不是第一个纪元,当我得到这个错误的时候
class Model(torch.nn.Module):
def __init__(self, user_M, item_M, feature_M, embedding_M):
super(Model, self).__init__()
self.dimension = embedding_M + feature_M + 1
# Memories
self.user_memory = torch.randn(user_M, 2, 1, self.dimension)
self.item_memory = torch.randn(item_M, 2, 1, self.dimension)
# Writer
self.user_LSTM = nn.LSTMCell(self.dimension, self.dimension)
self.item_LSTM = nn.LSTMCell(self.dimension, self.dimension)
def score(self, user_id, item_id):
user_em = torch.index_select(self.user_memory[:, 1, :].clone(), 0, user_id[:,0].clone())
item_em = torch.index_select(self.item_memory[:, 1, :].clone(), 0, item_id[:,0].clone())
return torch.sigmoid(torch.bmm(user_em, item_em.permute(0,2,1)))
def forward(self, train_data, user_id, item_id):
ui_train = train_data[:, 0:1].type(torch.LongTensor).clone()
ii_train = train_data[:, 1:2].type(torch.LongTensor).clone()
for row in range(len(train_data)):
memory = self.user_memory[ui_train[row][0]].clone()
h, c = self.user_LSTM(train_data[row:row+1].clone(), (memory[0], memory[1]))
self.user_memory[ui_train[row]] = torch.stack((h, c))
for row in range(len(train_data)):
memory = self.item_memory[ii_train[row][0]].clone()
h, c = self.item_LSTM(train_data[row:row+1].clone(), (memory[0], memory[1]))
self.item_memory[ii_train[row]] = torch.stack((h, c))
# Scoring
y = self.score(user_id, item_id)
return y.reshape(-1, 1)
尝试使用-
torch.autograd.set\u detect\u normal(True)
-对我来说,最可能的罪魁祸首似乎是就地分配self.user\u memory[ui\u train[row]=…
和self.user\u memory[ii\u train[row]=…
。尝试使用-torch.autograd.set\u detect\u normal(True)查找原因
-在我看来,最有可能的罪魁祸首似乎是原位分配self.user\u memory[ui\u train[row]]=…
和self.user\u memory[ii\u train[row]=…
。
model = Model(user_M, item_M, feature_M, 3)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
for epoch in range(10):
print(epoch)
output = model(train_data, user_id, item_id)
loss = criterion(output, label)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
`RuntimeError`: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [176, 704]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).