在 PyTorch 中建立張量
什麼是 Pytorch?
Pytorch 是一個基於 Python 的機器學習框架,主要用於科學計算和構建深度學習模型。
張量是機器學習和資料科學中一個流行的概念。張量是矩陣和向量的廣義術語。簡單來說,張量可以是任何東西——一個 n 維陣列、一個向量或一個矩陣。在本文中,我們將探討在 Pytorch 中建立張量的多種方法。
建立張量的方法
張量可以透過多種方法建立。下面列出了一些重要的方法。
使用 ones 建立張量
使用 zeros 建立張量
使用 arange 建立張量
使用 rand() 建立張量
使用 full() 建立張量
使用 linspace 建立張量
使用 randint() 建立張量
使用 eye() 方法建立張量
使用 tensor 方法建立張量
使用 complex 方法建立張量
作為第一步,在開始使用不同的建立張量的方法之前,我們必須首先匯入 torch。
語法
#importing torch import torch
方法 1:使用 ones() 建立張量
使用 ones() 函式,建立的張量的所有元素都設定為 1。
讓我們在 Python 程式設計中編寫一段程式碼來使用 ones() 函式建立張量。
示例
#creating a tensor using ones() tensor_using_ones = torch.ones(4,4,5) #prints the created tensor print(tensor_using_ones)
輸出
從下面的輸出中我們可以看到,新建立的張量在形狀 (4,4,5) 中的所有條目都設定為“1”。
tensor([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]])
方法 2:使用 zeros() 建立張量
我們使用 zeros() 函式來建立張量,其所有元素都設定為 0。使用此函式,值可以作為列表、元組或空值傳遞。張量可以透過這些方式中的任何一種建立。
使用 zeros() 函式,建立的張量所有元素都設定為 0。
示例
#creating a tensor using zeros() tensor_using_zeros = torch.zeros(4,4,5) #prints the created tensor print(tensor_using_zeros)
輸出
從下面的輸出中我們可以看到,新建立的張量在形狀 (4,4,5) 中的所有條目都設定為“0”。
tensor([[[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]])
方法 3:使用 arange() 建立張量 - 示例 1
arange() 函式用於建立一維 (1D) 張量。
語法
arange() 函式具有以下語法。
torch.arange(start,end,step-size)
其中,
start - 包含在內
end - 不包含在指定的形狀中
我們在 Python 中編寫程式碼以建立形狀規格為 (5,30,2) 的一維張量。
示例
#creating a tensor using arange tensor_using_arange = torch.arange(5, 30,2) #prints the created tensor print(tensor_using_arange)
輸出
這裡,建立了一個一維張量,其初始元素為 5,結束元素為 29(即 30-1),公差(步長)為“2”。
tensor([ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])
方法 4:使用 arange() 建立張量 - 示例 2
讓我們看看在 Pytorch 中建立張量的另一個示例,其中起始值為負數,即 -5,步長為“1”。
我們在 Python 中編寫程式碼以建立形狀規格為 (-5,25,1) 的一維張量。
示例
#creating a tensor using arange tensor_using_arange1 = torch.arange(-5, 25,1) #prints the created tensor print(tensor_using_arange1)
輸出
這裡,建立了一個一維張量,其初始元素為 -5,結束元素為 24(即 25-1),公差(步長)為“1”。
tensor([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
方法 5:使用 rand() 建立張量
在 Python 中建立張量的另一種常用方法是 rand() 方法。使用 rand() 方法將為指定的形狀建立包含隨機數的張量。
語法
rand() 方法的語法如下所示 -
torch.rand(shape)
示例
現在,為了理解使用 rand() 方法建立張量,讓我們在 Python 程式設計中編寫一段程式碼。
#creating a tensor using rand() tensor_using_rand = torch.rand(5, 5) #prints the created tensor print(tensor_using_rand)
輸出
從下面的輸出中我們可以看到,使用 rand() 方法建立了一個 5X5 形狀的張量。張量內的元素是均勻分佈的隨機數。
tensor( [[0.2000, 0.6068, 0.3968, 0.6269, 0.8587], [0.1754, 0.5050, 0.8569, 0.5139, 0.9026], [0.2634, 0.6543, 0.3015, 0.6873, 0.2214], [0.9257, 0.6045, 0.4763, 0.5107, 0.7925], [0.7465, 0.0279, 0.4937, 0.0989, 0.8806]])
方法 6:使用 full() 建立張量
當希望張量中的元素為相同的數字時,可以使用張量中的 full() 方法。
語法
full() 方法的語法如下。
torch.full(size, fill_value, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
其中,
fill_value - 是我們希望在張量中使用的值(相同的數字)。
假設,如果我們想建立一個所有元素都為相同數字(4)的張量。我們可以按照下面所示在 Python 程式設計中編寫程式碼。fill_value 指定為 4。該值將是張量中的公共元素。
示例
#creating a tensor using full method tensor1 = torch.full((5,5,4) #prints the created tensor print(tensor1)
輸出
對於上面的程式碼,我們給出一個 5X5 矩陣的張量,其中所有元素都為 4。
tensor([[4, 4, 4, 4, 4],
[4, 4, 4, 4, 4],
[4, 4, 4, 4, 4],
[4, 4, 4, 4, 4],
[4, 4, 4, 4, 4]])
方法 7:使用 linspace 建立張量
語法
Linspace() 方法具有以下語法。
linspace (start, stop, num, endpoint=True, retstep=False, dtype=None, axis=0)
其中,
low - 起始元素(包含在內)
high- 結束元素(包含在內)
num- 要在張量中生成的元素數量
示例
我們使用 Linspace() 方法建立一維 (1D) 張量。例如,我們在此處指定的形狀為 (2,20,4)。在我們的示例中,2 是起始元素,20 是結束元素,4 是一維張量中存在的元素數量。
#creating a tensor using linspace tensor_using_linspace = torch.linspace(2, 20, 4) #prints the created tensor print(tensor_using_linspace)
輸出
我們可以看到,上面的程式碼產生了下面的輸出,其中建立了一個包含 4 個元素的一維張量。
tensor([ 2., 8., 14., 20.])
方法 8:使用 randint() 建立張量
randint() 方法建立一個張量,其中隨機整數根據指定的形狀生成。
語法
torch.randint(low,high,shape)
其中,
low - 起始元素(包含在內)
high- 結束元素(不包含在內)
shape - 張量的形狀
示例
我們使用 randint() 方法編寫 Python 程式碼,如下所示。
#creating a tensor using randint tensor_using_randint = torch.randint(10,100, (2,2)) #prints the created tensor print(tensor_using_randint)
輸出
在下面的輸出中,我們看到一個張量,其中隨機元素在形狀 (10,100,(5,5)) 之間生成。在這裡,我們得到一個張量,其中隨機元素在 10 和 99 之間,形狀為 5X5 矩陣。
tensor([[71, 32, 56, 34, 21],
[37, 71, 98, 63, 40],
[41, 45, 27, 41, 50],
[51, 42, 43, 71, 14],
[87, 52, 77, 88, 85]])
方法 9:使用 eye() 建立張量
當我們想要建立一個二維張量,其中對角線上的元素為 1,其餘元素為零時,eye() 方法很有用。
語法
torch.eye (shape)
現在,我們使用 eye() 方法編寫 Python 程式碼。
示例 -1
#creating a tensor using eye tensor1_using_eye = torch.eye(4, 4) #prints the created tensor print(tensor1_using_eye)
輸出
我們現在使用 eye() 方法建立了一個 2D 4X4 張量。
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
讓我們看看使用 eye 函式建立張量的另一個示例。
示例 -2
#creating a tensor using eye tensor2_using_eye = torch.eye(3) #prints the created tensor print(tensor2_using_eye)
輸出
我們現在使用 eye() 方法建立了一個 2D 3X3 張量,如下面的輸出所示。
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
方法 10:使用 Complex() 方法建立張量
complex() 方法是我們將要介紹的最後一種方法。它用於建立複數張量,其中建立的張量將同時具有虛部和實部。
語法
complex() 方法具有以下語法。
torch.complex(real part, imaginary part)
示例
complex() 方法的程式碼如下所示。
real_tensor = torch.rand(4,5) print(real_tensor) imaginary_tensor = torch.rand(4, 5) print(imaginary_tensor) tensor_using_complex = torch.complex(real_tensor, imaginary_tensor) #prints created tensor print(tensor_using_complex )
輸出
因此,我們建立了一個複數張量,其虛部和實部形狀為 4X5。
Tensor([[0.8284, 0.7833, 0.4952, 0.7634, 0.9098],
[0.6373, 0.3428, 0.0352, 0.6095, 0.8066],
[0.7292, 0.0703, 0.3846, 0.6653, 0.1924],
[0.2920, 0.2676, 0.4669, 0.2841, 0.5974]])
tensor([[0.5236, 0.9037, 0.7622, 0.7044, 0.3362],
[0.8543, 0.6088, 0.4882, 0.0563, 0.9318],
[0.7075, 0.1176, 0.6272, 0.3318, 0.3751],
[0.6317, 0.1482, 0.3772, 0.6941, 0.9126]])
tensor([[0.8284+0.5236j, 0.7833+0.9037j, 0.4952+0.7622j, 0.7634+0.7044j, 0.9098+0.3362j],
[0.6373+0.8543j, 0.3428+0.6088j, 0.0352+0.4882j, 0.6095+0.0563j, 0.8066+0.9318j],
[0.7292+0.7075j, 0.0703+0.1176j, 0.3846+0.6272j, 0.6653+0.3318j, 0.1924+0.3751j],
[0.2920+0.6317j, 0.2676+0.1482j, 0.4669+0.3772j, 0.2841+0.6941j, 0.5974+0.9126j]])
結論
透過本文,我們學習了在 Pytorch 中建立張量的多種方法。我們建議您也檢視我們的文章。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP