Calling cuda() with async results in SyntaxError(使用异步调用 cuda() 会导致 SyntaxError)
问题描述
我正在尝试运行此 PyTorch 代码:
I'm trying to run this PyTorch code:
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)
但是当我尝试时,我收到此错误消息:
But when I try I am getting this error message:
input = input.float().cuda(async=True)
^
SyntaxError: invalid syntax
Process finished with exit code 1
我做错了什么?我已经安装了 cuda.
What am I doing wrong? I already installed cuda.
推荐答案
您的代码不起作用,因为:
async是 python 中的保留关键字,不能以这种方式使用,这就是为什么你得到SyntaxError
asyncis a reserved keyword in python which cannot be used in that way, that is why you get theSyntaxError
cuda() 不再 有一个参数 async.构造函数看起来像这样:
cuda() no longer has an argument async. The constructor looks like this:
cuda(device=None, non_blocking=False) → 张量
- 以前有一个参数
async但这被non_blocking代替,因为async成为 Python 3.7 中的保留关键字.
https://github.com/pluskid/fit-random-labels/拉/5 - Previously there was an argument
asyncbut this replaced bynon_blockingasasyncbecame a reserved keyword in Python 3.7.
https://github.com/pluskid/fitting-random-labels/pull/5 non_blocking(bool):
如果True并且源在固定内存中,则复制将与主机异步.否则,论证没有效果.默认值:False.
https://pytorch.org/docs/stable/tensors.html#torch.Tensor.cudanon_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
参数 non_blocking 与之前的 async 具有相同的效果:
The argument non_blocking has the same effect as async previously had:
<小时>
作为附加组件:如果您对 async 的实际用途感兴趣,可以查看这里:https://www.python.org/dev/peps/pep-0492/#新语法
As an add-on: If you are interested in what async is actually used for you can take a look here:
https://www.python.org/dev/peps/pep-0492/#new-syntax
这篇关于使用异步调用 cuda() 会导致 SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用异步调用 cuda() 会导致 SyntaxError
基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
