0 views
Чем self-attention отличается от masked self-attention и как реализуется causal mask?
Asked at
AI answer
Обычный self-attention позволяет каждому токену учитывать все токены последовательности. Masked self-attention дополнительно скрывает часть позиций. В autoregressive-моделях применяют causal mask: позиция i может смотреть только на позиции j ≤ i, поэтому будущие токены не влияют на предсказание.
В scaled dot-product attention к оценкам добавляют маску до softmax:
scores = Q @ Kᵀ / sqrt(d_k)
scores = scores.masked_fill(mask == 0, -∞)
weights = softmax(scores)
output = weights @ V
Для causal mask это нижнетреугольная матрица: разрешены диагональ и элементы ниже неё. На практике -∞ часто заменяют большим отрицательным числом, а маску обычно также сочетают с маской padding. Некоторые библиотеки применяют causal mask внутри оптимизированного attention-оператора.
Also asked at Цифровые технологии и платформы
Similar questions
- How does attention in the encoder differ from attention in the decoder?
- What transformer architectures are there, and how do they differ in terms of attention?
- What are the encoder and decoder in the Transformer architecture, and how do they differ?
- How does a transformer account for token order? What approaches are there, and how does RoPE work?
- Why is multi-head attention needed?