在 numpy 中, V.shape
给出了 V 维度的整数元组。
在 tensorflow V.get_shape().as_list()
给出了 V 维度的整数列表。
在 pytorch 中, V.size()
给出了一个大小对象,但我如何将其转换为整数?
原文由 patapouf_ai 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 numpy 中, V.shape
给出了 V 维度的整数元组。
在 tensorflow V.get_shape().as_list()
给出了 V 维度的整数列表。
在 pytorch 中, V.size()
给出了一个大小对象,但我如何将其转换为整数?
原文由 patapouf_ai 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您喜欢 NumPy
ish 语法,那么这里有 tensor.shape
。
In [3]: ar = torch.rand(3, 3)
In [4]: ar.shape
Out[4]: torch.Size([3, 3])
# method-1
In [7]: list(ar.shape)
Out[7]: [3, 3]
# method-2
In [8]: [*ar.shape]
Out[8]: [3, 3]
# method-3
In [9]: [*ar.size()]
Out[9]: [3, 3]
PS : Note that tensor.shape
is an alias to tensor.size()
, though tensor.shape
is an attribute of the tensor in question whereas tensor.size()
is a function .
原文由 kmario23 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答873 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
对于 PyTorch v1.0 及可能更高版本:
您可以将任何 torch.Size 对象转换为原生 Python 列表:
在 PyTorch v0.3 和 0.4 中:
简单地
list(var.size())
,例如: