五個(gè)PyTorch 中的處理張量的基本函數(shù)
能夠以準(zhǔn)確有效的方式構(gòu)建神經(jīng)網(wǎng)絡(luò)是招聘人員在深度學(xué)習(xí)工程師中最受追捧的技能之一。PyTorch 是一個(gè) 主要用于深度學(xué)習(xí)的Python 庫。 PyTorch 最基本也是最重要的部分之一是創(chuàng)建張量,張量是數(shù)字、向量、矩陣或任何 n 維數(shù)組。在構(gòu)建神經(jīng)網(wǎng)絡(luò)時(shí)為了降低計(jì)算速度必須避免使用顯式循環(huán),我們可以使用矢量化操作來避免這種循環(huán)。在構(gòu)建神經(jīng)網(wǎng)絡(luò)時(shí),足夠快地計(jì)算矩陣運(yùn)算的能力至關(guān)重要。
“為什么不使用 NumPy 庫呢?”
對于深度學(xué)習(xí),我們需要計(jì)算模型參數(shù)的導(dǎo)數(shù)。 PyTorch 提供了在反向傳播時(shí)跟蹤導(dǎo)數(shù)的能力而 NumPy 則沒有,這在Pytorch中被稱為“Auto Grad”。PyTorch 為使用 GPU 的快速執(zhí)行提供了內(nèi)置支持。這在訓(xùn)練模型方面至關(guān)重要。由于 Numpy 缺乏將其計(jì)算轉(zhuǎn)移到 GPU 的能力,因此訓(xùn)練模型的時(shí)間最終會變得非常大。
所有使用 PyTorch 的深度學(xué)習(xí)項(xiàng)目都從創(chuàng)建張量開始。讓我們看看一些必須知道的函數(shù),它們是任何涉及構(gòu)建神經(jīng)網(wǎng)絡(luò)的深度學(xué)習(xí)項(xiàng)目的支柱。
- torch.tensor()
- torch.sum()
- torch.index_select()
- torch.stack()
- torch.mm()
在安裝完P(guān)ytorch后,在代碼中可以直接導(dǎo)入:
- # Import torch and other required modules
- import torch
torch.tensor()
首先,我們定義了一個(gè)輔助函數(shù),describe (x),它將總結(jié)張量 x 的各種屬性,例如張量的類型、張量的維度和張量的內(nèi)容。
- # Helper function
- def describe(x):
- print("Type: {}".format(x.type()))
- print("Shape/size: {}".format(x.shape))
- print("Values: \n{}".format(x)
使用 torch.Tensor 在 PyTorch 中創(chuàng)建張量
PyTorch 允許我們使用 torch 包以多種不同的方式創(chuàng)建張量。 創(chuàng)建張量的一種方法是通過指定其維度來初始化一個(gè)隨機(jī)張量
- describe(torch.Tensor(2, 3))
使用 Python 列表以聲明方式創(chuàng)建張量
我們還可以使用 python 列表創(chuàng)建張量。 我們只需要將列表作為參數(shù)傳遞給函數(shù),我們就有了它的張量形式。
- x = torch.Tensor([[1, 2, 3],[4, 5, 6]])
- describe(x)
使用 NumPy 數(shù)組創(chuàng)建張量
我們也可以從NumPy 數(shù)組中創(chuàng)建PyTorch 張量。 張量的類型是 Double Tensor 而不是默認(rèn)的 Float Tensor。 這對應(yīng)于 NumPy 的數(shù)據(jù)類型是float64,如下所示。
- import numpy as np
- npy = np.random.rand(2, 3)
- describe(torch.from_numpy(npy))
我們不能用張量做什么?張量必須是實(shí)數(shù)或復(fù)數(shù),不應(yīng)是字符串或字符。
- 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() 構(gòu)成了任何 PyTorch 項(xiàng)目的核心,從字面上看,因?yàn)樗褪菑埩俊?/p>
torch.sum()
此函數(shù)返回輸入張量中所有元素的總和。
- describe(torch.sum(x, dim=0,keepdims=True))
如果你了解 NumPy ,可能已經(jīng)注意到,對于 2D 張量,我們將行表示為維度 0,將列表示為維度 1。torch.sum() 函數(shù)允許我們計(jì)算行和列的總和。
我們還為 keepdims 傳遞 True 以保留結(jié)果中的維度。 通過定義 dim = 1 我們告訴函數(shù)按列折疊數(shù)組。
- 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)
該函數(shù)在計(jì)算指標(biāo)和損失函數(shù)時(shí)非常有用。
torch.index_select()
這個(gè)函數(shù)返回一個(gè)新的張量,該張量使用索引中的條目(LongTensor)沿維度 dim 對輸入張量進(jìn)行索引。
- indices = torch.LongTensor([0, 2])
- describe(torch.index_select(x, dim=1, index=indices))
我們可以將索引作為張量傳遞并將軸定義為 1,該函數(shù)返回一個(gè)新的張量大小 rows_of_original_tensor x length_of_indices_tensor。
- indices = torch.LongTensor([0, 0])
- describe(torch.index_select(x, dim=0, index=indices))
我們可以將索引作為張量傳遞并將軸定義為 0,該函數(shù)返回大小為
columns_of_original_tensor x length_of_indices_tensor 的新張量。
- indices = torch.FloatTensor([0, 2])
- describe(torch.index_select(x, dim=1, index=indices))
此函數(shù)在張量的非連續(xù)索引這種復(fù)雜索引中很有用。
torch.stack()
這將沿新維度連接一系列張量。
- describe(torch.stack([x, x, x],dim = 0))
我們可以將我們想要連接的張量作為一個(gè)張量列表傳遞,dim 為 0,以沿著行堆疊它。
- describe(torch.stack([x, x, x],dim = 1))
我們可以將我們想要連接的張量作為一個(gè)張量列表傳遞,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
該函數(shù)與torch.index_select()結(jié)合使用非常有用,可以壓扁矩陣。
torch.mm()
此函數(shù)執(zhí)行矩陣的矩陣乘法。
- mat1 =torch.randn(3,2)
- describe(torch.mm(x, mat1))
只需將矩陣作為參數(shù)傳遞,我們就可以輕松地執(zhí)行矩陣乘法,該函數(shù)將產(chǎn)生一個(gè)新的張量作為兩個(gè)矩陣的乘積。
- mat1 = np.random.randn(3,2)
- mat1 = torch.from_numpy(mat1).to(torch.float32)
- describe(torch.mm(x, mat1))
在上面的例子中,我們定義了一個(gè) NumPy 數(shù)組然后將其轉(zhuǎn)換為 float32 類型的張量。 現(xiàn)在我們可以成功地對張量執(zhí)行矩陣乘法。 兩個(gè)張量的數(shù)據(jù)類型必須匹配才能成功操作。
- 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)
為了執(zhí)行成功的矩陣乘法運(yùn)算,矩陣1的列和矩陣2的行必須匹配。 torch.mm() 函數(shù)遵循的是矩陣乘法的基本規(guī)則。 即使矩陣的順序相同,它仍然不會自動與另一個(gè)矩陣的轉(zhuǎn)置相乘,用戶必須手動定義它。
為了在反向傳播時(shí)計(jì)算導(dǎo)數(shù),必須能夠有效地執(zhí)行矩陣乘法,這就是 torch.mm () 出現(xiàn)的地方。
總結(jié)
我們對 5 個(gè)基本 PyTorch 函數(shù)的研究到此結(jié)束。 從基本的張量創(chuàng)建到具有特定用例的高級和鮮為人知的函數(shù),如 torch.index_select (),PyTorch 提供了許多這樣的函數(shù),使數(shù)據(jù)科學(xué)愛好者的工作更輕松。