Self-attention: query, key y value
Permita que cada token observe a todos los demás
Self-attention: query, key y value es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Deep Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Deep Learning Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Tokens That Talk
In a sequence, the meaning of one word depends on others. Self-attention lets every token look at every other token to gather the context it needs.
Three Roles per Token
Each token plays three roles: a query that asks, a key that answers, and a value that carries content. These come from the same word, used three ways.
The Query
A token's query describes what it is looking for. Think of it as the question this word is asking about the rest of the sentence.
The Key
Every token also exposes a key, a label advertising what it offers. A query is compared against all keys to find good matches.
The Value
Once a match is found, the value is the actual information that gets passed along. Keys decide how much, values decide what.
Make Q, K, V
You build queries, keys, and values by projecting the input through three learned linear layers. Same input, three different weight matrices.
q = self.W_q(x)
k = self.W_k(x)
v = self.W_v(x)Score by Similarity
To see how well a query matches a key, you take their dot product. A bigger score means the two tokens are more relevant to each other.
scores = q @ k.transpose(-2, -1)Scores to Weights
Raw scores become attention weights with softmax, so each query's weights are positive and sum to one across all keys.
weights = scores.softmax(dim=-1)Blend the Values
The output for each token is a weighted sum of all values, mixed by the attention weights. Relevant tokens contribute more.
out = weights @ vWhy It Beats RNNs
Self-attention connects any two tokens in one step, so distance does not matter. This parallel view is why transformers handle long context so well.
Learned, Not Fixed
The Q, K, V projections are trained by gradient descent. The network learns what to ask, what to advertise, and what to share, all from data.
Quick Check
Let's test how attention combines its pieces.
Recap
You learned that self-attention turns each token into a query, key, and value, scores query-key matches, and blends values by softmax weights. Nice work!
Preguntas frecuentes
¿La lección «Self-attention: query, key y value» es gratis?
Sí — el texto completo de «Self-attention: query, key y value» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Deep Learning Academy, actualiza a CoddyKit PRO. El curso de Deep Learning Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Self-attention: query, key y value»?
Permita que cada token observe a todos los demás Practicas Deep Learning Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Deep Learning Academy?
No se requiere experiencia previa. Deep Learning Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Self-attention: query, key y value»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Deep Learning Academy?
Sí. Cada lección de Deep Learning Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Self-attention: query, key y value
- Producto punto escalado y multi-head
- Codificación posicional para representar el orden
- Apile un bloque de encoder Transformer