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

Mixture of Experts

A Mixture of Experts (MoE) layer replaces a single, large, densely-activated feed-forward network with several smaller "expert" sub-networks and a learned router that selects only a few of them per input โ€” dramatically increasing total model capacity without proportionally increasing compute cost per token.

The Core Idea โ€” Sparse Activation

Recall the position-wise feed-forward network from Feed-Forward Network โ€” every token passes through the exact same, single feed-forward network. An MoE layer instead maintains \(N\) separate expert networks (each structurally similar to a standard feed-forward network), and for each individual token, a lightweight router selects just a small number (commonly the top-1 or top-2) of experts to actually process that specific token โ€” every other expert is skipped entirely for that token.

The Routing Formula

\[ g(\mathbf{x}) = \text{softmax}(\mathbf{W}_r \mathbf{x}), \qquad \text{output} = \sum_{i \in \text{top-}k} g_i(\mathbf{x}) \cdot E_i(\mathbf{x}) \]

The router computes a softmax score over all \(N\) experts for the current token \(\mathbf{x}\); only the top-\(k\) highest-scoring experts (commonly \(k=1\) or \(k=2\)) are actually computed, and their outputs are combined, weighted by the router's own scores. Every other expert contributes exactly nothing for this specific token โ€” no wasted computation on unused experts.

Why This Is a Genuinely Different Kind of Scaling

Standard Dense ModelMixture of Experts
Total parametersAll active for every tokenCan be very large โ€” only a small fraction active per token
Compute per tokenScales directly with total parameter countScales only with the (much smaller) number of active experts per token
Effective specializationOne shared network handles everythingDifferent experts can specialize in different types of input/patterns

This decoupling โ€” total capacity versus per-token compute cost โ€” is exactly what makes MoE architectures able to reach enormous total parameter counts (sometimes trillions) while keeping the actual compute cost per token comparable to a much smaller dense model.

Diagram

Router Expert 1 (active) Expert 2 (skipped) Expert 3 (active) Expert 4 (skipped)

Only the top-k selected experts actually compute anything for a given token โ€” every other expert is skipped entirely, saving compute.

Code

import torch
import torch.nn as nn

class MixtureOfExperts(nn.Module):
    def __init__(self, d_model, d_ff, num_experts, top_k=2):
        super().__init__()
        self.experts = nn.ModuleList([
            nn.Sequential(nn.Linear(d_model, d_ff), nn.ReLU(), nn.Linear(d_ff, d_model))
            for _ in range(num_experts)
        ])
        self.router = nn.Linear(d_model, num_experts)
        self.top_k = top_k

    def forward(self, x):
        router_logits = self.router(x)
        weights, indices = torch.topk(router_logits.softmax(dim=-1), self.top_k, dim=-1)

        output = torch.zeros_like(x)
        for i in range(self.top_k):
            expert_idx = indices[..., i]
            expert_weight = weights[..., i].unsqueeze(-1)
            for e in range(len(self.experts)):
                mask = (expert_idx == e)
                if mask.any():
                    output[mask] += expert_weight[mask] * self.experts[e](x[mask])
        return output

Common Mistakes

  • Ignoring load balancing between experts โ€” without an additional balancing loss term encouraging roughly even usage across experts, the router can collapse to relying heavily on just a few "favorite" experts, wasting the extra capacity the other experts represent.
  • Confusing MoE's total parameter count with its effective compute cost โ€” a trillion-parameter MoE model can have a per-token compute cost comparable to a much smaller dense model, since only a small fraction of experts activate per token.

Interview Relevance

Q: "How does a Mixture of Experts layer let a model have far more total parameters without a proportional increase in compute cost per token?" Instead of one large, densely-activated feed-forward network processing every token, an MoE layer maintains many smaller expert networks and a lightweight router that selects only a small number (top-1 or top-2) to actually process each specific token. Total parameter count (summed across all experts) can be enormous, but compute cost per token depends only on the small number of experts actually activated, decoupling total capacity from per-token compute.

Practice Question

Why might a Mixture of Experts model risk under-utilizing most of its experts without an explicit load-balancing mechanism during training?

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

Mixture of Experts โ€“ FAQs

Quick answers about learning Mixture of Experts in Deep Learning.

This free note from CodingNow 2.0 explains Mixture of Experts 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 Mixture of Experts, is 100% free with no signup required.
With focused practice, most students grasp Mixture of Experts 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