Ravindra BagaleCourses & study guides English Track your progress

49. What Transformers Are (Text and Sequences)

Data Science

49.3 Tokens and Embeddings

49.3 Tokens आणि embeddings

Models letters नाही — numbers वाचतात.

हे का शिकायचं?

Why. Models letters नाही — numbers वाचतात. Tokenisation text split करते; embeddings token ids → vectors.

हे काय आहे?

What. Beginner view: प्रत्येक word piece ला id; nn.Embedding ids ला trainable vectors बनवते.

कसं करायचं?

How.

Tiny vocabulary demo

  1. word→id dict बनावा.
  2. Short review encode करा.
  3. nn.Embedding ने embed करा.
import torch
import torch.nn as nn

vocab = {"pad": 0, "pedha": 1, "tasty": 2, "not": 3, "good": 4, "stale": 5}
ids = torch.tensor([[1, 2, 0, 0], [3, 4, 0, 0]]) # batch of 2
emb = nn.Embedding(num_embeddings=6, embedding_dim=4)
print(emb(ids).shape) # (2, 4, 4) = batch, seq, dim

Output / जे दिसायला हवं. Shape (batch, sequence length, embed dim).