C.W.K.
Stream
Lesson 02 of 04 · published

멘털 그림: 벡터, 행렬, 텐서

~10 min · linalg, vector, matrix, tensor, intuition

Level 0Beginner
0 XP0/38 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

기하 먼저, 인덱스는 나중

Tensor가 '뭐다' 잊어버리는 가장 빠른 길은 정의를 'N차원 array' 로 읽는 거. 그건 storage view야. 모델 디버깅 도와주는 직관은 기하적이야:

  • 벡터 (1-D) → 길이 (norm)이랑 방향 (feature 조합) 가진 화살표. 1024-D embedding은 1024차원 공간의 화살표 한 개. '비슷한 개념' = 같은 방향 가리키는 화살표들.
  • 행렬 (2-D) → 그 화살표들의 stack. row인지 column인지는 layout에 따라. row-major vs column-major 선택은 정확히 어느 axis가 메모리에서 contiguous인가의 문제.
  • Tensor (3-D+) → '시간 step별 / 배치별 / attention head별 행렬'. compute 관점에선 여전히 GEMM 통과하는 batched 행렬, 추가 차원은 부기 처리야.

왜 중요하냐 — 디버깅하는 모든 shape mismatch는 어느 axis가 어느 axis냐 질문이야. 'Attention 출력이 왜 (32, 16, 64, 512) 고 (32, 512, 16, 64) 가 아니지?' 는 기하 그림 잡으면 풀려: batch × head × seq × head_dim, GEMM이 마지막 두 axis 따라 일어났다는 거 기억.

Code

매일 보는 모양 넷 만들기·python
import numpy as np

# 벡터 — 1024-D 화살표 (embedding 한 개)
embedding = np.random.randn(1024)
magnitude = np.linalg.norm(embedding)            # 길이
direction = embedding / magnitude                # 단위 화살표

# 행렬 — embedding 32개 row로 쌓은 batch
embeddings_batch = np.random.randn(32, 1024)

# Tensor — 시퀀스 16, 토큰 512씩, dim 1024
sequence_data = np.random.randn(16, 512, 1024)

# 4-D tensor — multi-head attention 입력
# (batch, head, seq, head_dim)
attention_input = np.random.randn(32, 16, 512, 64)
Cosine similarity — dot product의 기하적 의미·python
def cosine(u, v):
    return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))

a = np.array([1.0, 1.0, 0.0])    # '오른쪽 위' 가리키는 화살표
b = np.array([1.0, 0.9, 0.1])    # 거의 같은 방향
c = np.array([-1.0, -1.0, 0.0])  # 정반대 방향
d = np.array([1.0, -1.0, 0.0])   # a랑 수직

print(cosine(a, b))   # ~0.99 — 거의 동일 개념
print(cosine(a, c))   # -1.0  — 반대
print(cosine(a, d))   #  0.0  — 직교, 무관

External links

Exercise

좋아하는 embedding 모델 (sentence-transformers, OpenAI ada, Python wrapper 있는 거 아무거나) 잡고 문장 셋 embed: (1) 'Cats love sunshine.' (2) 'Sunbathing kittens are happy.' (3) 'Stock market crashed.' 쌍별 cosine similarity 계산. (1,2) ≈ 0.7+, 둘 다 (3) 보다 ≪ 가 보여야 해. 숫자가 다른 사람이랑 일치할 필요는 없어 — 근데 (1,2)가 둘 중 어느 것보다도 (3)이랑 멀게 안 나오면 cosine 쓰는지 Euclidean 쓰는지 확인.

Progress

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

댓글 0

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

아직 댓글이 없어요. 첫 댓글을 남겨보세요.