【DEBUG日记】torch.unique() 报错

问题描述

在模型输出数据的后处理中,在使用torch.unique() 时报以下错误:

RuntimeError: isDifferentiableType(variable.scalar_type()) INTERNAL ASSERT FAILED at "..\\torch/csrc/autograd/functions/utils.h":64, please report a bug to PyTorch.


问题分析:

torch.unique()类似于集合,返回一个无重复的tensor

torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None)
'''
input: 待处理的tensor
sorted:是否进行排列,默认升序
return_inverse: 是否返回原始tensor中的每个元素在这个无重复tensor中的索引
return_counts: 统计原始张量中每个独立元素的个数
dim: 处理维度
'''

从报错看,应该是输入数据的类型有问题,检查了tensor的shape等均无问题,应该是其他的参数,例如requires_grad


解决方案:

  • 在模型计算之前加上with torch.no_grad():
  • 在对tensor计算前使用tensor.requires_grad = False

猜你喜欢

转载自blog.csdn.net/lucifer479/article/details/125997037
今日推荐