0Pricing
NLP Academy · Lezione

Self-attention passo dopo passo

Query, key e value

Self-attention passo dopo passo è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What Is Self-Attention?

In self-attention, every word looks at every other word in the same sentence to build a richer, context-aware version of itself. 🔍

Three Roles per Word

Each word is projected into three vectors: a query, a key, and a value. These three roles drive the whole self-attention computation.

The Query

The query represents what a word is looking for. Think of it as the question this word asks about the rest of the sentence.

Keys and Values

Each word also offers a key that advertises what it contains, and a value holding the actual information to pass along if chosen.

Scoring With Dot Products

Compare a query to every key using a dot product. A larger product means the query and key align, so that word deserves more focus.

scores = query @ keys.T

Scale the Scores

Divide scores by the square root of the key dimension. This scaling keeps numbers stable so softmax does not become too sharp.

scores = scores / (d_k ** 0.5)

Softmax for Weights

Run the scaled scores through softmax to get attention weights that are positive and sum to 1 across all words in the sentence.

weights = softmax(scores)

Blend the Values

Multiply each value by its weight and add them up. The result is a new vector that blends information from the most relevant words.

output = weights @ values

The Full Formula

All steps combine into one tidy expression: scaled dot-product attention over queries, keys, and values, as introduced in the Transformer paper.

attn = softmax(Q @ K.T / d_k**0.5) @ V

Why Self, Not Cross

It is called self-attention because the queries, keys, and values all come from the same sequence, letting words attend to their own neighbors.

Resolving Ambiguity

In "it was tired," self-attention links it to the right noun by weighting nearby words, giving every token clearer context.

Quick Check

Let us confirm the self-attention steps.

Recap

You walked through self-attention: turn words into queries, keys, and values, score, scale, softmax, then blend the values. That is the engine. ⚙️

Domande Frequenti

La lezione «Self-attention passo dopo passo» è gratuita?

Sì — il testo completo di «Self-attention passo dopo passo» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.

Cosa imparerò in «Self-attention passo dopo passo»?

Query, key e value Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare NLP Academy?

Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Self-attention passo dopo passo»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione NLP Academy?

Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. L'idea dell'attention
  2. Self-attention passo dopo passo
  3. Multi-head attention e posizioni
  4. All'interno del blocco Transformer
← Torna a NLP Academy