基本概念
torch.float64
對(duì)應(yīng)torch.DoubleTensor
torch.float32
對(duì)應(yīng)torch.FloatTensor
- 在判斷數(shù)據(jù)類(lèi)型的時(shí)候只能使用
torch.FloatTensor
不能使用torch.float32
,而且tensorA.type()
返回的是數(shù)字串類(lèi)型的
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
因?yàn)樵O(shè)置的是數(shù)據(jù)類(lèi)型而不是tensor
類(lèi)型虱疏。.to(float32)
就等于.to(float)
蝠检,torch.int
也有但是等于torch.IntTensor
不等于torch.LongTensor
喘漏。
進(jìn)行數(shù)據(jù)轉(zhuǎn)換的幾種方式
- 使用函數(shù)
tensor1.type_as(tensor2)
將1的數(shù)據(jù)類(lèi)型轉(zhuǎn)換成2的數(shù)據(jù)類(lèi)型怀愧。
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)