数据类型转换pytorch
生活随笔
收集整理的這篇文章主要介紹了
数据类型转换pytorch
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
du = torch.ones([2,2])
a = np.array([[1,2],[3,4]],dtype=np.float32)
b = torch.from_numpy(a)#數據類型是不變的 上面dtype是什么類型 下面torch對應就是什么tensor
print('yes')
#tensor 默認是float32的 比如torch.ones zeros都是生成float32數據a=torch.FloatTensor( (3,2) )
print(a.dtype)
qq = np.array([1,2],dtype=np.int32)
qq = qq.astype(np.float32) #這個是numpy的類型轉換 下面是tensor的類型轉換 只astype或者int()并不能改變數據類型
#必須有賦值操作才可以a = a.int()##tensor 數據類型轉換
print(a.dtype)#對于python的數據 直接進行類型轉化即可
#例如
m = 5
n = float(m)
torch 數據類型轉換:
tensor = torch.Tensor(3, 5)# torch.long() 將tensor投射為long類型
newtensor = tensor.long()# torch.half()將tensor投射為半精度浮點類型
newtensor = tensor.half()# torch.int()將該tensor投射為int類型
newtensor = tensor.int()# torch.double()將該tensor投射為double類型
newtensor = tensor.double()# torch.float()將該tensor投射為float類型
newtensor = tensor.float()# torch.char()將該tensor投射為char類型
newtensor = tensor.char()# torch.byte()將該tensor投射為byte類型
newtensor = tensor.byte()# torch.short()將該tensor投射為short類型
newtensor = tensor.short()
總結
以上是生活随笔為你收集整理的数据类型转换pytorch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矩阵拼接 cat padding_pyt
- 下一篇: xgboost重要参数1