python - 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])
任何澄清都非常受欢迎!提前致谢 :)
解决方案
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.
推荐阅读
- javascript - 在数据表中创建删除确认 SweetAlert
- swiftui - 如何将文本字段值分配给环境对象 - 错误无法将“字符串”类型的值转换为预期的参数类型“绑定”
" - excel - 在 Excel 中剪切/粘贴在同一行中的重复金额
- c# - 在 C# 中构建正则表达式
- python - 如何将多个数据框合并成一个新的数据框,其中每个数据框成为 Pandas 中的新列
- arrays - 在 C 中将字符串添加到 Char 数组中
- rpa - 如何在 Blue Prism 中查找/计算未来的日程安排
- vue.js - 如何使用 vuex getter 返回过滤后的数组?
- html - 并行下拉多级菜单
- jenkins - 运行同一管道的多个作业