import torch
x=torch.Tensor([1,2])
print('x: ',x)
print('type(x): ',type(x))
print('x.dtype: ',x.dtype) # x的具体类型
y=x.int()
print('y: ',y)
print('type(y): ',type(y))
print('y.dtype: ',y.dtype) # y的具体类型
运行结果:
x: tensor([1., 2.])
type(x): <class 'torch.Tensor'>
x.dtype: torch.float32
y: tensor([1, 2], dtype=torch.int32)
type(y): <class 'torch.Tensor'>
y.dtype: torch.int32
所以想查看tensor中数据的类型,就使用 tensor.dtype
想查看变量的类型,就使用 type(变量)