五個PyTorch 中的處理張量的基本函數
能夠以準確有效的方式構建神經網絡是招聘人員在深度學習工程師中最受追捧的技能之一。PyTorch 是一個 主要用于深度學習的Python 庫。 PyTorch 最基本也是最重要的部分之一是創建張量,張量是數字、向量、矩陣或任何 n 維數組。在構建神經網絡時為了降低計算速度必須避免使用顯式循環,我們可以使用矢量化操作來避免這種循環。在構建神經網絡時,足夠快地計算矩陣運算的能力至關重要。
“為什么不使用 NumPy 庫呢?”
對于深度學習,我們需要計算模型參數的導數。 PyTorch 提供了在反向傳播時跟蹤導數的能力而 NumPy 則沒有,這在Pytorch中被稱為“Auto Grad”。PyTorch 為使用 GPU 的快速執行提供了內置支持。這在訓練模型方面至關重要。由于 Numpy 缺乏將其計算轉移到 GPU 的能力,因此訓練模型的時間最終會變得非常大。
所有使用 PyTorch 的深度學習項目都從創建張量開始。讓我們看看一些必須知道的函數,它們是任何涉及構建神經網絡的深度學習項目的支柱。
- torch.tensor()
- torch.sum()
- torch.index_select()
- torch.stack()
- torch.mm()
在安裝完Pytorch后,在代碼中可以直接導入:
- # Import torch and other required modules
- import torch
torch.tensor()
首先,我們定義了一個輔助函數,describe (x),它將總結張量 x 的各種屬性,例如張量的類型、張量的維度和張量的內容。
- # Helper function
- def describe(x):
- print("Type: {}".format(x.type()))
- print("Shape/size: {}".format(x.shape))
- print("Values: \n{}".format(x)
使用 torch.Tensor 在 PyTorch 中創建張量
PyTorch 允許我們使用 torch 包以多種不同的方式創建張量。 創建張量的一種方法是通過指定其維度來初始化一個隨機張量
- describe(torch.Tensor(2, 3))
使用 Python 列表以聲明方式創建張量
我們還可以使用 python 列表創建張量。 我們只需要將列表作為參數傳遞給函數,我們就有了它的張量形式。
- x = torch.Tensor([[1, 2, 3],[4, 5, 6]])
- describe(x)
使用 NumPy 數組創建張量
我們也可以從NumPy 數組中創建PyTorch 張量。 張量的類型是 Double Tensor 而不是默認的 Float Tensor。 這對應于 NumPy 的數據類型是float64,如下所示。
- import numpy as np
- npy = np.random.rand(2, 3)
- describe(torch.from_numpy(npy))
我們不能用張量做什么?張量必須是實數或復數,不應是字符串或字符。
- torch.tensor([[1, 2], [3, 4, 5]])
- ---------------------------------------------------------------------------
- ValueError Traceback (most recent call last)
- <ipython-input-5-28787d136593> in <module>
- 1 # Example 3 - breaking (to illustrate when it breaks)
- ----> 2 torch.tensor([[1, 2], [3, 4, 5]])
- ValueError: expected sequence of length 2 at dim 1 (got 3)
torch.tensor() 構成了任何 PyTorch 項目的核心,從字面上看,因為它就是張量。
torch.sum()
此函數返回輸入張量中所有元素的總和。
- describe(torch.sum(x, dim=0,keepdims=True))
如果你了解 NumPy ,可能已經注意到,對于 2D 張量,我們將行表示為維度 0,將列表示為維度 1。torch.sum() 函數允許我們計算行和列的總和。
我們還為 keepdims 傳遞 True 以保留結果中的維度。 通過定義 dim = 1 我們告訴函數按列折疊數組。
- torch.sum(npy,dim=1,keepdims=True)
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-17-1617bf9e8a37> in <module>()
- 1 # Example 3 - breaking (to illustrate when it breaks)
- ----> 2 torch.sum(npy,dim=1,keepdims=True)
- TypeError: sum() received an invalid combination of arguments - got (numpy.ndarray, keepdims=bool, dim=int), but expected one of:
- * (Tensor input, *, torch.dtype dtype)
- didn't match because some of the keywords were incorrect: keepdims, dim
- * (Tensor input, tuple of ints dim, bool keepdim, *, torch.dtype dtype, Tensor out)
- * (Tensor input, tuple of names dim, bool keepdim, *, torch.dtype dtype, Tensor out)
該函數在計算指標和損失函數時非常有用。
torch.index_select()
這個函數返回一個新的張量,該張量使用索引中的條目(LongTensor)沿維度 dim 對輸入張量進行索引。
- indices = torch.LongTensor([0, 2])
- describe(torch.index_select(x, dim=1, index=indices))
我們可以將索引作為張量傳遞并將軸定義為 1,該函數返回一個新的張量大小 rows_of_original_tensor x length_of_indices_tensor。
- indices = torch.LongTensor([0, 0])
- describe(torch.index_select(x, dim=0, index=indices))
我們可以將索引作為張量傳遞并將軸定義為 0,該函數返回大小為
columns_of_original_tensor x length_of_indices_tensor 的新張量。
- indices = torch.FloatTensor([0, 2])
- describe(torch.index_select(x, dim=1, index=indices))
此函數在張量的非連續索引這種復雜索引中很有用。
torch.stack()
這將沿新維度連接一系列張量。
- describe(torch.stack([x, x, x],dim = 0))
我們可以將我們想要連接的張量作為一個張量列表傳遞,dim 為 0,以沿著行堆疊它。
- describe(torch.stack([x, x, x],dim = 1))
我們可以將我們想要連接的張量作為一個張量列表傳遞,dim 為 1,以沿著列堆疊它。
- y = torch.tensor([3,3])
- describe(torch.stack([x, y, x],dim = 1))
- --------------------------------------------------------------------------
- RuntimeError Traceback (most recent call last)
- <ipython-input-37-c97227f5da5c> in <module>()
- 1 # Example 3 - breaking (to illustrate when it breaks)
- 2 y = torch.tensor([3,3])
- ----> 3 describe(torch.stack([x, y, x],dim = 1))
- RuntimeError: stack expects each tensor to be equal size, but got [2, 3] at entry 0 and [2] at entry 1
該函數與torch.index_select()結合使用非常有用,可以壓扁矩陣。
torch.mm()
此函數執行矩陣的矩陣乘法。
- mat1 =torch.randn(3,2)
- describe(torch.mm(x, mat1))
只需將矩陣作為參數傳遞,我們就可以輕松地執行矩陣乘法,該函數將產生一個新的張量作為兩個矩陣的乘積。
- mat1 = np.random.randn(3,2)
- mat1 = torch.from_numpy(mat1).to(torch.float32)
- describe(torch.mm(x, mat1))
在上面的例子中,我們定義了一個 NumPy 數組然后將其轉換為 float32 類型的張量。 現在我們可以成功地對張量執行矩陣乘法。 兩個張量的數據類型必須匹配才能成功操作。
- mat1 =torch.randn(2,3)
- describe(torch.mm(x, mat1))
- ---------------------------------------------------------------------------
- RuntimeError Traceback (most recent call last)
- <ipython-input-62-18e7760efd23> in <module>()
- 1 # Example 3 - breaking (to illustrate when it breaks)
- 2 mat1 =torch.randn(2,3)
- ----> 3 describe(torch.mm(x, mat1))
- RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3)
為了執行成功的矩陣乘法運算,矩陣1的列和矩陣2的行必須匹配。 torch.mm() 函數遵循的是矩陣乘法的基本規則。 即使矩陣的順序相同,它仍然不會自動與另一個矩陣的轉置相乘,用戶必須手動定義它。
為了在反向傳播時計算導數,必須能夠有效地執行矩陣乘法,這就是 torch.mm () 出現的地方。
總結
我們對 5 個基本 PyTorch 函數的研究到此結束。 從基本的張量創建到具有特定用例的高級和鮮為人知的函數,如 torch.index_select (),PyTorch 提供了許多這樣的函數,使數據科學愛好者的工作更輕松。