又是一个莫名其妙的bug,搞了我一下午。。
这样有问题:
attention_vector = self.softmax(attentions)
然后这样就OK了:
attention_vector = self.softmax(attentions).clone()
加了个.clone()就行了。。。
(看好多博客说无非就是赋值问题,如a+=b是不行的,但我这样的确实没见过)
如果梯度有问题的话可以用这个来检查:
with torch.autograd.set_detect_anomaly(True):
这样即可:
with torch.autograd.set_detect_anomaly(True):
loss.backward()