import torch
a = torch.tensor([[ 0.0349, 0.0670, -0.0612, 0.0280, -0.0222, 0.0422],
[-1.6719, 0.1242, -0.6488, 0.3313, -1.3965, -0.0682],
[-1.3419, 0.4485, -0.6589, 0.1420, -0.3260, -0.4795],
[-0.0658, -0.1490, -0.1684, 0.7188, 0.3129, -0.1116],
[-0.2098, -0.2980, 0.1126, 0.9666, -0.0178, 0.1222],
[ 0.1179, -0.4622, -0.2112, 1.1151, 0.1846, 0.4283]])
# 查看tensor的维度信息:torch.Size([6, 6])
print(a.size())
numel就是"number of elements"的简写。numel()可以直接返回int类型的元素个数。
import torch
a = torch.tensor([[ 0.0349, 0.0670, -0.0612, 0.0280, -0.0222, 0.0422],
[-1.6719, 0.1242, -0.6488, 0.3313, -1.3965, -0.0682],
[-1.3419, 0.4485, -0.6589, 0.1420, -0.3260, -0.4795],
[-0.0658, -0.1490, -0.1684, 0.7188, 0.3129, -0.1116],
[-0.2098, -0.2980, 0.1126, 0.9666, -0.0178, 0.1222],
[ 0.1179, -0.4622, -0.2112, 1.1151, 0.1846, 0.4283]])
b = a.numel()
print(type(b)) # int
print(b) # 36