Pytorch 基本使用
8 min readSep 9, 2024
這邊主要會介紹一些pytorch基本的使用,還有他跟原本python的資料類型要如何轉換等內容。
Outline
- 認識Tensor
- 如何建立tensor(從python list轉換、直接定義、從numpy轉換)
- 如何查看目前tensor的size
- 轉換tensor的維度(3*4 → 4*3)
- 查看特定tensor的數值(Slicing, index)
- Tensor之間的計算
認識Tensor
說到pytorch就必需要提到tensor,相信大家一開始聽到tensor可能會覺得很陌生,但其實tensor就跟我們之前在其他語言中學習到的array是異曲同工之妙,另外也是說pytorch的tensor他是可以使用在GPU上面。
- 1-D tensor → vector(一維的tensor可以說是數學上的vector)
- 2-D tensor → matrix(二維的tensor可以說是數學上的matrix)
- k-D tensor→ k-D (數學上k個維度)
1. 如何建立tensor
1–1. python list 到 tensor
我們可以很簡單的把python中的list直接轉換成pytorch中tensor的形式。使用torch.tensor(list)
ppython_list = [1, 2, 3, 4, 5]
pytorch_tensor = torch.tensor(python_list)
print(pytorch_tensor)
如果是「二維矩陣」也可以直接轉換
python_list = [[0, 1], [2, 3], [4, 5]]
python_tensor = torch.tensor(python_list)
print(python_tensor)
1-2. tensor到python list或是數字
tensor.tolist()
pytorch_tensor = torch.ones(2, 2)
python_list = pytorch_tensor.tolist()
python_num = pytorch_tensor[0,0].tolist()
1-3. numpy到tensor
torch.from_numpy(numpy_arr)
numpy_arr = numpy.ones(5)
python_torch = torch.from_numpy(numpy_arr)
1-4. tensor到numpy
tensor_name.numpy()
pytorch_tensor = torch.ones(5)
numpy_arr = pytorch_tensor.numpy()
2. 直接建立一個tensor
torch.Tensor(input_list)
:上一個所提到的方法,list轉換成tensortorch.zeros(2,2)
:建立全部都是0的2*2 tensortorch.ones(2,2)
:建立都是1的2*2 tensortorch.rand()
:建立random variable的tensortorch.randn()
:建立normal distribution的tensortorch.arange()
:建立有序列的tensor(等等看範例就可以知道)torch.linspace()
:建立有規則性的tensor(等等看範例就可以知道)
2–1. torch.zeros()
x_zeros = torch.zeros(2, 2, 2) # 維度一、維度二、維度三
print(x_zeros)
2–2. torch.ones()
x_ones = torch.ones(2, 2, 2)
print(x_ones)
2–3. torch.rand()
x_rand = torch.ones(2, 3, 4)
print(x_rand)
2–4. torch.arange(start, end, step)
x_arange = torch.arange(start=0, end=10, step=1)
print(x_arange)
2–5. torch.linspace(start, end, step): 呈現一個等差級數的概念
x_linspace = torch.arange(start=3, end=10, step=5)
print(x_linspace)
3. 如何查看目前tensor的size(.dtype, .shape)
.shape
:目前tensor的大小.dtype
:目前tensor的data type
3–1. shape 取得tensor大小
x.shape
:注意不需要括號
x = torch.randn(3, 2, 2)
print(x.shape) # 注意不需要括號()
print(x.shape[0], x.shape[1], x.shape[2])
print(x.size()) # 需要括號
dim1, dim2, dim3 = x.size() # 和shape不一樣,可以分別拆開
3–2. dtype: 指定data type
dtype
data = [[1, 0, 3], [0, 5, 0], [7, 0, 9]]
x_float = torch.tensor(data, dtype=torch.float)
x_bool = torch.tensor(data, dtype=torch.bool)
4. 轉換tensor的維度
這個在建立模型架構的時候很有用,因為建立架構時,常常會遇到維度不一緻的狀態,因此可以轉換維度,方便進行下一層的輸入。
.view()
.reshape()
4–1. x_view: 從5*4轉換到10*2維
x = torch.rand(5, 4)
x_view = x.view(10, 2)
print(x_view)
另一種方式,是將不同維度互換(.permute
),也就是說原本的是2*3*4
,permute
的方法就是在2*3*4
之間的維度互相變換,不會突然出現某個維度是5的狀況。
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print('Original tensor\n', x)
print('Shape of tensor:', x.shape)
# 進行彼此維度之間的轉換
x = x.permute(1, 0) #第一維跟第二維互換
print('After swapping:', x,shape)
5. 查看特定tensor的數值(Slicing, index)
當我們可能只需要某幾個維度的數值時,這個method就很好用
x = torch.arange(12).view(3, 4)
print(x)
只想取第一個數字
print(x[0, 0])
>>> tensor(0)
取第一維度
print(x[0,:])
print(x[0])
>>> tensor([0, 1, 2, 3])
取column 2
print(x[:,1])
>>> tensor([1, 5, 9])
取最後兩個維度
print(x[1:, :])
print(x[1:3, :]
進階題:
x = torch.arange(16*3).view(3, 4, 4)
# 取第一個維度
print(x[0])
# 想要取[0, 16, 32],也就是第一維度第一個)
print(x[:, 0, 0])
# 取[[32, 33], [36, 37]]
print(x[2, :2, :2])
6. Tensor之間的計算
跟一般的python差不多
x1 = torch.randint(low=0, high=10, size=(3,3))
x2 = torch.randint(low=0, high=10, size=(3,3))
y = x1 + x2
print("X1", x1)
print("X2", x2)
print("Y", y)
x = torch.ones((3,2,2))
print(x + 2)
print(x * 2)
print(x / 3)
torch.matmul
: matrix producttorch.mm
: 計算matrix product,「但不支援broadcasting」,下面會補充到什麼是broadcasting。
# Alternative to a.matmul(b)
# a @ b.T returns the same result since b is 1D tensor and the 2nd dimension
# is inferred
a = torch.ones((4,3)) * 6 # 4x3 matrix
b = torch.ones(3,2) * 2 # 3x2 matrix
print(a @ b) #和a.matmul(b)相同,不同寫法
**補充**
Broadcasting
: 當不同維度的矩陣在進行相加的時候,在計算過程中,會將比較小的陣列「自動」擴展成跟比較大的陣列相同,方面做計算。
import numpy as np
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
B = np.array([1, 2, 3])
# Broadcasting 會將 B 擴展為 3x3,然後進行相加
C = A + B
print(C)
>>> [[2, 4, 6],
[5, 7, 9],
[8, 10, 12]]
以上,希望也能幫助帶給大家一點幫助,我們下次見!