這里介紹一下Pytorch模塊的基本操作,在Pytorch中,資料的操作型別為tensor(張量型別),什么是張量呢?推薦自主閱讀維基百科張量(超鏈接點擊跳轉)來進行學習,
- 匯入
Pytoch模塊
import torch
print(torch.__version__)
"""
輸出結果:
1.9.0+cpu
"""
注意這里匯入時使用的關鍵字是torch而不是Pytorch. 其次我們可以看到這里的輸出結果表示我們安裝的是cpu版本,有些電腦支持GPU也可以安裝對應的GPU版本,
- 創建一個
tensor物件
import torch
t1 = torch.Tensor([1, 2, 3])
print(t1)
"""
輸出結果:
tensor([1., 2., 3.])
"""
我們可以看到輸出的資料型別為tensor型別,
- 將
Numpy中使用的型別轉化為Pytorch中可以使用的型別
import torch
import numpy as np
array1 = np.arange(12).reshape(3, 4)
t1 = torch.Tensor(array1)
print(t1)
"""
輸出結果:
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
"""
可以看到我們成功將ndarray型別資料轉換為了tensor型別的資料,
- 創建三行四列空的
tensor物件
import torch
t1 = torch.empty([3, 4])
print(t1)
"""
輸出結果:
tensor([[9.3674e-39, 1.0929e-38, 1.0469e-38, 1.0561e-38],
[1.0286e-38, 1.0653e-38, 1.0194e-38, 4.6838e-39],
[5.1429e-39, 4.9592e-39, 9.9184e-39, 9.0000e-39]])
"""
這里需要注意空并不代表沒有資料,此時,在我們未指定資料值的情況下,系統會自動生成一些非常小的資料,類似的還有torch.ones()和torch.zeros()函式,
- 創建值位于
[0, 1)區間內三行四列的亂數
import torch
t1 = torch.rand([3, 4])
print(t1)
"""
輸出結果:
tensor([[0.5059, 0.3887, 0.8215, 0.5488],
[0.8683, 0.1002, 0.8799, 0.0522],
[0.6802, 0.7736, 0.6704, 0.1614]])
"""
- 創建值位于
[0, 3)之間三行四列的隨機整數
import torch
t1 = torch.randint(low=0, high=3, size=[3, 4])
print(t1)
"""
輸出結果:
tensor([[2, 1, 0, 2],
[2, 2, 0, 0],
[1, 2, 1, 0]])
"""
7.獲取tensor中的資料,當tensor中只有一個元素可用時tensor.item()
import torch
t1 = torch.tensor(1)
print(t1)
print(t1.item())
"""
輸出結果:
tensor(1)
1
"""
- 將
tensor型別的資料轉換為ndarray型別
import torch
t1 = torch.tensor([1, 2])
print(t1.numpy())
"""
輸出結果:
[1 2]
"""
- 獲取
tensor物件的尺寸(形狀)
import torch
t1 = torch.tensor([[[1, 2, 3]]])
print(t1.size())
print(t1.size(0))
print(t1.size(1))
print(t1.size(2))
"""
輸出結果:
torch.Size([1, 1, 3])
1
1
3
"""
我們可以看到,當給定引數0時代表輸出第一個維度中的尺寸,1代表第二個維度中的尺寸,2代表第三個維度中的尺寸,
- 改變
tensor的尺寸(形狀)
import torch
t1 = torch.tensor([[[1, 2],
[2, 3],
[3, 4]]])
print(t1.view(-1))
print(t1.view(2, 3))
print(t1.view(2, -1))
print(t1.view(3, 2))
"""
輸出結果:
tensor([1., 2., 2., 3., 3., 4.])
tensor([[1., 2., 2.],
[3., 3., 4.]])
tensor([[1., 2., 2.],
[3., 3., 4.]])
tensor([[1., 2.],
[2., 3.],
[3., 4.]])
"""
t1.view(-1)表示將tensor變為一維的,t1.view(2, 3)表示將tensor變為兩行三列的,t1.view(2, -1)表示將tensor變為兩行三列的,其中-1表示由編譯器自動計算列數,
- 獲取
tensor的維度,最大,最小值和標準差,
import torch
t1 = torch.tensor([[[1, 2],
[2, 3],
[3, 4]]])
# dim method
print(t1.dim())
# max method
print(t1.max())
# min method
print(t1.min())
# standard deviation
print(t1.std())
"""
輸出結果:
3
tensor(4.)
tensor(1.)
tensor(1.0488)
"""
- 低維度(一維,二維)
tensor的轉置操作
# 一維tensor,轉置后仍舊為原來的tensor
t1 = torch.tensor(1)
print(f"The original tensor is: {t1}")
print(f"The transpose tensor of t1 is {t1.t()}")
print(f"The transpose tensor of t1 is {torch.transpose(input=t1, dim0=0, dim1=-1)}")
# 二維tensor,類似矩陣轉置
t1 = torch.tensor([[1, 2], [3, 4]])
print(f"The original tensor is: {t1}")
print(f"The transpose tensor of t1 is {t1.t()}")
print(f"The transpose tensor of t1 is {torch.transpose(input=t1, dim0=0, dim1=1)}")
"""
輸出結果:
The original tensor is: 1
The transpose tensor of t1 is 1
The transpose tensor of t1 is 1
The original tensor is: tensor([[1, 2],
[3, 4]])
The transpose tensor of t1 is tensor([[1, 3],
[2, 4]])
The transpose tensor of t1 is tensor([[1, 3],
[2, 4]])
"""
我們可以看到對于一維或者二維tensor可以使用.t()或者.transpose()函式進行轉置. 對于高級tensor的轉置操作可以看這里Pytorch中高階tensor的轉置操作(超鏈接點擊跳轉),
tensor切片操作
import torch
import numpy as np
t1 = torch.tensor(np.arange(36).reshape((3, 3, 4)))
# 獲取單個值
print(t1[1, 2, 1])
# 獲取第二個區域內的值
print(t1[1, :, :])
"""
輸出結果:
tensor(21, dtype=torch.int32)
tensor([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]], dtype=torch.int32)
"""
14.輸出tensor中元素的資料型別
import torch
import numpy as np
t1 = torch.tensor(np.arange(24).reshape((2, 3, 4)))
print(t1.dtype)
"""
輸出結果:
torch.int32
"""
15.定義tensor時設定內部元素資料型別
import torch
import numpy as np
# 定義tensor時指定輸入資料型別
t1 = torch.tensor(1, dtype=torch.double)
print(t1.dtype)
# numpy生成陣列時定義元素資料型別,之后轉換為tensor
t1 = torch.tensor(np.array(12, dtype=np.int32))
print(t1.dtype)
t1 = torch.LongTensor(1, 2)
print(t1.dtype)
t1 = torch.DoubleTensor(1, 2)
print(t1.dtype)
"""
輸出結果:
torch.float64
torch.int32
torch.int64
torch.float64
"""
16.tensor的加法運算
import torch
t1 = torch.ones(3, 5)
print(t1)
t2 = torch.rand((3, 5))
print(t2)
# add method
print(t1 + t2)
print(torch.add(t1, t2))
"""
輸出結果:
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
tensor([[0.5156, 0.1977, 0.9491, 0.4509, 0.7190],
[0.9628, 0.4868, 0.9708, 0.9781, 0.7506],
[0.3628, 0.0885, 0.7869, 0.9531, 0.4079]])
tensor([[1.5156, 1.1977, 1.9491, 1.4509, 1.7190],
[1.9628, 1.4868, 1.9708, 1.9781, 1.7506],
[1.3628, 1.0885, 1.7869, 1.9531, 1.4079]])
tensor([[1.5156, 1.1977, 1.9491, 1.4509, 1.7190],
[1.9628, 1.4868, 1.9708, 1.9781, 1.7506],
[1.3628, 1.0885, 1.7869, 1.9531, 1.4079]])
"""
相應的減法,乘法和除法運算可以類比于加法運算,關于四則運算的進階寫法,可以參考這篇Pytorch中的四則運算高級寫法,
碼字不易,如果大家覺得有用,請高抬貴手給一個贊讓我上推薦讓更多的人看到吧~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293508.html
標籤:AI
上一篇:【強烈推薦】神經網路基礎
