
v1 masked mean 은 uniform 가중치. 실무 센텐스 인코더 (USE, Sentence-T5, Gemma embedding) 는 학습된 query vector 로 토큰을 가중 평균:
즉 scaled dot-product attention 에서 query 가 토큰당 하나가 아닌 글로벌 학습 벡터 로 고정된 버전.
scores = x @ q / sqrt(d) shape (B, L).-inf 대입 → softmax 후 0.weights = softmax(scores, axis=-1) shape (B, L).out = weights[..., None] · x 합 → (B, d).수치 안정성: softmax 전 max 빼기.
q 만 추가 파라미터 (d 개).함수 attention_pool(x, mask, q) 를 완성하세요.
x shape (B, L, d), mask (B, L) bool, q (d,).(B, d).| # | 이름 | 검증 |
|---|---|---|
| 1 | shape (B, d) | |
| 2 | pad 제외: pad 값 변경해도 출력 불변 | |
| 3 | softmax 가중치 합 = 1 (암묵) | 직접 검증 |
| 4 | q=0 → masked mean (v1) 과 동일 | 모든 score 0 |
| 5 | 다른 q → 다른 출력 | |
| 6 | 한 토큰이 지배 (큰 score) | 그 토큰 ≈ 출력 |
| 7 | 알려진 toy 값 |
코드를 작성하고 Run 을 눌러보세요.