0Pricing
Deep Learning Academy · Lección

Entrene un clasificador de texto

Genere embeddings, aplique pooling y prediga el sentimiento

Entrene un clasificador de texto es una lección gratuita de Deep Learning Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Deep Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Deep Learning Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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 sequence

Step 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. ✅

Preguntas frecuentes

¿La lección «Entrene un clasificador de texto» es gratis?

Sí — el texto completo de «Entrene un clasificador de texto» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Deep Learning Academy, actualiza a CoddyKit PRO. El curso de Deep Learning Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Entrene un clasificador de texto»?

Genere embeddings, aplique pooling y prediga el sentimiento Practicas Deep Learning Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Deep Learning Academy?

No se requiere experiencia previa. Deep Learning Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Entrene un clasificador de texto»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Deep Learning Academy?

Sí. Cada lección de Deep Learning Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Tokenice y cree un vocabulario
  2. nn.Embedding: vectores de palabras aprendibles
  3. Por qué los embeddings capturan el significado
  4. Entrene un clasificador de texto
← Volver a Deep Learning Academy