1 回答

TA贡献1831条经验 获得超10个赞
with torch.no_grad():
predFeats = self(x)
targetFeats = self(target)
loss = torch.tensor(np.corrcoef(predFeats.cpu().numpy(),
targetFeats.cpu().numpy())[1][1]).float()
您可以RuntimeError通过从计算图中分离张量 (predFeats和targetFeats) 来避免第一个。即获取没有梯度和梯度函数 ( grad_fn)的张量数据的副本。
所以,而不是
torch.Tensor.detach(np.corrcoef(x.numpy(), y.numpy())) # Detaches a newly created tensor!
# x and y still may have gradients. Hence the first error.
什么都不做,做
# Detaches x and y properly
torch.Tensor(np.corrcoef(x.detach().numpy(), y.detach().numpy()))
但是,让我们不要打扰所有的支队。
就像您正确修复一样,让我们禁用渐变。
torch.no_grad()
现在,计算特征。
predFeats = self(x) # No need for the explicit .forward() call
targetFeats = self(target)
我发现打破你的最后一条线很有帮助。
loss = np.corrcoef(predFeats.numpy(), targetFeats.numpy()) # We don't need to detach
# Notice that we don't need to cast the arguments to fp32
# since the `corrcoef` casts them to fp64 anyway.
print(loss.shape, loss.dtype) # A 2-dimensional fp64 matrix
loss = loss[1][1]
print(type(loss)) # Output: numpy.float64
# Loss now just a simple fp64 number
这就是问题所在!
因为,当我们做
loss = torch.from_numpy(loss)
我们传入一个数字 ( numpy.float64) 而它需要一个 numpy 张量 ( np.ndarray)。
如果您使用的是 PyTorch 0.4 或更高版本,则内置了对标量的支持。
只需将from_numpy()方法替换为通用tensor()创建方法即可。
loss = torch.tensor(loss)
PS 您可能还想查看设置rowvar=False,corrcoef因为 PyTorch 张量中的行通常表示观察结果。
添加回答
举报