torch.cat
import torch
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
c = torch.cat((a, b), dim=0) # shape: (4, 2)
d = torch.cat((a, b), dim=1) # shape: (2, 4)
torch.stack
import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = torch.stack((a, b), dim=0) # shape: (2, 3)
d = torch.stack((a, b), dim=1) # shape: (3, 2)
torch.cat
과 torch.stack
의 차이:
torch.cat
은 기존 차원 중 하나에서 텐서들을 이어붙입니다.torch.stack
은 새로운 차원을 추가하여 텐서들을 쌓습니다.tensor.expand
a = torch.tensor([1, 2, 3])
b = a.expand(3, 3) # shape: (3, 3)
tensor.repeat
a = torch.tensor([1, 2, 3])
b = a.repeat(3, 1) # shape: (3, 3)
tensor.add
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a.add(b) # tensor([5, 7, 9])
tensor.add_
add
와 동일하지만, in-place 연산으로 결과를 첫 번째 텐서에 저장합니다.a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
a.add_(b) # a는 tensor([5, 7, 9])로 변함
tensor.sub
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a.sub(b) # tensor([-3, -3, -3])
tensor.sub_
sub
와 동일하지만, in-place 연산으로 결과를 첫 번째 텐서에 저장합니다.a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
a.sub_(b) # a는 tensor([-3, -3, -3])로 변함
tensor.mul
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a.mul(b) # tensor([4, 10, 18])
tensor.mul_
mul
와 동일하지만, in-place 연산으로 결과를 첫 번째 텐서에 저장합니다.a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
a.mul_(b) # a는 tensor([4, 10, 18])로 변함
tensor.div
a = torch.tensor([4, 9, 16])
b = torch.tensor([2, 3, 4])
c = a.div(b) # tensor([2, 3, 4])
tensor.div_
div
와 동일하지만, in-place 연산으로 결과를 첫 번째 텐서에 저장합니다.a = torch.tensor([4, 9, 16])
b = torch.tensor([2, 3, 4])
a.div_(b) # a는 tensor([2, 3, 4])로 변함
torch.pow
a = torch.tensor([1, 2, 3])
b = torch.pow(a, 2) # tensor([1, 4, 9])
torch.pow_
pow
와 동일하지만, in-place 연산으로 결과를 첫 번째 텐서에 저장합니다.a = torch.tensor([1, 2, 3])
a.pow_(2) # a는 tensor([1, 4, 9])로 변함
torch.eq
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.eq(a, b) # tensor([True, False, True])
torch.ne
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.ne(a, b) # tensor([False, True, False])
torch.gt
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.gt(a, b) # tensor([False, True, False])
torch.ge
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.ge(a, b) # tensor([True, True, True])
torch.lt
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.lt(a, b) # tensor([False, False, False])
torch.le
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.le(a, b) # tensor([True, False, True])
torch.logical_and
a = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
c = torch.logical_and(a, b) # tensor([True, False, False])
torch.logical_or
a = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
c = torch.logical_or(a, b) # tensor([True, True, True])
torch.logical_xor
a = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
c = torch.logical_xor(a, b) # tensor([False, True, True])
torch.norm
(L1 노름)a = torch.tensor([1, -2, 3])
norm_l1 = torch.norm(a, p=1) # L1 노름: 1 + 2 + 3 = 6
torch.norm(a, p=2)
(L2 노름)a = torch.tensor([1, -2, 3])
norm_l2 = torch.norm(a, p=2) # L2 노름: sqrt(1^2 + (-2)^2 + 3^2) = sqrt(14)
torch.norm(a, p=float('inf'))
(Linf 노름)a = torch.tensor([1, -2, 3])
norm_linf = torch.norm(a, p=float('inf')) # Linf 노름: max(1, 2, 3) = 3
torch.norm(a - b, p=1)
(맨해튼 거리, 맨해튼 유사도)a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 0, -1])
manhattan_distance = torch.norm(a - b, p=1)
# 맨해튼 거리: |1-4| + |2-0| + |3+1| = 9
# 맨해튼 유사도: 1 / (1 + manhattan_distance)
torch.norm(a - b, p=2)
(유클리드 거리, 유클리드 유사도)a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 0, -1])
euclidean_distance = torch.norm(a - b, p=2)
# 유클리드 거리: sqrt((1-4)^2 + (2-0)^2 + (3+1)^2) = sqrt(26)
# 유클리드 유사도: 1 / (1 + euclidean_distance)
torch.dot(a, b) / (torch.norm(a, p=2) * torch.norm(b, p=2))
(코사인 거리, 코사인 유사도)b = torch.tensor([1, 2, 3])
c = torch.tensor([4, 0, -1])
cos_similarity = torch.dot(a, b) / (torch.norm(a, p=2) * torch.norm(b, p=2))
# 코사인 유사도: 1*4 + 2*0 + 3*(-1) / (sqrt(1^2 + 2^2 + 3^2) * sqrt(4^2 + 0^2 + (-1)^2))
a.matmul(b), a.mm(b), a @ b
matmul
, mm
, @
연산자는 모두 행렬 곱셈을 수행함.a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
matmul_result = a.matmul(b) # 행렬 곱셈 결과
mm_result = a.mm(b) # 행렬 곱셈 결과
at_result = a @ b # 행렬 곱셈 결과
import torch
import matplotlib.pyplot as plt
a = torch.tensor([[1, 2], [3, 4]])
a_flipped_lr = torch.fliplr(a) # 좌우 대칭
# a_flipped_ud = a @ torch.tensor([[0, 1], [1, 0]]) 도 동일
plt.subplot(1, 2, 1)
plt.imshow(a, cmap='gray')
plt.title('Original')
plt.subplot(1, 2, 2)
plt.imshow(a_flipped_lr, cmap='gray')
plt.title('Left-Right Flipped')
plt.show()
import torch
import matplotlib.pyplot as plt
a = torch.tensor([[1, 2], [3, 4]])
a_flipped_ud = torch.flipud(a) # 상하 대칭
# a_flipped_ud = torch.tensor([[0, 1], [1, 0]]) @ a 도 동일
plt.subplot(1, 2, 1)
plt.imshow(a, cmap='gray')
plt.title('Original')
plt.subplot(1, 2, 2)
plt.imshow(a_flipped_ud, cmap='gray')
plt.title('Up-Down Flipped')
plt.show()