Deep Learning Academy · レッスン

系列をパックしてパディングを扱う

長さの異なるデータを効率的に学習します

レッスン 4/413 ステップ

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

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

Batches Want Rectangles

To process many sequences at once, a batch must be a neat rectangle. But real sentences have different lengths, so they don't line up.

Padding to a Common Length

The fix is padding: add filler tokens, usually zeros, to short sequences so every row in the batch is the same length.

pad_sequence Helper

PyTorch's pad_sequence stacks a list of variable-length tensors into one padded batch in a single call.

from torch.nn.utils.rnn import pad_sequence
batch = pad_sequence(seqs, batch_first=True)

Padding Wastes Compute

Those filler tokens are fake. If the RNN processes them anyway, it burns compute and can let the padding pollute the hidden state.

Track the Real Lengths

Before padding, record each sequence's true length. You'll hand these lengths to PyTorch so it knows where the real data ends.

lengths = [len(s) for s in seqs]

Packing the Batch

pack_padded_sequence turns the padded batch plus lengths into a compact form the RNN can run without touching the padding.

packed = pack_padded_sequence(batch, lengths, batch_first=True)

Run the RNN on Packed Data

Feed the packed object straight into nn.LSTM or nn.GRU. The cell skips padded steps, so memory stays clean and clear.

out_packed, h_n = lstm(packed)

Unpack the Output

To read per-step outputs again, call pad_packed_sequence to restore the rectangular padded shape after the RNN finishes.

out, lengths = pad_packed_sequence(out_packed, batch_first=True)

Sort by Length

Classic packing wants sequences sorted longest first. Set enforce_sorted to False and PyTorch handles unsorted batches for you.

Mask the Loss

Even with packing, ignore padded positions when scoring. A mask or ignore_index keeps fake tokens out of the loss.

loss_fn = nn.CrossEntropyLoss(ignore_index=PAD_ID)

Wire It Into the DataLoader

Do the padding inside a custom collate_fn so every batch arrives already padded, with lengths ready for packing.

Quick Check

Why pack a padded batch before feeding it to an RNN?

Recap

Pad to align a batch, track true lengths, then pack so the RNN ignores filler. Mask padded tokens in the loss too. ✅

無料で開始

AI チューターと学ぶ Python — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「系列をパックしてパディングを扱う」レッスンは無料ですか?

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

「系列をパックしてパディングを扱う」で何を学びますか?

長さの異なるデータを効率的に学習します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「系列をパックしてパディングを扱う」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. 系列にメモリが必要な理由
  2. 基本的なRNNセル
  3. LSTMとGRUのゲート
  4. 系列をパックしてパディングを扱う
← Deep Learning Academyに戻る