Learn how a music model uses context to assign probabilities to possible next tokens, compares its prediction with the real target, and improves through repeated prediction errors.
About 12 minutes
Guiding question
How does musical context change the probability of the next event?
By the end, you’ll be able to
Explain next-event prediction using notes, symbolic events, waveform samples, and audio tokens.
Interpret a simple probability distribution over possible next outcomes.
Distinguish context, conditioning data, prediction, target, and sampled output.
Explain how cross-entropy loss turns a wrong prediction into a training signal.
Describe why the same trained model can generate different continuations.
Chapter 1 showed that music can be represented as waveform samples, MIDI-style events, spectrograms, embeddings, or compressed audio tokens. Chapter 2 then showed how those representations are organised into training examples.
Now the model has something it can learn from.
For many music models, the basic task is surprisingly simple: look at the available context and predict what comes next.
The next item might be a note event, a time-shift event, one waveform sample, or an audio token selected from a neural codec codebook. The representation changes, but the learning pattern stays similar.
A small melody example
Suppose a training sequence contains:
C4, E4, G4, C5
The model receives the first three notes as context:
C4, E4, G4
Its task is to predict the fourth note. The correct target for this training step is C5.
The model does not normally answer with certainty. It may produce:
C5: 55%
A4: 20%
G4: 12%
E4: 8%
Other notes: 5%
Because the real target is C5, a high probability for C5 produces a smaller error than a low probability for C5.
Why not choose one answer immediately?
Music rarely has only one valid continuation. After a chord, several melodies may sound natural. A drum pattern may repeat, add a fill, or leave a rest. A singer may hold a vowel or begin the next word.
A probability distribution lets the model represent this uncertainty. Common continuations can receive high probability while less common but still plausible choices remain available.
The distribution also gives training a useful scale. Predicting the correct target with 60% probability is better than assigning it 2%, even when neither prediction is perfect.
The prediction task across music representations
Swipe sideways to view the full comparison
Representation
Possible context
Possible next target
Real model example
Raw waveform
Earlier audio samples
The next sample value
WaveNet
Symbolic events
Earlier notes, durations, velocities, and time shifts
The next musical event token
Music Transformer
Semantic audio tokens
Earlier high-level audio tokens
The next semantic token
AudioLM
Neural codec tokens
Earlier compressed audio tokens
The next codec token
AudioLM and MusicGen
Text-conditioned codec tokens
Earlier tokens plus a written description
The next codec token that also fits the description
MusicGen
Examples in practice
Real-world example: WaveNet
WaveNet modelled raw audio directly. It represented the joint probability of a waveform as a product of predictions, where each audio sample was conditioned on all earlier samples. The paper showed that the same approach could generate speech and musical fragments, but raw audio requires prediction at tens of thousands of sample positions per second.
Real-world example: Music Transformer
Music Transformer applied sequence prediction to symbolic musical events. Its relative-attention design helped it work with long sequences and generate minute-long piano performances with recurring motifs and larger-scale structure. The model still predicted events step by step, but its context mechanism helped it refer back to earlier musical material.
Real-world example: AudioLM
AudioLM converted audio into discrete token sequences and treated generation as a language-modelling problem. It combined token types designed for long-term structure with neural codec tokens designed for acoustic detail. The researchers demonstrated speech and piano continuations from short audio prompts without requiring symbolic music notation.
Real-world example: MusicGen
MusicGen predicts streams of compressed EnCodec tokens with a Transformer language model. It can also receive a text description or melody as conditioning. The context therefore includes both previously generated audio tokens and information describing what the music should become.
Context can include more than earlier music
In an unconditional model, the earlier sequence may be the main context. In a conditioned model, extra information guides the probabilities.
For MusicGen, a prompt such as slow ambient piano with soft rain changes the distribution over upcoming audio tokens. Tokens associated with piano-like timbre, slow pacing, and quiet texture should become more likely than they would be under an unrelated prompt.
This is why Chapter 2, Section 3 placed so much emphasis on caption quality. Vague or unsupported captions teach weaker or incorrect relationships between language and sound.
Turning a whole sequence into many predictions
A sequence model can describe the probability of an entire sequence by breaking it into next-step probabilities:
The full training track therefore creates many prediction tasks. Each position supplies a context and a target. This is far more efficient than treating the whole song as one indivisible answer.
One sequence creates several training steps
Swipe sideways to view the full comparison
Step
Context
Target
1
Start token
C4
2
C4
E4
3
C4, E4
G4
4
C4, E4, G4
C5
From scores to probabilities
The model first produces one numerical score for every possible next token. These unnormalised scores are often called logits.
A softmax operation converts the logits into probabilities that add up to one. A token with a much larger logit receives a larger probability, but every candidate can retain some probability unless it is removed by a decoding rule.
During training, cross-entropy implementations commonly accept logits directly and compare them with the target token.
Learning from prediction error
After the model produces its scores, the training system reveals the real target from the dataset.
A common loss for token prediction is cross-entropy. It gives a larger penalty when the model assigns low probability to the correct target and a smaller penalty when the target already has high probability.
The training algorithm then adjusts the model's parameters in a direction that should reduce similar errors in the future. One correction is tiny. Repeated across many positions, examples, and training steps, these corrections shape the model's behaviour.
One training step
1
Read the context
The model receives earlier tokens and any conditioning information.
2
Produce token scores
It assigns a logit to every possible next token.
3
Compare with the target
The real next token comes from the training example.
4
Calculate the loss
The loss measures how poorly the prediction matched the target.
5
Update the parameters
Optimisation changes the model so the target should receive a better score in similar contexts.
Training and generation are not the same step
Swipe sideways to view the full comparison
Stage
What follows the probability distribution
Purpose
Training
The known target from the dataset
Calculate loss and update model parameters
Generation
A selected token from the model distribution
Extend the new musical sequence
Choosing a token during generation
During generation, the correct next token is unknown because the model is creating a new sequence. A decoding rule must choose from the predicted distribution.
Greedy decoding selects the highest-probability token every time. Sampling draws from the distribution, so several likely tokens can be chosen across different runs.
MusicGen's official implementation supports greedy selection, temperature-based sampling, top-k sampling, and top-p sampling. These settings change how the distribution is used. They do not retrain the model.
Ways to choose the next token
Swipe sideways to view the full comparison
Method
How it chooses
Possible effect
Greedy
Always selects the highest-probability token
Predictable but can become repetitive
Temperature sampling
Reshapes probabilities before drawing
Controls how concentrated or varied the choices are
Top-k
Samples only from the k highest-scoring tokens
Removes the long tail of very unlikely choices
Top-p
Samples from the smallest set whose combined probability reaches a threshold
Adapts the candidate set to the shape of the distribution
In practice
Why one prompt can produce several songs
The prompt changes the probabilities, but it does not normally reduce the next step to one certain token. Sampling can select different plausible tokens, and each selected token becomes part of the context for the following prediction. Small early differences can therefore lead to clearly different musical continuations.
Context changes the answer
The same three-note ending can lead to different predictions depending on what came before it.
A short context may show only C4, E4, G4. A longer context may reveal that the piece repeats an eight-bar theme, that the harmony recently moved to another key, or that the rhythm follows a call-and-response pattern.
Music Transformer was designed to improve access to long-range musical relationships. AudioLM also separated token roles so long-term structure and acoustic detail could both be modelled. Context is therefore not only about how much information is present. It is also about whether the architecture and representation make that information usable.
Interactive lesson
Predict the Next Musical Token
Try it: Start with the symbolic note sequence and guess the next token before revealing the model distribution. Change the earlier context, then add a text condition. Finally, compare greedy selection with sampling at several temperatures. Repeat the exercise with codec token mode.
A sequence model uses earlier tokens and any conditioning data to score possible next tokens. Softmax converts the scores into probabilities. During training, the distribution is compared with the known target. During generation, a decoding rule selects a token and adds it to the context.
From one prediction to a full piece
One next-token prediction produces only one event. Music appears when the process repeats:
Predict a distribution.
Select a token.
Add it to the context.
Predict again.
The next section, Autoregressive Music Models, follows this loop from the first token to a complete musical sequence. It will also show why small early choices can reshape everything that follows.
Check your understanding
Ready for a quick check?
Test whether you can distinguish context, probabilities, targets, losses, and token selection.
Ready to continue?
Save this section to your account and continue from any device.