使用async调用cuda()会导致语法错误

17

我正在尝试运行这段PyTorch代码:

for i, (input, target) in enumerate(train_loader):

    input = input.float().cuda(async=True)
    target = target.cuda(async=True)
    input_var = torch.autograd.Variable(input)
    target_var = torch.autograd.Variable(target)

    output = model(input_var)

但是当我尝试时,我收到了这个错误消息:

input = input.float().cuda(async=True)
                               ^
SyntaxError: invalid syntax
Process finished with exit code 1

我做错了什么?我已经安装了cuda。


1
你好,欢迎来到 Stack Overflow。请参考 [提问] 链接以获取更多关于如何提问的详细信息,并相应地更新你的问题。 - Jeroen Heier
2个回答

34

你的代码不起作用,因为:

  • async 是Python中的保留关键词,不能以这种方式使用,这就是为什么你会得到 SyntaxError 的原因。

  • cuda() 不再具有参数 async。构造函数如下所示:

cuda(device=None, non_blocking=False) → Tensor

改用 non_blocking 参数:

参数 non_blocking 具有与先前的 async 相同的作用:



作为一种附加功能:如果你对async的实际用途感兴趣,可以在此处查看: https://www.python.org/dev/peps/pep-0492/#new-syntax


10

现在已经弃用了async参数,因为async在Python 3.7中成为了保留字。有关详细信息,请参阅此问题:rename .cuda(async=..) parameters。您可以使用non_blocking作为替代。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接