출처:
지미뉴트론 개발일기
PyTorch tensors can leverage GPU acceleration, while NumPy arrays are CPU-bound.
* 연산자numpy.dot 함수@ 연산자dot과 같은 역할numpy.matmul과 동일matmul, @와 동일* 연산자torch.dot 함수torch.mul(t1,t2) 함수 / t1.mul(t2) 메서드torch.matmul 함수@와 동일# NumPy에서의 *
A_numpy = np.array([[1, 2, 3]])
B_numpy = np.array([[2], [2]])
result_numpy = A_numpy * B_numpy
print(A_numpy.shape, B_numpy.shape)
# pytorch에서의 mul
A_torch = torch.tensor([[1, 2, 3]])
B_torch = torch.tensor([[2], [2]])
result_torch = torch.mul(A_torch, B_torch)
print("NumPy * result:\n", result_numpy)
print("PyTorch mul result:\n", result_torch)
(1, 3) (2, 1)
NumPy * result:
[[2 4 6]
[2 4 6]]
PyTorch mul result:
tensor([[2, 4, 6],
[2, 4, 6]])
# 3차원 배열 생성
A = np.random.rand(2, 3, 4)
B = np.random.rand(2, 4, 5)
# np.dot 수행
result_dot = np.dot(A, B)
# @ 연산자 수행
result_at = A @ B
print("Shape of A:", A.shape)
print("Shape of B:", B.shape)
# 앞의 행렬의 마지막 축과 뒤의 행렬의 두 번째 축간의 내적 수행
# 2차원 행렬인 경우 행렬내적으로 같은 결과가 나오겠지만
# 3차원 텐서부터 'dot' 결과값 다름
print("Shape of Result (dot):", result_dot.shape)
print("Shape of Result (@):", result_at.shape)
Shape of A: (2, 3, 4)
Shape of B: (2, 4, 5)
Shape of Result (dot): (2, 3, 2, 5)
Shape of Result (@): (2, 3, 5)
# 3차원 텐서 생성
A = torch.rand(2, 3, 4)
B = torch.rand(2, 4, 5)
# torch.matmul 수행
result_matmul = torch.matmul(A, B)
# @ 연산자 수행
result_at = A @ B
print("Shape of A:", A.shape)
print("Shape of B:", B.shape)
print("Shape of Result (matmul):", result_matmul.shape)
print("Shape of Result (@):", result_at.shape)
Shape of A: torch.Size([2, 3, 4])
Shape of B: torch.Size([2, 4, 5])
Shape of Result (matmul): torch.Size([2, 3, 5])
Shape of Result (@): torch.Size([2, 3, 5])
import numpy as np
# 1차원 * 1차원
a = np.array([1, 3, 5])
b = np.array([4, 2, 0])
np.dot(a, b) # 10
a @ b # 혹은 np.matmul(a, b) # 10
# 2차원 * 2차원
a = np.array([[1, 3], [2, 4]])
b = np.array([[1, 0], [2, 1]])
np.dot(a, b)
'''
array([[ 7, 3],
[10, 4]])'''
a @ b # 혹은 np.matmul(a, b)
'''
array([[ 7, 3],
[10, 4]])'''
# 2차원 * 1차원
a = np.array([[1, 3], [2, 4]])
b = np.array([2, 1])
np.dot(a, b) # array([5, 8])
a @ b # array([5, 8])
# 1차원 * 2차원
np.dot(b, a) # array([ 4, 10])
b @ a # array([ 4, 10])
# 배열 * 스칼라
a = np.array([1, 3, 5])
b = 2
np.dot(a, b) # array([ 2, 6, 10])
a @ b # ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)
# 2차원 * 3차원
a = np.array([[1, 3], [2, 4]])
b = np.array([[[1, 1], [0, 1]], [[5, 0], [0, 0]]])
np.dot(a, b)
'''
array([[[ 1, 4],
[ 5, 0]],
[[ 2, 6],
[10, 0]]])'''
a @ b
'''
array([[[ 1, 4],
[ 2, 6]],
[[ 5, 0],
[10, 0]]])'''

a = np.ones([5, 7, 4, 3])
b = np.ones([5, 7, 3, 6])
np.dot(a, b).shape # (5, 7, 4, 5, 7, 6)
np.matmul(a, b).shape # (5, 7, 4, 6)
| Function | Condition (3-D) | Formula (3-D) |
|---|---|---|
| np.dot | A.shape # (a1, a2, a3) B.shape # (b1, b2, b3) --> a3==b2 >>> C = np.dot(A,B) >>> C.shape (a1, a2, b1, b3) | C[i,j,k,m] = np.sum(A[i,j,:] * B[k,:,m]) |
| np.matmul | A.shape # (a1, a2, a3) B.shape # (b1, b2, b3) --> (a1==b1) and (a3==b2) >>> C = np.matmul(A,B) >>> C.shape (a1, a2, b3) | C[i,j,k] = np.sum(A[i,j,:] * B[i,:,k]) |