๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #274

Embeddings (LLMs)

Building on Word Embeddings, this note covers the input embedding layer specifically as it functions inside a large-scale LLM โ€” including a common weight-sharing trick worth knowing.

The Embedding Layer, at LLM Scale

\[ \mathbf{E} \in \mathbb{R}^{\text{vocab\_size} \times d_{\text{model}}} \]

For a model with a 50,000-token vocabulary and \(d_{\text{model}}=4096\), the embedding matrix alone has over 200 million parameters โ€” often a genuinely significant fraction of a smaller model's total size, though proportionally less significant for the very largest models where the transformer blocks themselves dominate.

Weight Tying โ€” Sharing Input and Output Embeddings

Recall from Vocabulary that both the input embedding layer and the final output projection layer have matching shapes (\(\text{vocab\_size}\times d_{\text{model}}\), just transposed relative to each other). Many LLM implementations deliberately tie these two weight matrices together โ€” using the exact same learned matrix for both converting tokens into vectors and converting the final hidden state back into vocabulary logits. This roughly halves the parameter count contributed by these two layers, and is motivated by the intuition that "how a token should be represented" and "what vector should predict this token" are closely related concepts worth sharing.

Code

import torch.nn as nn

class TiedEmbeddingModel(nn.Module):
    def __init__(self, vocab_size, d_model):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, d_model)
        self.output_layer = nn.Linear(d_model, vocab_size, bias=False)
        self.output_layer.weight = self.embedding.weight   # WEIGHT TYING: same parameters, shared

model = TiedEmbeddingModel(vocab_size=50000, d_model=768)
print(model.embedding.weight.shape)       # (50000, 768)
print(model.output_layer.weight.shape)    # (50000, 768) -- literally the SAME tensor, not a copy

Common Mistakes

  • Assuming weight tying is always beneficial regardless of model scale โ€” for very large models, the relative parameter savings from tying become proportionally less significant, and some architectures intentionally choose not to tie for other performance reasons.
  • Forgetting that tied weights mean a gradient update to one layer automatically affects the other โ€” since they're literally the same underlying parameter tensor, not two separately-initialized tensors that happen to start out equal.

Interview Relevance

Q: "What is weight tying in an LLM, and why is it used?" Sharing the exact same weight matrix between the input token embedding layer and the final output projection layer (which converts hidden states into vocabulary logits) โ€” since both have matching shapes and arguably related roles. This roughly halves the parameter count these two layers would otherwise contribute, at little to no cost in model quality for many architectures.

Practice Question

For a vocabulary of 32,000 tokens and \(d_{\text{model}}=1024\), how many parameters does the embedding layer alone contain? How many would the (separate) output layer add without weight tying?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Embeddings (LLMs) โ€“ FAQs

Quick answers about learning Embeddings (LLMs) in Deep Learning.

This free note from CodingNow 2.0 explains Embeddings (LLMs) in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Embeddings (LLMs), is 100% free with no signup required.
With focused practice, most students grasp Embeddings (LLMs) in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now