我正在尝试运行此 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。
请您参考如下方法:
您的代码不起作用,因为:
的原因async是 python 中的保留关键字,不能以这种方式使用,这就是您得到SyntaxErrorcuda()不再 有参数async。构造函数如下所示:
cuda(device=None, non_blocking=False) → Tensor
- 之前有一个参数
async,但是这个参数被non_blocking取代,因为async成为了Python 3.7中的保留关键字。
https://github.com/pluskid/fitting-random-labels/pull/5
改用non_blocking:
参数 non_blocking 与 async 之前的效果相同:
non_blocking(bool):
IfTrueand the source is in pinned memory, the copy will be asynchronous with respect to the host. Otherwise, the argument has no effect. Default:False.
https://pytorch.org/docs/stable/tensors.html#torch.Tensor.cuda
作为附加组件:如果您对 async 的实际用途感兴趣,可以在这里查看: https://www.python.org/dev/peps/pep-0492/#new-syntax
