1 numpy與CUDA之間的轉換
1.tensor張量與numpy相互轉換
tensor ----->numpy
import torch
a=torch.ones([2,5])
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
# **********************************
b=a.numpy()
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=float32)
numpy ----->tensor
import numpy as np
a=np.ones([2,5])
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
# **********************************
b=torch.from_numpy(a)
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=torch.float64)
2.tensor張量與list相互轉換
tensor—>list
a=torch.ones([1,5])
tensor([[1., 1., 1., 1., 1.]])
# ***********************************
b=a.tolist()
[[1.0, 1.0, 1.0, 1.0, 1.0]]
list—>tensor
a=list(range(1,6))
[1, 2, 3, 4, 5]
# **********************************
b=torch.tensor(a)
tensor([1, 2, 3, 4, 5])
3.tensor張量見類型轉換
構建一個新的張量,你要轉變成不同的類型只需要根據自己的需求選擇即可
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()
4.type_as() 將張量轉換成指定類型張量
>>> a=torch.Tensor(2,5)
>>> a
tensor([[1.9431e-19, 4.8613e+30, 1.4603e-19, 2.0704e-19, 4.7429e+30],
[1.6530e+19, 1.8254e+31, 1.4607e-19, 6.8801e+16, 1.8370e+25]])
>>> b=torch.IntTensor(1,2)
>>> b
tensor([[16843009, 1]], dtype=torch.int32)
>>> a.type_as(b)
tensor([[ 0, -2147483648, 0, 0, -2147483648],
[-2147483648, -2147483648, 0, -2147483648, -2147483648]],
dtype=torch.int32)
>>> a
tensor([[1.9431e-19, 4.8613e+30, 1.4603e-19, 2.0704e-19, 4.7429e+30],
[1.6530e+19, 1.8254e+31, 1.4607e-19, 6.8801e+16, 1.8370e+25]])
1.Pytorch上的數據類型
Pytorch的類型可以分為CPU和GPU上的Tensor, 它們擁有的數據類型是基本上是一樣的:
tensor.FloatTensor
tensor.LongTensor
tensor.ByteTensor
tensor.CharTensor
tensor.ShortTensor
tensor.IntTensor
torch.LongTensor
其中torch.Tensor是默認的tensor.FloatTensor的簡稱。
2.數據類型之間的轉換
tensor = torch.Tensor(3, 5)
torch.long() 將tensor投射為long類型:newtensor = torch.long()
torch.int()將該tensor投射為int類型:newtensor = torch.int()
torch.double()將該tensor投射為double類型:newtensor = torch.double()
一般,只要在Tensor后加long(), int(), double(), float(), byte()等函數就能將Tensor的類型進行轉換
除此之外,可以使用type()函數,data為Tensor數據類型,data.type()給出data的類型,如果使用data.type(torch.FloatTensor)則強制轉換為torch.FloatTensor類型的張量, 如果不知道什么類型,可以使用tensor_1.type_as(tensor_2), 將tensor_1轉換成tensor_2。
-
self = torch.LongTensor(3, 5)
-
# 轉換為其他類型
-
print self.type(torch.FloatTensor)
3.cuda數據類型,cpu類型和一般的數據類型
如果沒有特別說明:tensor是cpu上的變量
使用gpu張量:tensor.cuda()
使用cpu張量:tensor.cpu()
Variable轉換成普通的Tensor: variable.data()
Tesnor轉換成numpy array的格式:tensor.numpy()
numpy數據轉換成Tensor: torch.from_numpy(np_data)
Tensor轉換成Variable: Variable(tensor)
Pytorch中的Tensor常用的類型轉換函數(inplace操作):
(1)數據類型轉換
在Tensor后加 .long(), .int(), .float(), .double()等即可,也可以用.to()函數進行轉換,所有的Tensor類型可參考https://pytorch.org/docs/stable/tensors.html
(2)數據存儲位置轉換
CPU張量 ----> GPU張量,使用data.cuda()
GPU張量 ----> CPU張量,使用data.cpu()
(3)與numpy數據類型轉換
Tensor---->Numpy 使用 data.numpy(),data為Tensor變量
Numpy ----> Tensor 使用 torch.from_numpy(data),data為numpy變量
(4)與Python數據類型轉換
Tensor ----> 單個Python數據,使用data.item(),data為Tensor變量且只能為包含單個數據
Tensor ----> Python list,使用data.tolist(),data為Tensor變量,返回shape相同的可嵌套的list
(5)剝離出一個tensor參與計算,但不參與求導
Tensor后加 .detach()
官方解釋為:
Returns a new Tensor, detached from the current graph. The result will never require gradient. Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks.
(以前這個功能用過.data(),但現在不推薦使用了)
2 張量數據類型的轉換
Pytorch中tensor的類型
- Pytorch中定義了8種CPU張量類型和對應的GPU張量類型,CPU類型(如torch.FloatTensor)中間加一個cuda即為GPU類型(如torch.cuda.FloatTensor)
- torch.Tensor()、torch.rand()、torch.randn() 均默認生成 torch.FloatTensor型
- 相同數據類型的tensor才能做運算
一個例子:
- torch.FloatTensor(2,3) #構建一個2*3 Float類型的張量
- torch.DoubleTensor(2,3) #構建一個2*3 Double類型的張量
- torch.HalfTensor (2,3) #構建一個2*3 HalfTenso類型的張量
- torch.ByteTensor(2,3) #構建一個2*3 Byte類型的張量
- torch.CharTensor(2,3) #構建一個2*3 Char類型的張量
- torch.ShortTensor(2,3) #構建一個2*3 Short類型的張量
- torch.IntTensor(2,3) #構建一個2*3 Int類型的張量
- torch.LongTensor(2,3) #構建一個2*3 Long類型的張量
import torch print(torch.FloatTensor(2,3).type()) #構建一個2*3 Float類型的張量 print(torch.DoubleTensor(2,3).type()) #構建一個2*3 Double類型的張量 print(torch.HalfTensor (2,3).type()) #構建一個2*3 HalfTenso類型的張量 print(torch.ByteTensor(2,3).type()) #構建一個2*3 Byte類型的張量 print(torch.CharTensor(2,3).type()) #構建一個2*3 Char類型的張量 print(torch.ShortTensor(2,3).type()) #構建一個2*3 Short類型的張量 print(torch.IntTensor(2,3).type()) #構建一個2*3 Int類型的張量 print(torch.LongTensor(2,3).type()) #構建一個2*3 Long類型的張量 torch.FloatTensor torch.DoubleTensor torch.HalfTensor torch.ByteTensor torch.CharTensor torch.ShortTensor torch.IntTensor torch.LongTensor
tensor數據類型轉換方法
- 使用獨立的函數如 int(),float()等進行轉換
- 使用torch.type()函數,直接顯示輸入需要轉換的類型
- 使用type_as()函數,將該tensor轉換為另一個tensor的type
使用獨立的函數
import torch tensor = torch.randn(2, 2) print(tensor.type()) # torch.long() 將tensor轉換為long類型 long_tensor = tensor.long() print(long_tensor.type()) # torch.half()將tensor轉換為半精度浮點類型 half_tensor = tensor.half() print(half_tensor.type()) # torch.int()將該tensor轉換為int類型 int_tensor = tensor.int() print(int_tensor.type()) # torch.double()將該tensor轉換為double類型 double_tensor = tensor.double() print(double_tensor.type()) # torch.float()將該tensor轉換為float類型 float_tensor = tensor.float() print(float_tensor.type()) # torch.char()將該tensor轉換為char類型 char_tensor = tensor.char() print(char_tensor.type()) # torch.byte()將該tensor轉換為byte類型 byte_tensor = tensor.byte() print(byte_tensor.type()) # torch.short()將該tensor轉換為short類型 short_tensor = tensor.short() print(short_tensor.type()) torch.FloatTensor torch.LongTensor torch.HalfTensor torch.IntTensor torch.DoubleTensor torch.FloatTensor torch.CharTensor torch.ByteTensor torch.ShortTensor
使用torch.type()函數
type(new_type=None, async=False)如果未提供new_type,則返回類型,否則將此對象轉換為指定的類型。 如果已經是正確的類型,則不會執行且返回原對象,用法如下:
t1 = torch.LongTensor(3, 5) print(t1.type()) # 轉換為其他類型 t2=t1.type(torch.FloatTensor) print(t2.type()) torch.LongTensor torch.FloatTensor
使用type_as()函數
- 這個函數的作用是將該tensor轉換為另一個tensor的type,可以同步完成轉換CPU類型和GPU類型,如torch.IntTensor-->torch.cuda.floatTendor.
- 如果張量已經是指定類型,則不會進行轉換
t1=torch.Tensor(2,3) t2=torch.IntTensor(3,5) t3=t1.type_as(t2) print(t3.type()) torch.IntTensor
主要參考:
https://blog.csdn.net/qq_40357974/article/details/101697721
https://blog.csdn.net/weixin_40446557/article/details/88221851