invalid gradient at index 0 - expected type TensorOptions

问题描述

Traceback (most recent call last):
File “D:\python\modelsmy\saliencymap.py”, line 24, in compute_jacobian
output.backward(mask, retain_graph=True)

RuntimeError: invalid gradient at index 0 - expected type

TensorOptions(dtype=float, device=cuda:0, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt))

but got

TensorOptions(dtype=float, device=cpu, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt))

虚惊一场 只是设备报错
原代码
output.backward(mask, retain_graph=True)
修改后
output.backward(torch.tensor(mask).cuda(), retain_graph=True)

你可能感兴趣的:(invalid gradient at index 0 - expected type TensorOptions)