Pytorch中的volatile变量是什么

What is volatile variable in Pytorch

Pytorch中变量的volatile属性是什么?这是在 PyTorch 中定义变量的示例代码。

datatensor = Variable(data, volatile=True)

基本上,如果您仅进行推理并且不会 运行 反向传播以节省内存,请将网络的输入设置为易失性。

来自docs

Volatile is recommended for purely inference mode, when you’re sure you won’t be even calling .backward(). It’s more efficient than any other autograd setting - it will use the absolute minimal amount of memory to evaluate the model. volatile also determines that requires_grad is False.

编辑:自 pytorch 版本 0.4.0 起,volatile 关键字已 deprecated

对于 0.4.0 之前的 Pytorch 版本,VariableTensor 是两个不同的实体。对于变量,您可以指定两个标志:volatilerequire_grad。它们都用于从梯度计算中细粒度地排除子图。

volatilerequires_grad 之间的区别在于标志如何应用于操作的输出。如果甚至有一个 volatile = True 变量作为操作的输入,其输出也将被标记为 volatile。对于 requires_grad,您需要标记该操作的所有输入 requires_grad = False,以便输出也以相同的方式标记。

Pytorch 0.4.0 开始,TensorsVariables 已合并,volatile 标志已弃用。