Deep learning Pytork/cuda:CPU错误和映射位置

Deep learning Pytork/cuda:CPU错误和映射位置,deep-learning,pytorch,runtime-error,Deep Learning,Pytorch,Runtime Error,我编写此代码是为了下载我的模型: args = parser.parse_args() use_cuda = torch.cuda.is_available() state_dict = torch.load(args.model) model = Net() model.load_state_dict(state_dict) model.eval() if use_cuda: print('Using GPU') model.cuda() else: print(

我编写此代码是为了下载我的模型:

args = parser.parse_args()

use_cuda = torch.cuda.is_available()

state_dict = torch.load(args.model)
model = Net()
model.load_state_dict(state_dict)
model.eval()

if use_cuda:
    print('Using GPU')
    model.cuda()
else:
    print('Using CPU')

但是我的终端返回以下错误
RuntimeError:尝试反序列化CUDA设备上的对象,但torch.CUDA.is\u available()为False。如果您在仅CPU的机器上运行,请使用torch.load with map_location=torch.device(“CPU”)将存储映射到CPU。

因此,我试图在不太理解的情况下写作:

args = parser.parse_args()

map_location=torch.device('cpu')
state_dict = torch.load(args.model)
model = Net()
model.load_state_dict(state_dict)
model.eval()

但我还是犯了同样的错误。你知道我怎样才能改正它吗?(实际上,我想用我的CPU加载我的模型)。

我假设您将模型保存在带有GPU的计算机上,而现在正在将其加载到没有GPU的计算机上,或者您出于某种原因GPU不可用。另外,哪一行导致了错误

参数
map\u location
需要在
torch.load
内设置。像这样:

state_dict = torch.load(args.model, map_location='cpu')

请注意,您需要将map\u位置变量发送到
torch.load
函数

map_location=torch.device('cpu')
state_dict = torch.load(args.model, map_location=map_location)