自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

五個(gè)PyTorch 中的處理張量的基本函數(shù)

人工智能 深度學(xué)習(xí)
每個(gè)深度學(xué)習(xí)初學(xué)者都應(yīng)該知道這5個(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)入:

 

  1. # Import torch and other required modules 
  2. import torch 

 

torch.tensor()

首先,我們定義了一個(gè)輔助函數(shù),describe (x),它將總結(jié)張量 x 的各種屬性,例如張量的類型、張量的維度和張量的內(nèi)容。

 

  1. # Helper function 
  2. def describe(x): 
  3.   print("Type: {}".format(x.type())) 
  4.   print("Shape/size: {}".format(x.shape)) 
  5.   print("Values: \n{}".format(x) 

 

使用 torch.Tensor 在 PyTorch 中創(chuàng)建張量

PyTorch 允許我們使用 torch 包以多種不同的方式創(chuàng)建張量。 創(chuàng)建張量的一種方法是通過指定其維度來初始化一個(gè)隨機(jī)張量

 

  1. describe(torch.Tensor(2, 3)) 

使用 Python 列表以聲明方式創(chuàng)建張量

我們還可以使用 python 列表創(chuàng)建張量。 我們只需要將列表作為參數(shù)傳遞給函數(shù),我們就有了它的張量形式。

 

  1. x = torch.Tensor([[1, 2, 3],[4, 5, 6]])  
  2. describe(x) 

 

使用 NumPy 數(shù)組創(chuàng)建張量

我們也可以從NumPy 數(shù)組中創(chuàng)建PyTorch 張量。 張量的類型是 Double Tensor 而不是默認(rèn)的 Float Tensor。 這對應(yīng)于 NumPy 的數(shù)據(jù)類型是float64,如下所示。

 

  1. import numpy as np 
  2. npy = np.random.rand(2, 3) 
  3. describe(torch.from_numpy(npy)) 

 

我們不能用張量做什么?張量必須是實(shí)數(shù)或復(fù)數(shù),不應(yīng)是字符串或字符。

 

  1. torch.tensor([[1, 2], [3, 4, 5]]) 
  2.  
  3.  
  4. --------------------------------------------------------------------------- 
  5. ValueError                                Traceback (most recent call last
  6. <ipython-input-5-28787d136593> in <module> 
  7.       1 # Example 3 - breaking (to illustrate when it breaks) 
  8. ----> 2 torch.tensor([[1, 2], [3, 4, 5]]) 
  9.  
  10. ValueError: expected sequence of length 2 at dim 1 (got 3) 

 

torch.tensor() 構(gòu)成了任何 PyTorch 項(xiàng)目的核心,從字面上看,因?yàn)樗褪菑埩俊?/p>

 

torch.sum()

此函數(shù)返回輸入張量中所有元素的總和。

 

  1. 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ù)組。

 

  1. torch.sum(npy,dim=1,keepdims=True
  2.  
  3. --------------------------------------------------------------------------- 
  4. TypeError                                 Traceback (most recent call last
  5. <ipython-input-17-1617bf9e8a37> in <module>() 
  6.       1 # Example 3 - breaking (to illustrate when it breaks) 
  7. ----> 2 torch.sum(npy,dim=1,keepdims=True) 
  8.  
  9. TypeError: sum() received an invalid combination of arguments - got (numpy.ndarray, keepdims=bool, dim=int), but expected one of
  10.  * (Tensor input, *, torch.dtype dtype) 
  11.       didn't match because some of the keywords were incorrect: keepdims, dim 
  12.  * (Tensor input, tuple of ints dim, bool keepdim, *, torch.dtype dtype, Tensor out
  13.  * (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)行索引。

 

  1. indices = torch.LongTensor([0, 2]) 
  2. describe(torch.index_select(x, dim=1, index=indices)) 

 

我們可以將索引作為張量傳遞并將軸定義為 1,該函數(shù)返回一個(gè)新的張量大小 rows_of_original_tensor x length_of_indices_tensor。

 

  1. indices = torch.LongTensor([0, 0]) 
  2. describe(torch.index_select(x, dim=0, index=indices)) 

 

我們可以將索引作為張量傳遞并將軸定義為 0,該函數(shù)返回大小為

columns_of_original_tensor x length_of_indices_tensor 的新張量。

 

  1. indices = torch.FloatTensor([0, 2]) 
  2. describe(torch.index_select(x, dim=1, index=indices)) 

此函數(shù)在張量的非連續(xù)索引這種復(fù)雜索引中很有用。

torch.stack()

這將沿新維度連接一系列張量。

 

  1. describe(torch.stack([x, x, x],dim = 0)) 

我們可以將我們想要連接的張量作為一個(gè)張量列表傳遞,dim 為 0,以沿著行堆疊它。

 

  1. describe(torch.stack([x, x, x],dim = 1)) 

我們可以將我們想要連接的張量作為一個(gè)張量列表傳遞,dim 為 1,以沿著列堆疊它。

 

  1. y = torch.tensor([3,3]) 
  2. describe(torch.stack([x, y, x],dim = 1)) 
  3.  
  4. -------------------------------------------------------------------------- 
  5. RuntimeError                              Traceback (most recent call last
  6. <ipython-input-37-c97227f5da5c> in <module>() 
  7.       1 # Example 3 - breaking (to illustrate when it breaks) 
  8.       2 y = torch.tensor([3,3]) 
  9. ----> 3 describe(torch.stack([x, y, x],dim = 1)) 
  10.  
  11. 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í)行矩陣的矩陣乘法。

 

  1. mat1 =torch.randn(3,2) 
  2. describe(torch.mm(x, mat1)) 

 

只需將矩陣作為參數(shù)傳遞,我們就可以輕松地執(zhí)行矩陣乘法,該函數(shù)將產(chǎn)生一個(gè)新的張量作為兩個(gè)矩陣的乘積。

 

  1. mat1 = np.random.randn(3,2) 
  2. mat1 = torch.from_numpy(mat1).to(torch.float32) 
  3. describe(torch.mm(x, mat1)) 

 

在上面的例子中,我們定義了一個(gè) NumPy 數(shù)組然后將其轉(zhuǎn)換為 float32 類型的張量。 現(xiàn)在我們可以成功地對張量執(zhí)行矩陣乘法。 兩個(gè)張量的數(shù)據(jù)類型必須匹配才能成功操作。

 

  1. mat1 =torch.randn(2,3) 
  2. describe(torch.mm(x, mat1)) 
  3.  
  4. --------------------------------------------------------------------------- 
  5. RuntimeError                              Traceback (most recent call last
  6. <ipython-input-62-18e7760efd23> in <module>() 
  7.       1 # Example 3 - breaking (to illustrate when it breaks) 
  8.       2 mat1 =torch.randn(2,3) 
  9. ----> 3 describe(torch.mm(x, mat1)) 
  10.  
  11. 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é)愛好者的工作更輕松。

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2022-11-15 16:37:38

PyTorch抽樣函數(shù)子集

2023-02-13 16:42:08

云計(jì)算CloudOps工具

2024-10-22 15:51:42

PyTorch張量

2024-08-14 16:06:02

2023-12-27 14:19:33

Python內(nèi)置函數(shù)開發(fā)

2021-11-05 12:59:51

深度學(xué)習(xí)PytorchTenso

2020-07-03 11:30:12

首席信息官數(shù)據(jù)資產(chǎn)

2020-07-03 14:06:37

大數(shù)據(jù)CIO技術(shù)

2021-08-11 09:33:15

Vue 技巧 開發(fā)工具

2024-03-01 20:55:40

Pytorch張量Tensor

2024-07-29 10:46:50

2015-11-12 10:45:11

問題系統(tǒng)Linux

2024-10-07 08:37:34

PyPDF2PDF代碼

2021-07-27 18:02:01

VueUse 函數(shù)開發(fā)

2010-05-27 17:45:13

MySQL存儲過程

2023-05-09 15:01:43

JavaScript編程語言異常處理

2022-08-29 00:37:53

Python技巧代碼

2022-08-23 14:57:43

Python技巧函數(shù)

2022-02-23 21:22:52

首席數(shù)據(jù)官CIO

2025-01-07 13:58:08

SQL數(shù)據(jù)處理函數(shù)數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號