
torch.catimport 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.stackimport 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.expanda = torch.tensor([1, 2, 3])
b = a.expand(3, 3) # shape: (3, 3)tensor.repeata = torch.tensor([1, 2, 3])
b = a.repeat(3, 1) # shape: (3, 3)tensor.adda = 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.suba = 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.mula = 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.diva = 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.powa = 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.eqa = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.eq(a, b) # tensor([True, False, True])torch.nea = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.ne(a, b) # tensor([False, True, False])torch.gta = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.gt(a, b) # tensor([False, True, False])torch.gea = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.ge(a, b) # tensor([True, True, True])torch.lta = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.lt(a, b) # tensor([False, False, False])torch.lea = torch.tensor([1, 2, 3])
b = torch.tensor([1, 0, 3])
c = torch.le(a, b) # tensor([True, False, True])torch.logical_anda = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
c = torch.logical_and(a, b) # tensor([True, False, False])torch.logical_ora = torch.tensor([True, False, True])
b = torch.tensor([True, True, False])
c = torch.logical_or(a, b) # tensor([True, True, True])torch.logical_xora = 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 = 6torch.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) = 3torch.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 @ bmatmul, 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()
