PyTorch學習(1)
PyTorch學習(1)
一、預先善其事,必先利其器-pytorch與cuda對應關系
| pytorch | torchvision | python | cuda |
|---|---|---|---|
| <1.0.1 | 0.2.2 | ==2.7,>=3.5,<=3.7 | 9.0,10.0 |
| 1.1.0 | 0.3.0 | ==2.7,>=3.5,<=3.7 | 9.0,10.0 |
| 1.2.0 | 0.4.0 | ==2.7,>=3.5,<=3.7 | 9.2,10.0 |
| 1.3.0 | 0.4.1 | ==2.7,>=3.5,<=3.7 | 9.2,10.0 |
| 1.3.1 | 0.4.2 | ==2.7,>=3.5,<=3.7 | 9.2,10.0 |
| 1.4.0 | 0.5.0 | ==2.7,>=3.5,<=3.8 | 9.2,10.0 |
| 1.5.0 | 0.6.0 | >=3.6 | 9.2,10.1,10.2 |
| 1.5.1 | 0.6.1 | >=3.6 | 9.2,10.1,10.2 |
各個版本最好相對應,不然代碼的運行容易出現問題。
二、pytorch相關
1.創建張量
import torch
?
?
a1 = torch.tensor(3)
a2 = torch.tensor([1, 2, 3])
a3 = torch.randn(2, 3)
b3 = torch.rand(2, 3)
a4 = torch.rand(1, 2, 3)
?
print('a1的值:', a1)
print('a1的大小:', a1.shape)
print('------------')
print('a2的值:', a2)
print('a2的大小:', a2.shape)
print('------------')
print('a3的值:', a3)
print('a3的大小:', a3.shape)
print('------------')
print('b3的值:', b3)
print('b3的大小:', b3.shape)
print('------------')
print('a4的值:', a4)
print('a4的大小:', a4.shape)
print('\n 以上為分步定義tensor的值 \n *******************')
?
# 結果顯示
a1的值: tensor(3)
a1的大小: torch.Size([])
------------
a2的值: tensor([1, 2, 3])
a2的大小: torch.Size([3])
------------
a3的值: tensor([[ 0.8593, 0.8400, -0.7855],
[-0.6212, -0.2771, -0.9999]])
a3的大小: torch.Size([2, 3])
------------
b3的值: tensor([[0.0023, 0.1359, 0.0431],
[0.9841, 0.4317, 0.2710]])
b3的大小: torch.Size([2, 3])
------------
a4的值: tensor([[[0.3898, 0.1011, 0.8075],
[0.4289, 0.2972, 0.8072]]])
a4的大小: torch.Size([1, 2, 3])
?
以上為分步定義tensor的值
*******************
?
print(torch.tensor([1, 2.2, -1]))
print('定義的確定數據的float張量:', torch.FloatTensor([1, 2.2, -1]))
print(torch.tensor([[1, 2.2],[3, -1]])) # 與rand的操作類似,構建多維張量
print('\n 以上為直接定義tensor的值 \n *******************')
?
#結果顯示
tensor([ 1.0000, 2.2000, -1.0000])
定義的確定數據的float張量: tensor([ 1.0000, 2.2000, -1.0000])
tensor([[ 1.0000, 2.2000],
[ 3.0000, -1.0000]])
?
以上為直接定義tensor的值
*******************
?
?
print(torch.empty(2, 4)) # 定義未初始化的2行4列的張量
print('定義的1行3列的隨機float張量:', torch.FloatTensor(1, 3))
print('\n 以上為隨機(未初始化)定義tensor的值 \n *******************')
?
#結果顯示
tensor([[1.9758e-43, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])
定義的1行3列的隨機float張量: tensor([[0.0000e+00, 0.0000e+00, 5.3564e-18]])
?
以上為隨機(未初始化)定義tensor的值
*******************
?
print('a1原來的類型:', a1.type())
torch.set_default_tensor_type(torch.DoubleTensor)
print('a1轉變后的類型:', a1.type())
print('\n 以上為轉換默認張量類型 \n *******************')
?
#結果顯示
a1原來的類型: torch.LongTensor
a1轉變后的類型: torch.LongTensor
?
以上為轉換默認張量類型
*******************
?
a5 = torch.rand(3)
b5 = torch.randperm(3) # 生成隨機的整數張量
print('a5的值:', a5)
print('b5的值:', b5)
print('將b5作為a5的索引的值:', a5[b5])
print('\n 以上為生成隨機的整數張量 \n *******************')
?
#結果顯示
a5的值: tensor([0.5683, 0.6638, 0.6250])
b5的值: tensor([1, 0, 2])
將b5作為a5的索引的值: tensor([0.6638, 0.5683, 0.6250])
?
以上為生成隨機的整數張量
*******************
擴展:所創建張量的其他相關語句
-
torch.ones(size)/zero(size)/eye(size): 返回全為1/0/對角單位的張量
-
torch.full(size,fill_value): 返回以fill_value取值填充的size大小的張量
-
torch.rand(size): 返回[0,1)之間的均勻分布張量
-
torch.randn(size): 返回方差為1,均值為0的正態分布張量
-
torch.*_like(input): 返回和輸入大小(幾維、幾行幾列)一樣的張量,其中*可以是rand、randn等等
-
torch.linspace(start,end,step=100): 返回以步長為100的由start到end的一維張量
-
torch.logspace(start,end,steps=100,base=10.0): 返回以100為步長的由base為底的start次方到end次方的一維張量
2.維度變換
先列一個總綱,具體用法可見代碼,順序與總綱一致
-
tensor.squeeze()/tensor.unsqueeze(0) 降維/升維
-
tensor.expand()/tensor.repeat() 擴展張量
-
tensor.transpose()/tensor.premute() 調換張量維度的順序
-
tensor.cat()/tensor.stack() 張量拼接
import torch
?
x = torch.rand(4, 1, 28, 1, 28, 1)
y1 = x.unsqueeze(0) # 在對應索引位置插入一個維度
print('y1的大小:', y1.shape)
y2 = x.squeeze() # 刪除維度為1的維度
print('y2的大小:', y2.shape)
y3 = x.squeeze(1) # 刪除括號數值里對應的索引維度的維度為1的維度
print('y3的大小:', y3.shape)
?
#結果顯示
y1的大小: torch.Size([1, 4, 1, 28, 1, 28, 1])
y2的大小: torch.Size([4, 28, 28])
y3的大小: torch.Size([4, 28, 1, 28, 1])
?
?
?
a = torch.tensor([[[1, 2, 3]]])
print(a)
print('a的大小:', a.shape)
b1 = a.expand(1, 2, 3) # 注意的是expand中的擴展是對某個單一維度(值為1的維度)進行擴展,比如是1行3列,就對行(因為行才是1)進行擴展,列(如果多維,就除要變的不一樣,其他必須一樣)需要與原數據一致。
print(b1)
print('b1的大小:', b1.shape)
b2 = a.expand(1, -1, 3) # -1表示與原張量維度一致
print(b2)
print('b2的大小:', b2.shape)
c = torch.tensor([[[1, 2, 3]]])
print(c)
d1 = c.repeat(2, 4, 2) # repeat是將原張量看成一個整體,對其進行復制操作,例中對第三個維度復制兩次,即變成兩個,行復制四次,列復制兩次,可以不用管維度對應,只管擴張。
print(d1)
print('d1的大小:', d1.shape)
d2 = c.repeat(2, 4, 2, 1) # 此處是增加一個維度,即整體變成兩個,然后里面的一個小塊是四個,四個塊中的一個又是經過原張量行復制兩次,列不復制生成。
print(d2)
print('d2的大小:', d2.shape)
?
#結果顯示
tensor([[[1, 2, 3]]])
a的大小: torch.Size([1, 1, 3])
tensor([[[1, 2, 3],
[1, 2, 3]]])
b1的大小: torch.Size([1, 2, 3])
tensor([[[1, 2, 3]]])
b2的大小: torch.Size([1, 1, 3])
?
tensor([[[1, 2, 3]]])
tensor([[[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3]],
?
[[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3]]])
d1的大小: torch.Size([2, 4, 6])
tensor([[[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]]],
?
?
[[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]],
?
[[1, 2, 3],
[1, 2, 3]]]])
d2的大小: torch.Size([2, 4, 2, 3])
?
?
?
?
e = torch.rand(2, 2, 3, 4)
# print(e)
f1 = e.transpose(1, 3) # 將指定的維度進行調換,換的只能是兩個
# print(f1)
print('f1的大小:', f1.shape)
f2 = e.permute(0, 2, 3, 1) # 將所有維度進行括號內的索引順序轉換,轉換的個數必須和原張量一樣
# print(f2)
print('f2的大小:', f2.shape)
?
#結果顯示
f1的大小: torch.Size([2, 4, 3, 2])
f2的大小: torch.Size([2, 3, 4, 2])
?
?
?
?
g1 = torch.randn(3, 4)
g2 = torch.rand(3, 4)
print(g1)
print(g2)
h1 = torch.cat((g1, g2), 0) # 按行進行同一維度的拼接,如上例,按行拼接拼接后為(6,4)
h2 = torch.stack((g1, g2), 0) # 沿著一個新的維度對輸入張量進行拼接,此處的dim一般為0,不取其他值
print('h1的大小:', h1.shape)
print('h2的大小:', h2.shape)
?
#結果顯示
tensor([[ 0.5554, 0.0449, 0.1231, -0.5494],
[-0.1639, -0.2909, 2.2580, 1.5841],
[ 0.1315, -1.4964, 0.0706, -0.9549]])
tensor([[0.9899, 0.5225, 0.7383, 0.9421],
[0.5493, 0.0317, 0.3085, 0.9770],
[0.5221, 0.0223, 0.2915, 0.7914]])
h1的大小: torch.Size([6, 4])
h2的大小: torch.Size([2, 3, 4])
3.索引切片及數學運算
索引切片:
import torch
?
a = torch.rand(2, 3, 4, 4)
print(a.shape)
?
# 索引
print('a的前兩個維度的索引:', a[0, 0].shape)
print('a的具體值索引:', a[0, 0, 2, 3])
?
# 切片
print('a的第一個維度進行切片:', a[:1].shape)
print('a的每個維度進行切片:', a[:-1, :1, :, :].shape)
?
# ...的用法
print(a[...].shape)
print(a[0, ...].shape)
print(a[:, 2, ...].shape)
print(a[..., :2].shape)
?
# 掩碼取值
x = torch.rand(3, 4)
print(x)
mask = x.ge(0.5) # 與0.5比較,大的為Ture,小的為False
print(mask)
print(torch.masked_select(x, mask)) # 挑選出里面為True的值并打印
?
# 通過torch.take取值
y = torch.tensor([[4, 3, 5], [6, 7, 8]])
y1 = torch.take(y, torch.tensor([0, 2, 5]))
print('y的取值:', y)
print('y1的取值:', y1)
?
#結果顯示
torch.Size([2, 3, 4, 4])
# 索引結果
a的前兩個維度的索引: torch.Size([4, 4])
a的具體值索引: tensor(0.8660)
# 切片結果
a的第一個維度進行切片: torch.Size([1, 3, 4, 4])
a的每個維度進行切片: torch.Size([1, 1, 4, 4])
# ...的用法結果
torch.Size([2, 3, 4, 4])
torch.Size([3, 4, 4])
torch.Size([2, 4, 4])
torch.Size([2, 3, 4, 2])
# 掩碼取值結果
tensor([[0.5534, 0.1831, 0.9449, 0.6261],
[0.4419, 0.2026, 0.4816, 0.0258],
[0.7853, 0.9431, 0.7531, 0.2443]])
tensor([[ True, False, True, True],
[False, False, False, False],
[ True, True, True, False]])
tensor([0.5534, 0.9449, 0.6261, 0.7853, 0.9431, 0.7531])
# 通過torch.take取值結果
y的取值: tensor([[4, 3, 5],
[6, 7, 8]])
y1的取值: tensor([4, 5, 8])
加、減、乘:
-
torch.add() 加法
-
torch.sub() 減法
-
torch.mul/mm/bmm/matmul() 乘法
數學運算:
import torch
?
#加、減、乘
a = torch.rand(3, 4)
b = torch.rand(4)
?
c1 = a + b
c2 = torch.add(a, b)
print('直接用加號結果:', c1)
print('使用add結果:', c2)
?
d1 = a - b
d2 = torch.sub(a, b)
print('直接用減號結果:', d1)
print('使用sub結果:', d2)
?
c = torch.randn(1, 2, 3)
d = torch.randn(1, 3, 4)
e = torch.rand(1, 2)
f = torch.rand(2, 3)
e1 = a * b
e2 = torch.mul(a, b) # 點乘,當a,b維度不一樣可以自己復制填充不夠的然后相乘,對位相乘
e3 = torch.mm(e, f) # 針對二維矩陣,要滿足矩陣乘法規則
e4 = torch.bmm(c, d) # 輸入,即括號內的張量必須是三維的,且滿足第一個(x,y,z),第二個必須(x,z,隨意)
e5 = torch.matmul(c, d) # 具有廣播效果,矩陣維度不一樣時,自動填充,然后相乘,但需要相乘矩陣最后兩個維度滿足矩陣乘法法則
print(e1)
print(e2)
print(e3)
print(e4)
print(e5)
?
#結果顯示
直接用加號結果: tensor([[0.9060, 1.1983, 1.1655, 1.2972],
[1.6351, 0.3494, 0.8485, 1.0029],
[1.8000, 0.4619, 0.9559, 0.7184]])
使用add結果: tensor([[0.9060, 1.1983, 1.1655, 1.2972],
[1.6351, 0.3494, 0.8485, 1.0029],
[1.8000, 0.4619, 0.9559, 0.7184]])
直接用減號結果: tensor([[-0.8189, 0.7739, 0.7891, 0.2740],
[-0.0898, -0.0749, 0.4722, -0.0202],
[ 0.0752, 0.0375, 0.5796, -0.3047]])
使用sub結果: tensor([[-0.8189, 0.7739, 0.7891, 0.2740],
[-0.0898, -0.0749, 0.4722, -0.0202],
[ 0.0752, 0.0375, 0.5796, -0.3047]])
?
tensor([[0.0376, 0.2092, 0.1839, 0.4019],
[0.6663, 0.0291, 0.1243, 0.2514],
[0.8086, 0.0530, 0.1445, 0.1058]])
tensor([[0.0376, 0.2092, 0.1839, 0.4019],
[0.6663, 0.0291, 0.1243, 0.2514],
[0.8086, 0.0530, 0.1445, 0.1058]])
tensor([[0.1087, 0.0323, 0.2181]])
tensor([[[ 1.9481, 3.7797, -2.5594, 0.2444],
[ 0.3162, 0.1580, -0.0066, 0.0721]]])
tensor([[[ 1.9481, 3.7797, -2.5594, 0.2444],
[ 0.3162, 0.1580, -0.0066, 0.0721]]])
擴展:
-
torch.exp() e的指數冪
-
torch.log() 取對數
-
torch.mean () 求均值
-
torch.sum () 求和
-
torch.max\torch.min () 求最大/最小值
-
torch.prod () 返回input中所有元素的乘積
-
torch.argmin(input)/torch.argmax(input) 最大值/最小值的索引
-
torch.where(condition, x, y)) 如果符合條件返回x,不符合返回y
-
torch.gather(input, dim, index) 沿dim指定的軸收集數據
-
tensor.floor() 向下取整
-
tensor.pow() 平方
-
tensor.sqrt() 開根號
-
tensor.ceil() 向上取整
-
tensor.round() 四舍五入
-
tensor.trunc() 取整數值
-
tensor.frac() 取小數值
-
tensor.clamp(min,max) 比最小值小的變成最小值,把比最大值大的變成最大值
4.autograd:自動求導
首先,在pytorch中創建張量的形式為:torch.tensor(data= , dtype=None(默認) , device=None(默認) , requires_grad=False(默認) )。簡單來說,自動求導就是在進行張量定義時,自行的可以進行求導或者說求梯度計算,只要將張量默認輸入參數中的requires_gard設置成True,就看進行自動求導了。下面舉個例子,簡單看一下具體流程:
我們求的原式為:zi=3(xi+2)2,即可以看成z=3(x1+2)(x2+2)...(xi+2)
-
第一種情況,當我們的輸出時一個標量時
import torch
?
x = torch.ones(1, 3, requires_grad=True) # 為了方便手動計算,我們使用單位矩陣
a = x + 2
z = 3 * a.pow(2)
?
print('x的值', x)
print('a的值', a)
print('z的值', z)
?
out = torch.mean(z) # 此處的out是一個標量,由x的大小可以看出,求均值的分母為x的個數
out.backward()
print(x.grad)
?
#結果顯示
x的值 tensor([[1., 1., 1.]], requires_grad=True)
a的值 tensor([[3., 3., 3.]], grad_fn=<AddBackward0>)
z的值 tensor([[27., 27., 27.]], grad_fn=<MulBackward0>)
tensor([[6., 6., 6.]])
上面代碼中out被我們定義為:
$$out = \frac{{3\left[ {{{\left( {{x_1} + 2} \right)}^2} + {{\left( {{x_2} + 2} \right)}^2} + {{\left( {{x_3} + 2} \right)}^2}} \right]}}{3}$$
所以求導很容易看出:
$$\frac{{\partial out}}{{\partial {x_1}}} = \frac{{\partial out}}{{\partial {x_2}}} = \frac{{\partial out}}{{\partial {x_3}}} = \frac{{3*\left( {2*1 + 2*1 + 2*1} \right)}}{3} = 6$$
-
第二種情況,當我們的輸出是一個向量時
import torch
import copy
?
x = torch.ones(1, 3, requires_grad=True) # 為了方便手動計算,我們使用單位矩陣
a = x + 2
z = 3 * a.pow(2)
?
print('x的值', x)
print('a的值', a)
print('z的值', z)
?
gradients1 = torch.tensor([[0.1, 1, 0.01]], dtype=torch.float) # 要注意的是這里的參數要與out的維度保持一致
z.backward(gradients1, True) # 此處是為了保證最后輸出的行數,以此類推,幾個gradients就是幾行
A_temp = copy.deepcopy(x.grad)
x.grad.zero_()
?
gradients2 = torch.tensor([[1, 1, 1]], dtype=torch.float)
z.backward(gradients2)
B_temp = x.grad
print(torch.cat((A_temp, B_temp), 0))
?
#結果顯示
x的值 tensor([[1., 1., 1.]], requires_grad=True)
a的值 tensor([[3., 3., 3.]], grad_fn=<AddBackward0>)
z的值 tensor([[27., 27., 27.]], grad_fn=<MulBackward0>)
tensor([[ 1.8000, 18.0000, 0.1800],
[18.0000, 18.0000, 18.0000]])
這里我們傳入的參數看成行向量,與對應的雅可比矩陣1進行線性操作。
-
第三種情況,當我們輸出為一個矩陣時
import torch
?
x = torch.ones(2, 3, requires_grad=True) # 為了方便手動計算,我們使用單位矩陣
a = x + 2
z = 3 * a.pow(2)
?
print('x的值', x)
print('a的值', a)
print('z的值', z)
?
gradients = torch.tensor([[1, 1, 1], [0, 1, 2]], dtype=torch.float)
z.backward(gradients)
print(x.grad)
?
#結果顯示
x的值 tensor([[1., 1., 1.],
[1., 1., 1.]], requires_grad=True)
a的值 tensor([[3., 3., 3.],
[3., 3., 3.]], grad_fn=<AddBackward0>)
z的值 tensor([[27., 27., 27.],
[27., 27., 27.]], grad_fn=<MulBackward0>)
tensor([[18., 18., 18.],
[ 0., 18., 36.]])
浙公網安備 33010602011771號