2 numpy的item() 获取对应的值
np.random.seed(123)
x = np.random.randint(9, size=(3, 3))
x
array([[2, 2, 6],
[1, 3, 6],
[1, 0, 1]])
x.item(3)
1
x.item(7)
0
x.item((0, 1))
2
x.item((2, 2))
1
- tensor中的item(),获取值并保留2位小数
>>> x = torch.tensor([[1]]) >>> x tensor([[ 1]]) >>> x.item() 1 >>> x = torch.tensor(2.5) >>> x tensor(2.5000) >>> x.item() 2.5
4 类型转换,
类型转换, 将list ,numpy转化为tensor。 以list -> tensor为例:
print(torch.FloatTensor([1,2])) 输出: tensor([1., 2.])
求梯度只针对于浮点型tensor,整型tensor不能求梯度
requires_grad只针对浮点型tensor !!!!



