확장하여 연산 수행| 연산 | 함수 | In-Place |
|---|---|---|
| 덧셈 | torch.add(a, b) | a.add_(b) |
| 뺄셈 | torch.sub(a, b) | a.sub_(b) |
| 스칼라곱 | torch.mul(a, b) | a.mul_(b) |
| 나누기 | torch.div(a, b) | a.div_(b) |
| 거듭제곱 | torch.pow(a, b) | a.pow_(b) |
| 거듭제곱근 | torch.pow(a, 1/n) | a.pow_(1/n) |
### Example ###
t = torch.tensor([[1, 2], [3, 4]])
u = torch.tensor([[10, 20], [30, 40]])
v = torch.tensor([10, 100])
s = torch.tensor([[10], [20]])
torch.add(t, u) # tensor([[11, 22], [33, 44]])
torch.sub(u, s) # tensor([[0, 10], [10, 20]])
torch.mul(t, v) # tensor([[10, 200], [30, 400]])
torch.div(u, s) # tensor([[1.0, 2.0], [1.5, 2.0]])
나누기 연산에서 발생하는 Runtime Error
RuntimeError: result type Float can't be cast to the desired output type Long
데이터 타입 불일치로 인해 발생. Data Type을 Float로 변경하면 해결 가능.
Autograd와의 호환성 측면에서 문제가 있을 수 있음### Example ###
t = torch.tensor([[1, 2], [3, 4]])
u = torch.tensor([[10, 20], [30, 40]])
# in-place 연산
t.add_(u)
print(t) # tensor([[11, 22], [33, 44]])
# id 확인하기
id(t) # 137528900460336
| 연산 | 의미 | 함수 형태 |
|---|---|---|
| 등호 (Equal) | == | torch.eq(a, b) |
| 부등호 (Not Equal) | != | torch.ne(a, b) |
| 크다 (Greater Than) | > | torch.gt(a, b) |
| 크거나 같다 (Greater Than or Equal) | >= | torch.ge(a, b) |
| 작다 (Less Than) | < | torch.lt(a, b) |
| 작거나 같다 (Less Than or Equal) | <= | torch.le(a, b) |
### Example ###
a = torch.tensor([1, 2, 3])
b = torch.tensor([2, 2, 2])
equal = torch.eq(a, b) # tensor([False, True, False])
greater = torch.gt(a, b) # tensor([False, False, True])
Tensor 논리 연산 종류
| 항목 | 논리곱(AND) | 논리합(OR) | 배타적 논리합(XOR) |
|---|---|---|---|
| 코드 | torch.logical_and(x, y) | torch.logical_or(x, y) | torch.logical_xor(x, y) |
배타적 논리합(XOR)
- 두 명제 중 하나만
True일 때True- [
True,True] / [False,False] 는 는 False
### Example ###
x = torch.tensor([True, True, False, False])
y = torch.tensor([True, False, True, False])
result = torch.logical_xor(x, y)
print(result) # tensor([False, True, True, False])
# 숫자 텐서일 경우 예시
a = torch.tensor([0, 1, 2, 3])
b = torch.tensor([1, 1, 0, 4])
# 0이 아닌 값은 True로 간주
result_numeric = torch.logical_xor(a, b)
print(result_numeric) # tensor([True, False, True, False])
A.matmul(B) / A.mm(B) / A @ BA.matmul(B)A.mm(B)A @ B### Example ###
# 1. 벡터-벡터 곱(내적)
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = torch.matmul(a, b)
print(c) # tensor(32) # 1*4 + 2*5 + 3*6 = 32
# 2. 행렬-벡터 곱
A = torch.tensor([[1, 2], [3, 4]])
x = torch.tensor([5, 6])
y = torch.matmul(A, x)
print(y) # tensor([17, 39]) # [1*5 + 2*6, 3*5 + 4*6]
# 3. 행렬-행렬 곱
batch_A = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
batch_B = torch.tensor([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
batch_C = torch.matmul(batch_A, batch_B)
print(batch_C)
# tensor([[[ 31, 34],
# [ 71, 78]],
#
# [[167, 182],
# [231, 252]]])
### Example2 ###
A = torch.tensor([[1, 2], [3, 4]], dtype=torch.float)
B = torch.tensor([[5, 6], [7, 8]], dtype=torch.float)
# 1. A.matmul(B)
result_matmul = A.matmul(B)
# 2. A.mm(B)
result_mm = A.mm(B)
# 3. A @ B
result_at = A @ B
# result
# tensor([[19., 22.],
# [43., 50.]])
### Example ###
A = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.float)
# 3x3 단위 행렬 생성
I = torch.eye(3)
# tensor([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
# 좌우 대칭 이동을 위한 행렬 생성 (단위 행렬의 열을 뒤집음)
flip_matrix = torch.flip(I, [1])
# tensor([[0., 0., 1.],
# [0., 1., 0.],
# [1., 0., 0.]])
# 좌우 대칭 이동 수행
A_flipped = torch.matmul(A, flip_matrix)
# tensor([[3., 2., 1.],
# [6., 5., 4.],
# [9., 8., 7.]])