2021-06-11 pytorch基本语法
前言
pytorch是個好東西,它與tf的區別是,tf的布署性好,而pytorch布屬性稍差。何為布署性?就是在實際應用中,可以用c調用、可以用java調用、用php調用...總之,支持的語言越多,布屬性越好,這其實與模型嘗試、模型訓練沒有半毛錢關系,何況,以2017年做分水嶺,之后的項目全在tf2或pytorch上運行,因此提倡用pytorch,本文對最簡單的pytorch進行梳理。
1 python變量和pytorch變量對照
2 如何定義pytorch張量
【1】生成浮點數或整數變量
- 用size生成
a = torch.FloatTensor(2,3)? ? ?或
a = torch.IntTensor(2,3)
- 用list生成(list = [2,3,4,5])
a = torch.FloatTensor([2,3,4,5])? ?或
a = torch.IntTensor([2,3,4,5])
【2】生成浮點均勻分布隨機張量
生成浮點數2X3的矩陣:分布【0,1】均勻
a = torch.rand(2,3)
【3】生成順序表
import torch
a = torch.range(2,8,1)
?????【4】生成全零張量
import torch
a = torch.zeros(2,3)
???????【5】張量清空
將生成一個空張量
print( torch.empty(5, 3) )
???????【6】張量產生
x = x.new_ones(5, 3, dtype=torch.double)
print("x={}".format(x))
???????【7】形狀復制的隨機張量
x = x.new_ones(5, 3, dtype=torch.double)
print("x={}".format(x))
x = torch.rand_like(x,dtype=torch.float)
【8】獲取張量的元素值
# 獲取某一個元素的值
print("x[1][1]={}".format(x[1][1]))
print("x[1][1].item()={}".format(x[1][1].item()))
???????【9】轉換Torch tensor到numpy
a = torch.ones(6)
b = a.numpy()
【10】轉換numpy到Torch tensor
a = np.ones(2)
b = torch.from_numpy(a)
【11】將本地的tensor定義到GPU內
# pytorch 直接將張量定義到cuda內存中
if torch.cuda.is_available():
??? ????device = torch.device("cuda")
??? y = torch.ones_like(x,device=device)
【12】直接創建一個與x大小相同的tensor,放于GPU上
??? print("\nx={},dtype is {}".format(x,x.dtype))
??? print("y=torch.ones_like(x,device=device)={},dtype is {}".format(y,y.dtype))
??? #z = x+y #RuntimeError: expected type torch.FloatTensor but got torch.cuda.FloatTensor一個在GPU上,一個在CPU上,無法運算
??? x = x.to(device) # 將x傳到GPU上
??? print("x=x.to(device)={},dtype is {}".format(x,x.dtype))
??? z = x + y
??? print("z=x+y={},dtype is {}".format(z,z.dtype))
【13】 cpu數據和cuda數據合法性檢驗
x = torch.ones(5, 3, dtype=torch.double)print ( x.type() )x = x.cuda()print ( x.type() )結果:
torch.DoubleTensor
torch.cuda.DoubleTensor
3 pytorch張量其它
1.用numel獲取某張量的元素個數
a = torch.randn(1,2,3,4,5)
torch.numel(a)
>>>120
再來一個例子
a = torch.zeros(4,4)
torch.numel(a)
>>>16
???????2.測試某張量是否定義在GPU上
用python的Isinstance函數可以測量一個變量定義在內存(cpu),或顯存(gpu)
data= torch.randn(1,2,3,4,5)? ? ?定義在cpu內存上
Isinstance(data,torch.cuda.doubleTensor)
>>>False
data = data.cuda()? ? ? ? ? 將data定義到顯存GPU上
Isinstance(data,torch.cuda.doubleTensor)
>>>True
?
4 張量上的運算
1絕對值
import torch
a = torch.randn(2,3)
b = torch.abs(a)
2 張量加法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.add(a,b)
3 張量裁剪
具體的裁剪過程是:使用變量中的每個元素分別和裁剪的上邊界及裁剪的下邊界的值進行比較,如果元素的值小于裁剪的下邊界的值,該元素就被重寫成裁剪的下邊界的值;同理,如果元素的值大于裁剪的上邊界的值,該元素就被重寫成裁剪的上邊界的值。
import torch
a = torch.randn(2,3)
b = torch.clamp(a,-0.1,0.1)
tensor([[ 0.0251,? 1.8832,? 1.5243],
??????? [-0.1365,? 1.2307,? 0.0640]])? #裁剪前
tensor([[ 0.0251,? 0.1000,? 0.1000],
??????? [-0.1000,? 0.1000,? 0.0640]])? #裁剪后
4 張量的除法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.div(a,b)
d = torch.randn(2,3)
e = torch.div(d,10)
print(e)
5 張量的乘法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.mul(a,b)
d = torch.randn(2,3)
e = torch.mul(d,10)
print(e)
6 張量的冪函數
import torch
a = torch.randn(2,3)
print(a)
b = torch.pow(a,2)
print(b)
7 矩陣的張量積
pytorch乘積相當于tf的matmul
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3,2)
print(b)
b = torch.mm(a,b)
print(b)
8 矩陣和向量的積
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3)
print(b)
c = torch.mv(a,b)
print(c)
9 in-place方法
in-place方法就是不增加額外內存空間,在現有空間直接運算并用結果替換的方法。
??? y.add_(x)
??? x.copy_(y)
10 tensor resize/reshape
將張量形狀塑變成其它形狀
x = torch.rand(4,4)
y = x.view(16)
以上運算都是對張量進行修理的的運算,還有定義在張量內部運算,以后將在實例中逐步展現。
?
總結
以上是生活随笔為你收集整理的2021-06-11 pytorch基本语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021-05-21 深入理解SLAM技
- 下一篇: 编码调试:UnicodeDecodeEr