← 문제 목록/Causal Mask (autoregressive)
문제 해설

Causal Mask (autoregressive)

신경망 · easy

preview

Causal Mask

GPT 류 자기회귀(autoregressive) 모델이 학습 중 "미래 토큰을 보면 안 된다" 는 제약을 걸기 위해 쓰는 (L, L) 삼각 마스크.

점수 행렬 SRL×LS \in \mathbb{R}^{L \times L} 에서 SijS_{ij} 는 position ii 가 position jj 를 참조하는 점수. causal 에서는 j>ij > i (미래) 를 가려야 함 → 해당 위치를 마스킹 (softmax-v1 전에 -inf 대입 → 확률 0).

Mij={1ji0j>i(lower-triangular including diagonal)M_{ij} = \begin{cases} 1 & j \le i \\ 0 & j > i \end{cases} \quad \text{(lower-triangular including diagonal)}

과제

두 함수를 완성하세요.

  1. causal_mask(L) — shape (L, L) bool 배열, 하삼각(대각 포함) True.
  2. apply_mask(scores, mask, neg=-1e9)mask == False 위치를 neg 로 채움.

softmax-v1 에 넣기 전에 apply_mask(scores, causal_mask(L)) 을 통과시키면 미래 attention weight 가 0 이 됨.

테스트 케이스

#이름검증
1shape (L, L) + bool
2하삼각 True, 상삼각 False
3대각은 Trueself-attention 허용
4apply_mask 값 대입미래 위치 = neg
5softmax-v1 후 미래 확률 0합 1, 미래 ≈ 0
코드 작성
Loading...
실행 결과

코드를 작성하고 Run 을 눌러보세요.