0Pricing
Deep Learning Academy · Aula

Produto Escalar Dimensionado e Múltiplas Cabeças

Aplique atenção em subespaços paralelos

Produto Escalar Dimensionado e Múltiplas Cabeças é uma aula grátis de Deep Learning Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Deep Learning Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Deep Learning Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

The Scaling Problem

When key vectors are long, dot-product scores grow huge and softmax becomes razor-sharp. That kills gradients, so we need to scale the scores down.

Divide by Root d

The fix is to divide each score by the square root of the key dimension. This keeps the numbers in a stable range before softmax.

scores = (q @ k.transpose(-2, -1)) / d_k ** 0.5

Scaled Dot-Product Attention

Put it together: score, scale, softmax, then blend values. This four-step recipe is the famous scaled dot-product attention.

w = scores.softmax(dim=-1)
out = w @ v

One Head, One View

A single attention head learns just one way to relate tokens. But language has many relationships at once, so one head is limiting.

Many Heads in Parallel

Multi-head attention runs several attention heads side by side, each with its own Q, K, V projections, each attending in a different subspace.

Split the Dimension

You do not make the model wider. You split the model dimension across heads, so each head works on a smaller slice in parallel.

d_head = d_model // num_heads

Reshape into Heads

To run heads in parallel, you reshape Q, K, V to add a heads axis, then attend within each head independently.

q = q.view(B, T, H, d_head).transpose(1, 2)

Different Patterns

One head may track grammar, another may link a pronoun to its noun. Multiple heads capture diverse patterns the same input contains.

Concatenate Heads

After each head produces its output, you concatenate them back into one vector of the original model dimension.

out = out.transpose(1, 2).reshape(B, T, d_model)

Final Projection

A last output projection mixes the concatenated head results, letting the model combine what every head discovered.

out = self.W_o(out)

Use the Built-in

In practice PyTorch ships nn.MultiheadAttention, so you rarely hand-roll the reshapes. Knowing the math still helps you debug.

attn = nn.MultiheadAttention(d_model, num_heads)

Quick Check

Let's confirm why we scale the scores.

Recap

You saw how scaling stabilizes softmax and how multi-head attention splits the work across parallel heads, then concatenates and projects. Great progress!

Perguntas Frequentes

A aula “Produto Escalar Dimensionado e Múltiplas Cabeças” é grátis?

Sim — o texto completo de “Produto Escalar Dimensionado e Múltiplas Cabeças” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Deep Learning Academy, atualize para CoddyKit PRO. O curso de Deep Learning Academy inclui 4 aulas no total.

O que vou aprender em “Produto Escalar Dimensionado e Múltiplas Cabeças”?

Aplique atenção em subespaços paralelos Você pratica Deep Learning Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Deep Learning Academy?

Nenhuma experiência prévia é necessária. Deep Learning Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Produto Escalar Dimensionado e Múltiplas Cabeças”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Deep Learning Academy?

Sim. Cada aula de Deep Learning Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Atenção Própria: Consulta, Chave e Valor
  2. Produto Escalar Dimensionado e Múltiplas Cabeças
  3. Codificação Posicional para a Ordem
  4. Empilhe um Bloco Codificador Transformer
← Voltar para Deep Learning Academy