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
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
