๋ฐ์ํ
๐ฉTensor๋?
- Gpu ์์์ ๋ณ๋ ฌ ์ฐ์ฐ์ ์ ์ํํ๊ธฐ ์ํด์ ์ฌ์ฉํ๋ ์๋ฃํ
- ์๋ ๋ฏธ๋ถ ์ต์ ํ์ GPU์์ ์คํ๋๋ค.

๐ง ์์ฑ
import torch
import numpy as np
# tensor()
x = torch.tensor([1.0, 2.0, 3.0])
# from_numpy()
arr = np.array([4.0, 5.0, 6.0])
y = torch.from_numpy(arr)
| torch.tensor(data) | Python ๋ฆฌ์คํธ/๋ํ์ด ๋ฐฐ์ด ๋ฑ์ ํ ์๋ก ์์ฑ |
| torch.from_numpy(ndarray) | NumPy ๋ฐฐ์ด์ ๊ณต์ ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฐ์ผ๋ก ํ ์๋ก ๋ณํ (๋ฐ์ดํฐ ๊ณต์ ๋จ) |
๐ ํํ ๋ณํ
x = torch.tensor([[1, 2], [3, 4]])
# to()
x.to(torch.float32)
# transpose()
x.T # ๋๋ x.transpose(0, 1)
# reshape()
x.reshape(4)
# view()
x.view(-1) # reshape๊ณผ ๋น์ท (๋ฉ๋ชจ๋ฆฌ ์ฐ์์ฑ ํ์)
# squeeze()
torch.tensor([[[1], [2], [3]]]).squeeze() # → [1, 2, 3]
# unsqueeze()
torch.tensor([1, 2, 3]).unsqueeze(0) # → [[1, 2, 3]]
# permute()
x = torch.randn(2, 3, 4)
x.permute(2, 0, 1).shape # → (4, 2, 3)
# flatten()
x = torch.tensor([[1, 2], [3, 4]])
x.flatten() # → [1, 2, 3, 4]
# unflatten()
x = torch.tensor([1, 2, 3, 4])
x.unflatten(0, (2, 2)) # → [[1, 2], [3, 4]]
| to(device) | CPU ↔ GPU๋ก ๋ฐ์ดํฐ ์ด๋ |
| transpose(dim0, dim1) | ๋ ์ฐจ์ ์์น ๋ฐ๊ฟ |
| reshape(shape) | ์๋ก์ด shape์ผ๋ก ๋ณํ (๋ณต์ฌ/๋ทฐ ๋ ๋ค ๊ฐ๋ฅ) |
| view(shape) | reshape๊ณผ ์ ์ฌํ๋, ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ์ผ ๋๋ง ๊ฐ๋ฅ |
| squeeze(dim) | ์ฐจ์์ด 1์ธ dim์ ์ ๊ฑฐ (์: [1, 3, 1] → [3]) |
| unsqueeze(dim) | dim์ ์ฐจ์ 1์ ์ถ๊ฐ (์: [3] → [1, 3]) |
| permute(dims) | ์ฐจ์์ ์์๋ฅผ ์์๋ก ์ฌ๋ฐฐ์ด |
| flatten(start_dim, end_dim) | ํน์ ์ฐจ์ ๋ฒ์๋ฅผ 1์ฐจ์์ผ๋ก ํํํ |
| unflatten(dim, sizes) | flatten์ ๋๋๋ฆฌ๋ ํจ์ |
๐งฉ ๊ฒฐํฉ ๋ฐ ๋ถํ
a = torch.tensor([[1, 2]])
b = torch.tensor([[3, 4]])
# cat()
torch.cat([a, b], dim=0) # → [[1, 2], [3, 4]]
# stack()
torch.stack([a, b], dim=0) # → [[[1, 2]], [[3, 4]]]
# split()
x = torch.tensor([1, 2, 3, 4])
torch.split(x, 2) # → ([1, 2], [3, 4])
# chunk()
x = torch.tensor([1, 2, 3, 4])
torch.chunk(x, 2) # → ([1, 2], [3, 4])
# unbind()
x = torch.tensor([[1, 2], [3, 4]])
torch.unbind(x, dim=0) # → (tensor([1, 2]), tensor([3, 4]))
| cat(tensors, dim) | ์ฃผ์ด์ง dim์ ๊ธฐ์ค์ผ๋ก ํ ์ ์ฐ๊ฒฐ |
| stack(tensors, dim) | ์๋ก์ด dim์ ๋ง๋ค๋ฉฐ ํ ์๋ฅผ ์ฐ๊ฒฐ |
| split(tensor, split_size, dim) | ํน์ ํฌ๊ธฐ๋ก ๋๋๊ธฐ |
| chunk(tensor, chunks, dim) | ๊ท ๋ฑํ๊ฒ ๋๋๊ธฐ |
| unbind(tensor, dim) | ํน์ ์ฐจ์์ ๊ธฐ์ค์ผ๋ก ํ ์๋ฅผ ํํ๋ก ๋ถํด |
๐ ์ธ๋ฑ์ฑ ๋ฐ ์ฌ๋ผ์ด์ฑ
x = torch.tensor([[10, 20], [30, 40]])
# ์ผ๋ฐ ์ธ๋ฑ์ฑ
x[0, 1] # → 20
# ์ฌ๋ผ์ด์ฑ
x[:, 1] # → [20, 40]
# gather()
x = torch.tensor([[1, 2], [3, 4]])
index = torch.tensor([[0, 1], [1, 0]])
torch.gather(x, 1, index) # → [[1, 2], [4, 3]]
# index_select()
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
idx = torch.tensor([0, 2])
torch.index_select(x, 1, idx) # → [[1, 3], [4, 6]]
| tensor[i], tensor[i, j] | ์ผ๋ฐ ์ธ๋ฑ์ฑ/์ฌ๋ผ์ด์ฑ |
| gather(dim, index) | ์ฃผ์ด์ง index์ ๋ฐ๋ผ ์์ ์ ํ |
| index_select(dim, index) | ์ฃผ์ด์ง index๋ง ์ ํํ์ฌ ํ ์ ์์ฑ |
โ ์ฐ์ฐ
์ฌ์น์ฐ์ฐ
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
a.add(b) # → [4, 6]
a.sub(b) # → [-2, -2]
a.mul(b) # → [3, 8]
a.div(b) # → [0.3333, 0.5]
| add, sub, mul, div | ๋ง์ , ๋บ์ , ๊ณฑ์ , ๋๋์ (๋ธ๋ก๋์บ์คํ ์ง์) |
์ง๊ณ ์ฐ์ฐ
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
x.sum() # → 10.0
x.mean() # → 2.5
x.max() # → 4.0
x.min() # → 1.0
x.std() # → ํ์คํธ์ฐจ
| sum, mean, max, min, std | ํ ์์ ํฉ, ํ๊ท , ์ต๋๊ฐ, ์ต์๊ฐ, ํ์คํธ์ฐจ ๋ฑ ๊ณ์ฐ (dim ์ง์ ๊ฐ๋ฅ) |
๋น๊ต ์ฐ์ฐ
a = torch.tensor([1, 2, 3])
b = torch.tensor([2, 2, 1])
a.eq(b) # → [False, True, False]
a.gt(b) # → [False, False, True]
a.lt(b) # → [True, False, False]
| eq, ne, gt, lt | ๊ฐ์, ๋ค๋ฆ, ํผ, ์์ ๋น๊ต ๊ฒฐ๊ณผ๋ฅผ Boolean Tensor๋ก ๋ฐํ |
๐ ๊ธฐํ ํจ์
x = torch.tensor([1.0, 2.0, 3.0])
# numpy()
x.numpy() # → NumPy ๋ฐฐ์ด๋ก ๋ณํ
# tolist()
x.tolist() # → [1.0, 2.0, 3.0]
# clone()
y = x.clone()
# to()
x.to(torch.float64)
# expand()
x = torch.tensor([[1], [2], [3]]) # shape: (3, 1)
x.expand(3, 4) # shape: (3, 4), ๊ฐ ๋ณต์ ์๋
# repeat()
x = torch.tensor([[1], [2], [3]])
x.repeat(1, 3) # → [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
# detach()
x = torch.tensor([1.0], requires_grad=True)
y = x * 2
y = y.detach()
| numpy() | ํ ์๋ฅผ ๋ํ์ด ๋ฐฐ์ด๋ก ๋ณํ (๊ณต์ ๋ฉ๋ชจ๋ฆฌ) |
| tolist() | ํ ์๋ฅผ ํ์ด์ฌ ๋ฆฌ์คํธ๋ก ๋ณํ |
| clone() | ํ ์ ๋ณต์ (๋ ๋ฆฝ๋ ๋ณต์ฌ๋ณธ) |
| to(dtype/device) | ํ ์์ ํ์ ๋๋ ๋๋ฐ์ด์ค ๋ณ๊ฒฝ |
| expand() | ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํ๋ฉด์ ๋ธ๋ก๋์บ์คํธ ๋ฐฉ์์ผ๋ก ํ์ฅ |
| repeat() | ๊ฐ์ ๋ณต์ ํ์ฌ ํ ์ ํฌ๊ธฐ ํ์ฅ (expand์๋ ๋ฐฉ์ ๋ค๋ฆ) |
| detach() | ๊ทธ๋๋์ธํธ ๊ณ์ฐ์์ ํ ์๋ฅผ ๋ถ๋ฆฌ (autograd ๋๊ธฐ) |
๋ฐ์ํ
'{Lecture} > Artificial intelligence application' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ธ๊ณต์ง๋ฅ 3์ฃผ์ฐจ pre class ๊ฐ์ ์ ๋ฆฌ (0) | 2025.03.24 |
|---|---|
| ์ธ๊ณต์ง๋ฅ ์์ฉ 2์ฃผ์ฐจ in class ๊ฐ์ ์ ๋ฆฌ (0) | 2025.03.23 |
| ์ธ๊ณต์ง๋ฅ ์์ฉ 2์ฃผ์ฐจ pre class ๊ฐ์ ์ ๋ฆฌ (0) | 2025.03.23 |