Mixture of Experts from Scratch — Part 1: The Foundations (1991–1993)
Building Mixture of Experts from the ground up — adaptive expert networks, gating functions, the mixture-of-Gaussians interpretation, hierarchical mixtures, and the EM algorithm — all derived step by step with a 2-expert regression example.
We are going to build the Mixture of Experts (MoE) framework from scratch, starting from the two papers that created it: Jacobs, Jordan, Nowlan & Hinton (1991) and Jordan & Jacobs (1993). By the end of this post, we will have derived every equation in both papers using a single running example — a system of 2 experts trying to learn a piecewise-linear function.
The Running Example
Suppose we have data from a function that behaves differently in two regions of the input space. For , the true function is (a line with slope ), and for , the true function is (a line with slope ). A single linear model cannot capture both regions simultaneously. But two linear experts — one specializing in negative , the other in positive — can.
We will use 2 experts, each a simple linear function:
and a gating network that decides how much to trust each expert for a given input . Concretely, let us work with three training cases:
| Case | Input | Target |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
Cases 2 and 3 come from the region; case 1 comes from the region. We want our system to learn this decomposition automatically.
1. The Cooperative Error Function (and Why It Fails)
The most obvious approach is to linearly combine the expert outputs and compare the blend to the target. Hampshire & Waibel (1989) and Jacobs et al. (1990) used exactly this idea. Let be the proportion that the gating network assigns to expert for case , and let be the output of expert on case . The cooperative error on case is:
This is the squared difference between the desired output and the weighted combination of all expert outputs.
The problem with cooperation. To minimize this error, each expert must produce its output and cancel the residual error left by the combined effects of all other experts. The gradient of with respect to expert ‘s output is:
Let us derive it carefully. From the definition:
We apply the chain rule of calculus. The derivative of with respect to a parameter inside is times the derivative of . The quantity inside the norm that depends on is . So:
This is equation (1.4) from Jacobs et al. (1991) — but notice the crucial coupling: the gradient for expert depends on the outputs of all other experts through the sum . When the weights in other experts change, the residual changes, and so the error derivatives for expert change too. This strong coupling causes the experts to cooperate — many experts contribute small pieces to each case — rather than specialize.
Numerical check
Let us verify with our running example. Suppose at initialization both experts output 0 () and the gating network assigns equal proportions for all cases. For case 1 (, ):
Both experts get the exact same gradient — they are pushed in the same direction. There is no mechanism forcing them to specialize. This is the fundamental limitation of the cooperative error.
2. The Competitive Error Function
Jacobs et al. (1991) proposed a beautifully simple fix: instead of blending expert outputs, imagine that the gating network makes a stochastic decision about which single expert to use on each occasion. The error becomes the expected squared difference when the gating network stochastically selects one expert:
This is equation (1.2) in Jacobs et al. (1991). Notice the critical difference: each expert is now responsible for producing the entire output , not just a piece of it. The term is the error expert would make if it alone had to handle case .
Why this encourages specialization. Let us take the derivative with respect to . Only the -th term in the sum depends on :
This is equation (1.4) from the paper. Now the gradient for expert depends only on expert ‘s own output and the target — no other experts appear. The experts are decoupled. Each expert is pushed toward the target independently, weighted by its responsibility .
Numerical check
With the same initialization (, ), for case 1:
The total error is the same as before. But now:
At initialization, the gradients are identical — but as training proceeds and the gating network shifts responsibility (increasing for the expert that does better on case ), the expert that is losing responsibility gets a smaller gradient magnitude, while the winning expert gets a larger one. The competition is built into the weighting.
3. The Gating Network and Softmax
The gating network receives the same input as the experts and produces the mixing proportions . These must be positive and sum to 1 — they represent probabilities. The standard way to achieve this is the softmax function.
The gating network computes a linear function of the input for each expert:
where is the weight vector for expert in the gating network, and is the input (including a bias term of 1). Then:
This is the softmax function (Bridle, 1989). It maps any real-valued vector to a probability distribution: every and .
Numerical check
In our 2-expert example, suppose the gating network weights for case 1 () produce and . Then:
Expert 1 gets 95.3% of the responsibility for case 1. We can verify: . The softmax has converted arbitrary real numbers into a valid probability distribution.
4. The Mixture of Gaussians Interpretation
Here is where Jacobs et al. (1991) make a deep connection. The competitive error function in equation (1.2) was motivated by a stochastic selection argument, but in practice the authors used a different error function that gives better performance:
This is equation (1.3) in the paper. Where does it come from?
The term is (up to a normalizing constant) a Gaussian probability density centered at the expert’s output with unit variance, evaluated at the target . So the sum is proportional to the probability of generating under a mixture of Gaussians — a model where the output is generated by first picking expert with probability , then drawing from a Gaussian centered at .
The negative log of a probability is the negative log-likelihood. Minimizing is equivalent to maximizing the likelihood that the mixture model generated the observed target. This is a fundamental principle: the maximum likelihood estimation (MLE) framework.
Deriving the gradient under the log-likelihood error
Let us derive for this new error function. We have:
Let , so .
By the chain rule:
Now . Only the -th term depends on . Let . We need .
The exponent is . By the chain rule (derivative of is , and derivative of with respect to where is ):
So:
Putting it together:
Now define the posterior probability (or responsibility) of expert for case :
This is Bayes’ theorem in action: is the posterior probability that expert generated the target , given the prior and the Gaussian likelihood . So the gradient becomes:
This is equation (1.5) from Jacobs et al. (1991). Compare this to the gradient from the simple competitive error (equation 1.4): . The crucial difference is that (the prior) has been replaced by (the posterior). The posterior takes into account how well expert actually fits the data, not just the gating network’s prior assignment. An expert that fits the data well gets a large posterior even if its prior is moderate, and an expert that fits poorly gets a small posterior even if it has a large prior.
This is the part that makes the system work. Early in training, all experts have similar outputs, so the posteriors are close to the priors . As training proceeds and one expert begins to fit a particular case better, its posterior for that case increases, giving it a larger gradient and thus faster learning on that case. The system spontaneously discovers which expert should handle which subset of the data.
Numerical check
Suppose expert 1 has output on case 1 () and expert 2 has output . The gating network gives , .
Errors: , .
Gaussian likelihoods: , .
Mixture: .
Posteriors:
Expert 1 started with prior and ended with posterior . Its responsibility increased because it fits case 1 better ( vs. , error ) compared to expert 2 ( vs. , error ). The Bayesian update shifted responsibility toward the better-fitting expert.
Gradients:
Interestingly, expert 2 gets a larger gradient magnitude () despite having lower responsibility, because it is farther from the target. (Recall that as signed numbers, but magnitude means distance from zero — see the prerequisites post.) But the gating network will increasingly route case 1 away from expert 2 as training continues, reducing its responsibility toward zero.
5. The Architecture
Let us now put the pieces together into a complete architecture, as shown in Figure 1 of Jacobs et al. (1991).
All networks receive the same input . The experts produce outputs . The gating network produces mixing proportions via softmax. The selector stochastically chooses one expert according to , or in practice we train using the log-likelihood error which uses the mixture of all experts weighted by their responsibilities.
6. From Flat to Hierarchical: The HME Architecture
Two years after the original paper, Jordan & Jacobs (1993) introduced the Hierarchical Mixture of Experts (HME). The key insight: instead of having a single flat layer of experts, arrange them in a tree.
In a two-level hierarchy with 2 branches at each level, we get 4 expert networks at the leaves and 3 gating networks (one at the top, two at the second level):
The top-level gating network produces probabilities and (which branch to take). The lower-level gating networks produce conditional probabilities (which expert within branch ).
Each expert network produces output as a generalized linear function of the input:
where is a weight matrix and is a link function. For regression, is the identity (linear experts). For classification, could be the logistic function. This is the framework of generalized linear models (GLIMs) from statistics (McCullagh & Nelder, 1983).
The gating networks also use the generalized linear framework. At the top level:
This is the softmax function again — but now in the context of a log-linear probability model, a special case of GLIM commonly used for multiway classification.
The total output is:
Numerical check with our running example
Let us extend our 2-expert flat example to a 2-level hierarchy with 4 experts. Suppose for input the gating outputs are:
- Top level: ,
- Branch 1: ,
- Branch 2: ,
Expert outputs: , , , .
Branch outputs:
Total output:
For target , this gives error . Expert is doing the heavy lifting with output , and the hierarchy is correctly routing most responsibility through branch 1 () and then to expert ().
7. The Probability Model
Jordan & Jacobs (1993) gave the hierarchy a precise probabilistic interpretation. The mechanism for generating data involves a nested sequence of decisions:
- First, pick a top-level branch with probability — this is a multinomial decision.
- Then, within branch , pick an expert with conditional probability — another multinomial decision.
- Finally, generate output from the probability density centered at expert ‘s prediction.
The total probability of generating from is a mixture of the component densities, weighted by the multinomial probabilities:
This is equation (4) from Jordan & Jacobs (1993). For regression with Gaussian noise, the expert density is:
where is the expert’s predicted mean and is the covariance (the dispersion parameter in GLIM terminology).
This model belongs to the exponential family of densities, which includes Gaussians, Bernoulli, Poisson, and many others. This is not a coincidence — Jordan & Jacobs deliberately designed the architecture to fit within the GLIM framework, which provides a unified treatment of regression, classification, and counting problems.
8. Posterior Probabilities via Bayes’ Theorem
Given the probability model, we can compute how much each node should be “blamed” (or credited) for generating a particular data point. These are the posterior probabilities.
The gating outputs and are prior probabilities — they are computed from the input alone, before seeing the target .
After observing both and , we update our beliefs using Bayes’ theorem:
Top-level posterior:
This is equation (5) from Jordan & Jacobs (1993). The numerator is the joint probability that we chose branch AND generated . The denominator is the total probability of (summing over all paths). The ratio gives the probability that branch was responsible, given the observed output.
Lower-level conditional posterior:
This is equation (6). It gives the probability that expert within branch generated the data.
Joint posterior:
This is the probability that expert specifically generated the data point, accounting for both levels of the hierarchy.
Numerical check
Continuing our example with , , and assuming Gaussian densities with unit variance:
Expert likelihoods: , , , .
Branch likelihoods (weighted by lower gating):
Total: .
Top-level posteriors:
Branch 1’s responsibility increased from prior to posterior because expert fits the data well. Lower-level posteriors within branch 1:
Joint posteriors: , .
Expert has a joint posterior of — it bears 79% of the responsibility for this data point. This makes sense: it predicted for a target of , the closest of all experts.
9. The Log-Likelihood and Maximum Likelihood Estimation
The log-likelihood of the entire dataset is obtained by taking the log of the product of densities of the form of equation (4):
This is equation (7) from Jordan & Jacobs (1993). We want to maximize this function with respect to all parameters (expert weights and gating weights).
The problem: the logarithm sits outside the summation over experts. This log-of-a-sum structure makes direct gradient computation messy — each parameter affects the log-likelihood through a complex ratio. This is where the EM algorithm comes in.
10. The EM Algorithm
The Expectation-Maximization (EM) algorithm (Dempster, Laird & Rubin, 1977) is an iterative approach to maximum likelihood estimation. It is designed for exactly the situation we face: the likelihood would be easy to maximize if we knew some hidden variables, but those variables are unknown.
The key idea: missing data
Imagine we had indicator variables that tell us which expert generated each data point: if expert generated data point , and otherwise. Exactly one is 1 for each data point.
If we knew the ‘s, the complete-data log-likelihood would be:
This is equation (8) from Jordan & Jacobs (1993). Compare this to the incomplete-data log-likelihood in equation (7). The indicator variables have allowed the logarithm to be brought inside the summation signs, by the logarithm product rule . This substantially simplifies the maximization problem because the parameters of different experts and gating networks now appear in separate terms.
The E step
We do not know the ‘s, so we take their expected value given the data and current parameters. The expected value of is:
This is just the posterior probability that we computed in Section 8. So the E step simply computes the posterior probabilities at every node using the current parameter values.
The expected complete-data log-likelihood (the function) is:
This is equation (9) from the paper.
The M step
Now we maximize with respect to . The beauty of the complete-data formulation is that the parameters separate. Using (the logarithm product rule):
The first term depends only on the top-level gating parameters. The second depends only on the lower-level gating parameters. The third depends only on the expert parameters. We can maximize each separately.
Expert parameters: For each expert :
This is a weighted maximum likelihood problem for a generalized linear model, where the weights are the posterior probabilities . This can be solved by iteratively reweighted least squares (IRLS), a standard algorithm for GLIMs (McCullagh & Nelder, 1983).
Top-level gating parameters:
This is a weighted maximum likelihood problem for a log-linear (softmax) model. The “observations” are the data points , the “targets” are the posterior probabilities , and we are fitting a softmax model to predict them.
Lower-level gating parameters follow the same pattern.
The complete HME algorithm
Putting it all together:
- E step: For each data pair , compute posteriors and using current parameters.
- M step (experts): For each expert , solve a weighted IRLS problem with observations and weights .
- M step (top gating): Solve a weighted IRLS problem with observations and weights .
- M step (lower gating): For each branch , solve a weighted IRLS problem with observations and weights .
- Iterate with updated parameters.
The convergence guarantee
Dempster, Laird & Rubin (1977) proved that every EM iteration increases the incomplete-data log-likelihood:
with equality only at stationary points of . This is a powerful result. It says the EM algorithm is guaranteed to climb the likelihood surface — it never goes downhill. In practice, this means convergence to a local maximum.
The proof relies on the relationship between the complete and incomplete likelihoods. An increase in (the expected complete-data likelihood) implies an increase in (the incomplete-data likelihood). This is because where is an entropy term that depends on the missing data distribution, and the EM update is designed to increase while cannot decrease under the same update.
11. The On-line Algorithm
Jordan & Jacobs (1993) also developed an on-line version using recursive estimation theory (Ljung & Söderström, 1986). Instead of processing the entire dataset in each iteration, we update parameters after each individual data point.
The update rule for expert ‘s weight matrix is:
This is equation (10) from the paper. Here is the inverse covariance matrix for expert , updated via:
where is a decay parameter. This is the Sherman-Morrison-Woodbury formula applied to recursive least squares — it maintains a running estimate of the weighted covariance without storing all past data.
The structure of the update is intuitive: the change to the expert weights is proportional to:
- : the posterior probability (how responsible this expert is)
- : the prediction error (how wrong the expert was)
- : a curvature-adjusted input (second-order information)
12. Experimental Results: Speed and Task Decomposition
Jacobs et al. (1991) tested their system on a 4-class vowel discrimination task using formant data from 75 speakers. With 4 or 8 very simple experts (each restricted to a linear decision surface), the mixture achieved 90% test accuracy — matching a backpropagation network with 6 or 12 hidden units — but converging in roughly half the number of epochs.
| System | Train % | Test % | Avg. Epochs | SD |
|---|---|---|---|---|
| 4 Experts | 88 | 90 | 1124 | 23 |
| 8 Experts | 88 | 90 | 1083 | 12 |
| BP 6 Hid | 88 | 90 | 2209 | 83 |
| BP 12 Hid | 88 | 90 | 2435 | 124 |
The mixture system converged roughly twice as fast as backpropagation with less variance. The system automatically discovered the task decomposition: different experts specialized in different vowel pairs.
Jordan & Jacobs (1993) tested the HME on a robot dynamics identification problem (4-joint arm, 12 inputs, 4 outputs). The results were dramatic:
| Architecture | Relative Error | Epochs |
|---|---|---|
| Linear | .31 | 1 |
| Backprop | .09 | 5,500 |
| HME (batch) | .10 | 35 |
| Backprop (on-line) | .08 | 63 |
| HME (on-line) | .12 | 2 |
The HME batch algorithm converged in 35 epochs compared to backpropagation’s 5,500 epochs — a factor of 157x speedup — with comparable accuracy. The on-line HME converged in just 2 passes through the data.
Summary
We have built the Mixture of Experts framework from first principles. The cooperative error function couples experts and prevents specialization. The competitive error function decouples them by asking each expert to produce the entire output. The log-likelihood error function replaces priors with Bayesian posteriors, giving the system a principled way to discover which expert should handle which data. The Hierarchical Mixture of Experts extends this to a tree structure, and the EM algorithm provides a convergence-guaranteed learning procedure that decomposes into a collection of weighted generalized linear model fits. The on-line version brings recursive estimation theory to bear, enabling learning in a single pass through the data. These two papers — Jacobs et al. (1991) and Jordan & Jacobs (1993) — laid the mathematical foundations that every subsequent MoE paper builds upon.
In Part 2, we will see how Shazeer et al. (2017) scaled this framework to thousands of experts and billions of parameters, and how Fedus et al. (2021) simplified it further with the Switch Transformer.
Previous: Mathematical Prerequisites for Mixture of Experts
Next: Mathematical Prerequisites for Mixture of Experts — Part 2
Enjoyed this post?
Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.