0Pricing
Deep Learning Academy · Lesson

nn.Embedding: Learnable Word Vectors

A lookup table trained by gradient descent.

nn.Embedding: Learnable Word Vectors is a free Deep Learning Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Ids Alone Are Not Enough

Token ids are just labels. The id 5 is not greater than 2 in any meaningful way, so feeding raw ids to a net misleads it.

One-Hot Is Wasteful

One way to represent ids is a giant one-hot vector, but with thousands of words it is huge, sparse, and carries no similarity.

Enter the Embedding

An embedding maps each id to a short dense vector of floats. These few numbers can encode rich meaning about the word.

It Is a Lookup Table

Think of nn.Embedding as a lookup table: row i holds the vector for token id i. Indexing the table fetches that row.

import torch.nn as nn
emb = nn.Embedding(num_embeddings=1000, embedding_dim=16)

Two Key Arguments

You set num_embeddings to your vocab size and embedding_dim to how many numbers each word vector holds.

Look Up a Word

Pass a tensor of ids and the layer returns their vectors. A batch of ids becomes a batch of dense vectors instantly.

ids = torch.tensor([1, 5, 2])
vecs = emb(ids)  # shape (3, 16)

Output Shape Grows a Dim

The embedding adds an extra dimension. An input of shape (batch, length) becomes (batch, length, embedding_dim).

The Vectors Start Random

At first the embedding rows are random. They hold no meaning yet, just noise waiting to be shaped by training.

They Are Trainable

Embedding weights are parameters. Gradient descent nudges each word vector during training, so the table learns over time.

Choosing the Dimension

A larger embedding_dim can capture more nuance but costs memory. Small tasks use 16 to 100; large language models use far more.

Plug It Into a Model

The embedding is usually the first layer. It converts ids to vectors, then later layers process those vectors as usual.

Quick Check

How does nn.Embedding turn a token id into a vector?

Recap

nn.Embedding is a trainable lookup table mapping ids to dense vectors. It starts random and learns meaning through training. ✅

Frequently asked questions

Is the “nn.Embedding: Learnable Word Vectors” lesson free?

Yes — the full text of “nn.Embedding: Learnable Word Vectors” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.

What will I learn in “nn.Embedding: Learnable Word Vectors”?

A lookup table trained by gradient descent. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Deep Learning Academy?

No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “nn.Embedding: Learnable Word Vectors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Deep Learning Academy lesson?

Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Tokenize and Build a Vocabulary
  2. nn.Embedding: Learnable Word Vectors
  3. Why Embeddings Capture Meaning
  4. Train a Text Classifier
← Back to Deep Learning Academy