python - torch.autograd.Variable 的目的是什么?

标签 python deep-learning pytorch

我加载 featureslabels从我的训练数据集中。它们最初都是 numpy 数组,但我使用 torch.from _numpy(features.copy()) 将它们更改为火炬张量和 torch.tensor(labels.astype(np.bool)) .

我注意到 torch.autograd.Variable类似于 placeholdertensorflow .

当我训练我的网络时,我首先尝试了

features = features.cuda()
labels = labels.cuda()

outputs = Config.MODEL(features)
loss = Config.LOSS(outputs, labels)

然后我尝试了

features = features.cuda()
labels = labels.cuda()

input_var = Variable(features)
target_var = Variable(labels)
outputs = Config.MODEL(input_var)
loss = Config.LOSS(outputs, target_var)

两个 block 都成功激活了训练,但我担心可能会有微不足道的差异。

最佳答案

根据this question你不再需要变量来使用 Pytorch Autograd。
感谢@skytree,我们可以使它更加明确:Variables have been deprecated ,即你不应该再使用它们了。

Autograd automatically supports Tensors with requires_grad set to True.


更重要的是

Variable(tensor) and Variable(tensor, requires_grad) still work as expected, but they return Tensors instead of Variables.


这意味着如果您的 featureslabels你的Variable(features) 已经是张量了(它们似乎在你的例子中)和 Variable(labels)只会再次返回一个张量。
变量的最初目的是能够使用自动微分(Source):

Variables are just wrappers for the tensors so you can now easily auto compute the gradients.

关于python - torch.autograd.Variable 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580202/

相关文章:

python - 如何安装 python twins 旧版本?

python - 是否可以阻止迭代器到达文件末尾?

python - 训练期间的 Tensorflow Slim 调试

python - tensorflow 偏差和权重变量

deep-learning - 是否可以从神经网络模型中断的地方开始执行?

python - img 应该是 PIL 图片。得到了 <class 'torch.Tensor' >

python - 随机选择一个包含特定元素的列表

python - Qsub 作业使用集群上工作节点的子进程

python - 图像字幕给出较弱的结果

machine-learning - PyTorch 种子会影响 dropout 层吗?