Transformer Encoderブロックを積み重ねる
Attention、フィードフォワード、正規化を組み合わせます
「Transformer Encoderブロックを積み重ねる」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Building Block
A transformer is just one encoder block repeated. Learn the block and you understand the whole tower, from tiny models to giant ones.
Two Sub-Layers
Each block has two parts: a multi-head attention sub-layer, then a small feedforward network. Both wrapped with residuals and normalization.
Attention First
The block starts with self-attention, letting every token mix in context from the rest of the sequence before any further processing.
attn_out, _ = self.attn(x, x, x)The Residual Connection
A residual adds the sub-layer's input back to its output. This shortcut lets gradients flow and keeps deep stacks trainable.
x = x + attn_outLayer Normalization
After adding the residual, layer norm rescales each token's features to a stable distribution, steadying training across layers.
x = self.norm1(x)The Feedforward Net
Next comes a position-wise feedforward network: expand to a wider hidden size, apply a nonlinearity, then project back down.
ff = nn.Sequential(nn.Linear(d, 4*d), nn.GELU(), nn.Linear(4*d, d))Per-Token Processing
The feedforward layer treats each token independently. Attention shared information; this step refines each token on its own.
Second Residual and Norm
The feedforward output gets the same treatment: a residual add plus another layer norm, finishing the block.
x = self.norm2(x + ff(x))Stack Them Deep
Stack many identical blocks and the model builds richer representations layer by layer. Depth is where transformer power comes from.
layers = nn.ModuleList([Block(d) for _ in range(N)])Pre-Norm vs Post-Norm
Many modern models apply layer norm before each sub-layer instead of after. Pre-norm trains more stably in very deep stacks.
Use the Built-in
PyTorch gives you nn.TransformerEncoderLayer and nn.TransformerEncoder, so you can assemble a full stack in just a couple of lines.
layer = nn.TransformerEncoderLayer(d_model, nhead)
enc = nn.TransformerEncoder(layer, num_layers=6)Quick Check
Let's review the parts of an encoder block.
Recap
You assembled an encoder block: attention, residual, norm, feedforward, residual, norm. Stack it deep and you have a transformer. Amazing work!
よくある質問
「Transformer Encoderブロックを積み重ねる」レッスンは無料ですか?
はい。「Transformer Encoderブロックを積み重ねる」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「Transformer Encoderブロックを積み重ねる」で何を学びますか?
Attention、フィードフォワード、正規化を組み合わせます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Transformer Encoderブロックを積み重ねる」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Self-Attention:Query、Key、Value
- Scaled Dot-ProductとMulti-Head
- 順序のための位置エンコーディング
- Transformer Encoderブロックを積み重ねる