- 问题描述
由于要获取tensor的维度信息,特别是tensor的dims(维度数) - 代码示例
>>> import torch
>>> a=torch.randn(5,2)
>>> a
tensor([[ 0.3960, -1.4427],
[ 0.9658, -0.3596],
[ 0.0992, 0.4757],
[ 0.1087, -0.2690],
[ 1.2446, 0.4505]])
>>> if a.ndim !=2:
... msg='incorreift X.shanpe={}'
... ValueError(msg.format(a.shape))
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Tensor' object has no attribute 'ndim'
会报错,原因是tensor不支持这样求维度数。
改为:
if len(list(a.size())) !=2:
>>> len(list(a.size()))
2