对于我的任务,我不需要计算梯度。我只是在损失评估nn.L1Loss中用 numpy 函数 ( corrcoef)替换,但出现以下错误:
RuntimeError: Can’t call numpy() on Variable that requires grad. Use var.detach().numpy() instead.
我不知道我应该如何分离图表(我试过torch.Tensor.detach(np.corrcoef(x, y)),但我仍然遇到同样的错误。我最终使用torch.no_grad以下方法包装了所有内容:
with torch.no_grad():
predFeats = self.forward(x)
targetFeats = self.forward(target)
loss = torch.from_numpy(np.corrcoef(predFeats.cpu().numpy().astype(np.float32), targetFeats.cpu().numpy().astype(np.float32))[1][1])
但是这次我收到以下错误:
TypeError: expected np.ndarray (got numpy.float64)
我想知道,我做错了什么?
慕哥6287543
相关分类