【问题标题】:volatile was removed and now had no effect use with.torch.no_grad() instreadvolatile 已被删除,现在使用 with.torch.no_grad() 无效
【发布时间】:2021-05-03 03:06:48
【问题描述】:

我的手电筒程序此时停止 我想我不能使用 volatile=True
我应该如何改变它,停止的原因是什么?
我应该如何更改此代码?

images = Variable(images.cuda())
targets = [Variable(ann.cuda(), volatile=True) for ann in targets]

train.py:166: UserWarning: volatile 已被移除,现在无效。 请改用with torch.no_grad():

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    Variable 不做任何事情,并且自 pytorch 0.4.0 起已被弃用。它的功能与torch.Tensor 类合并。当时,volatile 标志用于禁用 volatile 变量所涉及的任何操作的计算图的构造。较新的 pytorch 已更改此行为,改为使用 with torch.no_grad(): 禁用计算图的构造with 语句的主体。

    您应该更改的内容首先取决于您使用 volatile 的原因。不管你可能想用什么

    images = images.cuda()
    targets = [ann.cuda() for ann in targets]
    

    在训练期间,您将使用类似以下内容来创建计算图(假设模型、标准和优化器的标准变量名称)。

    output = model(images)
    loss = criterion(images, targets)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    

    由于您不需要在评估期间执行反向传播,您可以使用 with torch.no_grad(): 禁用计算图的创建,从而减少内存占用并加快计算速度。

    with torch.no_grad():
        output = model(images)
        loss = criterion(images, targets)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多