0Pricing
Deep Learning Academy · レッスン

Scaled Dot-ProductとMulti-Head

複数の部分空間で並列にAttentionを行います

「Scaled Dot-ProductとMulti-Head」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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!

よくある質問

「Scaled Dot-ProductとMulti-Head」レッスンは無料ですか?

はい。「Scaled Dot-ProductとMulti-Head」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「Scaled Dot-ProductとMulti-Head」で何を学びますか?

複数の部分空間で並列にAttentionを行います ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Scaled Dot-ProductとMulti-Head」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Self-Attention:Query、Key、Value
  2. Scaled Dot-ProductとMulti-Head
  3. 順序のための位置エンコーディング
  4. Transformer Encoderブロックを積み重ねる
← Deep Learning Academyに戻る