基本概念
torch.float64
对应torch.DoubleTensor
torch.float32
对应torch.FloatTensor
- 在判断数据类型的时候只能使用
torch.FloatTensor
不能使用torch.float32
,而且tensorA.type()
返回的是数字串类型的
a=[[1.0,2.0],[3.9,4]]
b=[2.0,4]
a=torch.tensor(a)
b=torch.tensor(b).to(torch.float32)
if b.type()=="torch.FloatTensor":
print("ha")
>>>ha
在
torch.tensor([1.2,3],dtype=troch.float)
中的dtype不能用dtype=torch.FloatTensor
因为设置的是数据类型而不是tensor
类型。.to(float32)
就等于.to(float)
,torch.int
也有但是等于torch.IntTensor
不等于torch.LongTensor
。
进行数据转换的几种方式
- 使用函数
tensor1.type_as(tensor2)
将1的数据类型转换成2的数据类型。
tensor_1 = torch.FloatTensor(5)
tensor_2 = torch.IntTensor([10, 20])
tensor_1 = tensor_1.type_as(tensor_2)
tensor.type(torch.IntTensor)
-
tensor.long()
,tensor.char()
,tensor.int()
,tensor.byte()
,tensor.double()
tenosr.to(torch.long)