There are two rules that are performing or two main rules that performing matrix multiplication needs :
1. The inner dimensions(차원) must match. - 행렬 곱셈에서 내부 차원이 일치해야 함.
(3, 2) @ (3, 2) 에러(2, 3) @ (3, 2) 작동(3, 2) @ (2, 3) 작동(3, 2) @ (2, 3) -> (3, 3)(2, 3) @ (3, 2) -> (2, 2)torch.matmul(torch.rand(2, 3), torch.rand(2, 5))

torch.matmul(torch.rand(3, 6), torch.rand(6, 5))

(3, 2)와 (2, 3)의 곱셈은 결과 행렬의 차원이 (3, 3)이 됨.tensor_A = torch.tensor([[1, 2],
[3, 4],
[5, 6]])
tensor_B = torch.tensor([[7, 10],
[8, 11],
[9, 12]])
tensor_A.shape, tensor_B.shape
tensor.matmul(tensor_A, tensor_B) # torch.mm() = torch.matmul()

다음과 같은 에러가 난다.

둘의 inner demention이 다르기 때문이다.
To fix our tensor shape issues, we can manipulate the shape of our tensors using a transpose.
A transpose switches the dimensions of a given tensor.
transpose로 텐서의 차원을 변경하여 문제를 해결할 수 있다.
tensor_B.T, tensor_B.T.shape

.T 즉, transpose는 차원을 변경해준다.
# The matrix multiplication operation works when tensor_B is transposed.
print(f"Original shapes: tensor_A: {tensor_A.shape}, tensor_B: {tensor_B.shape}")
print(f"New shapes : tensor_A: {tensor_A.shape}, tensor_B.T: {tensor_B.T.shape}")
print(f"Mutiplying : {tensor_A.shape} @ {tensor_B.T.shape} <- inner dimensions must match")
output = torch.mm(tensor_A, tensor_B.T)
print(output)
print(f"\nOutput shape : {output.shape}")

텐서 집계는 텐서 데이터에서 특정 값을 요약하거나 분석하기 위해 중요한 작업이다. 예를 들어, 텐서의 최소값, 최대값, 평균, 합계 등을 구하는 과정이 포함. 이 작업은 대규모 데이터를 처리하고 요약하는 데 유용하며, 딥 러닝 모델에서도 자주 사용된다.
텐서 집계(Aggregation)는 텐서의 여러 값을 하나의 값으로 요약하는 작업이다. 예를 들어:
torch.arange()를 사용하여 텐서를 생성할 수 있습니다. 예를 들어, 0에서 100까지 10씩 증가하는 텐서를 생성해보자.
# Create a tensor
x = torch.arange(0, 100, 10)
x, x.dtype

# Find the minimum value in a tensor
torch.min(x), x.min()

# Find the maximum value in a tensor
torch.max(x), x.max()

# Find the mean value in a tensor
torch.mean(x.type(torch.float32)), x.type(torch.float32).mean()

# Find the sum of all values in a tensor
torch.sum(x), x.sum()

# Create a tensor
x = torch.arange(1, 100, 10)
x, x.dtype

argmin() -> returns index position of target tensor where the minimum valueargmin()를 사용하여 최소값을 갖는 텐서의 위치를 찾은 다음 인덱스 위치를 반환x.argmin() # 인덱스 위치

# 0번째 인덱스 위치의 값
x[0]

argmax() -> returns index position of target tensor where the maximum valueargmax()를 사용하여 최대값을 갖는 텐서의 위치를 찾은 다음 인덱스 위치를 반환x.argmax() # 최대값의 인덱스 위치

# 9번째 인덱스 위치의 값
x[9]

짱이다!!!