首页 > 解决方案 > PyTorch 中的 .data.size() 和 .size() 有什么区别?

问题描述

我是编程和 ML 的新手,我一直在玩一些简单的数据集和神经网络架构。很多时候我想检查我的数据和对象的维度,看看它们是否有意义。

我总是使用.size()or len(),但是我看到有人使用.data.size(). 我已经在我正在做的一些简单的事情上对它们进行了测试,到目前为止它们给了我相同的结果。我只是还没有遇到或它们完全一样之间有区别吗?

一个例子:

    print(test_images.data.size())
    print(test_outputs.data.size())

给出相同的:

    print(test_images.size())
    print(test_outputs.size())

这是:

    torch.Size([10, 1, 224, 224])
    torch.Size([10, 68, 2])
    torch.Size([10, 68, 2])

任何澄清都非常受欢迎!提前致谢 :)

标签: pythonpytorchsizetensortorchvision

解决方案


According to the documentation What about .data? there is no difference between .data and the tensor itself after Pytorch v0.4.0. However, .data can be unsafe in some cases. Any changes on x.data wouldn't be tracked by autograd, and the computed gradients would be incorrect if x is needed in a backward pass.


推荐阅读