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(变量)

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐