PyTorch張量操作


PyTorch是一個開源機器學習框架,廣泛應用於學術研究和工業界。它提供了豐富的工具和庫,可以有效地構建和訓練神經網路。

PyTorch中的張量

在PyTorch中,張量是一個多維數值陣列,可用於表示機器學習模型的資料。維度可以是1D、2D、3D等等,完全取決於它們表示的資料的複雜性。

例如,一維張量可以用來表示一系列值,例如時間序列資料,而二維張量可以用來表示影像,其中張量的每個元素對應影像中的一個畫素。

張量操作

張量操作是在張量上執行的數學運算,用於操作和轉換其值。PyTorch提供了一系列張量操作,可用於對張量執行算術、統計和邏輯運算等基本操作。

這些操作實現為函式,它們接受一個或多個張量作為輸入,並返回一個新的張量作為輸出。

示例

在PyTorch中,我們使用以下示例解釋如何建立張量,它將接受一個值列表或元組作為輸入。

import torch
# create a 2D tensor using a nested list
a = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
# print the tensor
print(a)

輸出

tensor([[1, 2, 3, 4],
         [5, 6, 7, 8]])

加法

可以使用`torch.add()`函式或`+`運算子執行張量加法。

示例

import torch
# create a 2D tensor using a nested list
a = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
b = torch.add(a, 5)
c = a + b
print(b)
print(c)

輸出

tensor([[6, 7, 8, 9],
        [10, 11, 12, 13]])

減法

可以使用`torch.sub()`函式或`-`運算子執行張量減法。

示例

# subtract a scalar value of 2 from each element of the tensor to create a new tensor
d = torch.sub(a, 2)
# subtract the modified tensor from the original tensor to create a new tensor
e = a - d
print(d)
print(e)

輸出

tensor([[-1, 0, 1, 2],  [3, 4, 5, 6]])
tensor([[2, 2, 2, 2],  [2, 2, 2, 2]])

乘法

可以使用`torch.mul()`函式或`*`運算子執行張量乘法。

示例

# multiply each element of the tensor by a scalar value of 2 to create a new tensor
f = torch.mul(a, 2)
g = a * f
print(f)
print(g)

輸出

tensor([[2, 4, 6, 8],
        [10, 12, 14, 16]])
tensor([[2, 8, 18, 32],
        [50, 72, 98,128]])

除法

可以使用`torch.div()`函式或`/`運算子執行張量除法。

示例

# divide each element of the tensor by a scalar value of 2 to create a new tensor
h = torch.div(a, 2)

# divide the original tensor element-wise by the modified tensor to create a new tensor
i = a / h
print(h)
print(i)

輸出

tensor([[0.5000, 1.0000, 1.5000, 2.0000],
        [2.5000, 3.0000, 3.5000, 4.0000]])
tensor([[2., 2., 2., 2.],
        [2., 2., 2., 2.]])

高階張量操作

高階張量操作包括矩陣乘法、轉置、重塑和連線,這些操作主要處理二維張量。

矩陣乘法

可以使用`torch.mm()`函式或`@`運算子執行矩陣乘法。

示例

# create two 2D tensors
A = torch.tensor([[1, 2], [3, 4]])
B = torch.tensor([[5, 6], [7, 8]])

# perform matrix multiplication using torch.mm() function
C = torch.mm(A, B)

# we can use the @ operator for matrix multiplication
D = A @ B
print(C)
print(D)

輸出

tensor([[19, 22],
        [43, 50]])
tensor([[19, 22],
        [43, 50]])

轉置

在張量操作中,轉置是翻轉張量軸的過程。它涉及交換二維張量的行和列,或者更一般地,交換任何維度的張量的軸。

可以使用`torch.t()`函式執行轉置。

示例

# create a 2D tensor
E = torch.tensor([[1, 2], [3, 4], [5, 6]])

# transpose the tensor using torch.t() function
F = torch.t(E)
print(E)
print(F)

輸出

tensor([[1, 2],
        [3, 4],
        [5, 6]])
tensor([[1, 3, 5],
        [2, 4, 6]])

重塑

在張量操作中,重塑是改變張量形狀或維度的過程,同時保留其底層資料。它涉及重新排列張量的元素以適應新的形狀,而不改變元素的總數。

可以使用`torch.reshape()`函式或`.view()`方法執行重塑。

示例

# create a 2D tensor
G = torch.tensor([[1, 2, 3], [4, 5, 6]])

# reshape the tensor using torch.reshape() function
H = torch.reshape(G, (3, 2))

# reshape the tensor using the .view() method
I = G.view(3, 2)

輸出

tensor([[1, 2, 3],
        [4, 5, 6]])
tensor([[1, 2],
        [3, 4],
        [5, 6]])
tensor([[1, 2],
        [3, 4],
        [5, 6]])

連線

在張量操作中,連線是在特定維度上連線兩個或多個張量以形成更大的張量的過程。生成的張量具有一個新的維度,它是輸入張量的原始維度的連線。

可以使用`torch.cat()`函式執行連線。

示例

# create two tensors
J = torch.tensor([[1, 2], [3, 4]])
K = torch.tensor([[5, 6]])

# concatenate the tensors along the first axis
L = torch.cat((J, K), dim=0)
print(J)
print(K)
print(L)

輸出

tensor([[1, 2],
        [3, 4]])
tensor([[5, 6]])
tensor([[1, 2],
        [3, 4],
       [5, 6]])

結論

PyTorch的應用領域包括計算機視覺、自然語言處理、語音識別、推薦系統和自動駕駛汽車。

Facebook人工智慧研究 (FAIR) 廣泛使用 PyTorch 來研究和開發最先進的深度學習模型。英偉達、特斯拉和 IBM 也使用 PyTorch 來開發自動駕駛系統、各種計算機視覺應用程式以及自然語言處理模型。

更新於:2023年8月7日

瀏覽量:389

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.