使用异步调用 cuda() 会导致 SyntaxError [英] Calling cuda() with async results in SyntaxError

查看:197
本文介绍了使用异步调用 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

  • async is a reserved keyword in python which cannot be used in that way, that is why you get the SyntaxError

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 async but this replaced by non_blocking as async became a reserved keyword in Python 3.7.
      https://github.com/pluskid/fitting-random-labels/pull/5
    • 参数 non_blocking 与之前的 async 具有相同的效果:

      The argument non_blocking has the same effect as async previously had:

      • non_blocking (bool):
        If True and 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/#新语法



      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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆