import torch x=torch.eye(3) # 生成一个测试用的tensor ,单位阵 y=torch.nonzero(x>0) # 找出值大于0的索引位置 print(y.shape[0]) #索引行数即是个数