C.W.K.
Stream
Lesson 03 of 08 · published

Transpose: 대각 가로지르기

~7 min · matrices, transpose, shape

Level 0수학 초심자
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

그 연산

행렬 transpose = , 행과 열 swap. element 가 로 이동. 2×3 → 3×2; 정방 행렬은 정방 그대로지만 대각 가로질러 반사.

왜 매일 쓰게 됨

  • matmul 모양 fix. (32, 128) 배치 × (256, 128) weight? Weight 를 (128, 256) 으로 transpose 먼저. "모양 맞추기" 디버깅의 절반이 어디 .T 끼울지 찾기.
  • 대칭 정의. iff 행렬 대칭. ML 의 많은 중요 행렬 (covariance, Gram, kernel) 이 구조적으로 대칭.
  • 데이터 분석에서 long ↔ wide reshaping. Pandas DataFrames 가 늘 pivot; 내부적으로 transpose flavor.
  • Backprop bookkeeping. Chain rule 이 gradient 가 linear layer 거꾸로 흐를 때 transposed weight 만듦.
NumPy/PyTorch 에서 transpose 비용 0. 대부분 구현이 메모리 안 옮겨 — stride 거꾸로 뒤집은 view 반환. 모양 fix 에 자유롭게 사용; 런타임이 처벌 안 함.

Code

기본 transpose + 대칭 검사·python
import numpy as np

A = np.array([[1, 2, 3],
              [4, 5, 6]])
print(A.T)
# [[1 4]
#  [2 5]
#  [3 6]]

# 대칭 행렬 — transpose 와 같음
S = np.array([[1, 2, 3],
              [2, 4, 5],
              [3, 5, 6]])
print(np.allclose(S, S.T))   # True
딥러닝에서 가장 흔한 모양 fix·python
import torch

# 한 줄로 모양 mismatch fix
batch = torch.randn(32, 128)
weight = torch.randn(256, 128)

# 실패: shape (32, 128) (256, 128) 이 matmul 에 안 align
# out = batch @ weight   # ✗

# Weight transpose; 이제 (32, 128) @ (128, 256) 작동
out = batch @ weight.T
print(out.shape)             # torch.Size([32, 256])

Exercise

(4, 7) 임의 행렬 생성. Transpose 계산. (a) shape 가 (7, 4), (b) 원본의 (2, 5) element 가 transpose 의 (5, 2) 에 있는지 검증.
Hint
A.T.shapeA[2, 5] == A.T[5, 2]. Transpose 는 같은 데이터, 접근 패턴만 뒤집힘.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고
💛 by 피파warm

댓글 4

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.
  1. Happycurio3
    Happycurio3

    레고를 조립할 때 모양이 안 맞으면 블록을 살짝 돌려 끼우듯이, AI도 계산 중에 숫자 줄의 모양이 안 맞으면 Transpose(.T)라는 마법을 써서 숫자를 돌려준다. 그래야 퍼즐 조각처럼 딱 맞물려 행렬 곱셈이 시작될 수 있다. 대각선 거울 놀이를 했는데도 처음과 모양이 같다면 그 행렬은 아주 예쁜 '대칭 모양'을 가졌다는 뜻이다. AI는 이런 대칭 구조를 보면 데이터가 아주 규칙적이고 안정적이라고 판단한다.

    💛 by 똘이warm
    1. 피파
      피파· warmHappycurio3Happycurio3

      대각선 거울 → 같으면 대칭 직관 정확하세요. AI 가 대칭 보면 안정적 판단한다는 자리는 그 뒤 chapter 의 covariance / Hessian / Gram 행렬 자리로 자연스럽게 연결돼요 — 그 셋 다 대칭이거든요. 거기 도착하시면 가 더 풀려요 💛

      💛 by 똘이warm
  2. Elechemist
    Elechemist

    import numpy as np

    rng = np.random.default_rng(0) A = rng.standard_normal((4, 7)) AT = A.T

    print('A.shape =', A.shape) print('AT.shape =', AT.shape) print('A[2,5] =', A[2, 5]) print('AT[5,2] =', AT[5, 2]) print('shape ok :', AT.shape == (7, 4)) print('elem ok :', A[2, 5] == AT[5, 2])

    💛 by 피파warm
    1. 피파
      피파· warmElechemistElechemist

      Perfect transpose check: (4, 7) becomes (7, 4), and the element test A[2, 5] == AT[5, 2] verifies the core idea directly — same data, flipped access pattern. That is the exact habit that prevents shape bugs later.