テキスト分類器を学習する
埋め込み、プーリングを行い、感情を予測します
「テキスト分類器を学習する」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Goal: Predict a Label
A text classifier reads a sentence and predicts a label, like positive or negative sentiment for a movie review. 🎬
Step One: Encode the Text
Reuse your pipeline: tokenize each review and map tokens to ids, turning every sentence into a list of integers.
Step Two: Embed the Ids
Feed those ids into an embedding layer. Each review becomes a sequence of dense word vectors the model can process.
self.emb = nn.Embedding(vocab_size, 32)Step Three: Pool the Vectors
A sentence has many vectors but you need one. Pooling, often a mean over the words, collapses them into a single vector.
pooled = vecs.mean(dim=1) # average over the sequenceStep Four: A Linear Head
Send the pooled vector through a linear layer to produce one score per class. These raw scores are called logits.
self.fc = nn.Linear(32, num_classes)Assemble the Model
Stack embed, pool, and the linear head in a forward method. That is a complete, tiny text classifier.
def forward(self, ids):
x = self.emb(ids).mean(dim=1)
return self.fc(x)Pick the Loss
For multiclass labels use cross-entropy loss. It expects raw logits and the integer class index as the target.
loss_fn = nn.CrossEntropyLoss()Choose an Optimizer
An optimizer like Adam updates the embedding and linear weights together as it minimizes the loss.
opt = torch.optim.Adam(model.parameters(), lr=1e-3)The Training Loop
For each batch: forward, compute loss, backward, and step. Repeat over the data for several epochs.
logits = model(ids)
loss = loss_fn(logits, labels)
loss.backward()
opt.step()Make a Prediction
At inference, take the class with the highest logit using argmax to get the predicted label for new text.
pred = model(ids).argmax(dim=1)Measure Accuracy
Compare predictions to true labels to compute accuracy. Watch it climb as the embedding learns sentiment patterns.
Quick Check
In this classifier, what turns a sequence of word vectors into one vector?
Recap
You embed ids, pool them into one vector, classify with a linear head, and train with cross-entropy to label text. ✅
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フィードバックを取得できます。ローカル設定は不要です。