Lunar Boom Learning

3.1 · Prediction as the Basic Learning Task

Section 3.1 of 3.6

Prediction as the Basic Learning Task

How context becomes a set of possible next events

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:

    1. C5: 55%
    2. A4: 20%
    3. G4: 12%
    4. E4: 8%
    5. 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

RepresentationPossible contextPossible next targetReal model example
Raw waveformEarlier audio samplesThe next sample valueWaveNet
Symbolic eventsEarlier notes, durations, velocities, and time shiftsThe next musical event tokenMusic Transformer
Semantic audio tokensEarlier high-level audio tokensThe next semantic tokenAudioLM
Neural codec tokensEarlier compressed audio tokensThe next codec tokenAudioLM and MusicGen
Text-conditioned codec tokensEarlier tokens plus a written descriptionThe next codec token that also fits the descriptionMusicGen

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:

P(x1, x2, ..., xT) = P(x1) × P(x2 | x1) × P(x3 | x1, x2) × ...

At position t, the model estimates:

P(xt | x1, ..., x(t-1), condition)

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

StepContextTarget
1Start tokenC4
2C4E4
3C4, E4G4
4C4, E4, G4C5

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

StageWhat follows the probability distributionPurpose
TrainingThe known target from the datasetCalculate loss and update model parameters
GenerationA selected token from the model distributionExtend 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

MethodHow it choosesPossible effect
GreedyAlways selects the highest-probability tokenPredictable but can become repetitive
Temperature samplingReshapes probabilities before drawingControls how concentrated or varied the choices are
Top-kSamples only from the k highest-scoring tokensRemoves the long tail of very unlikely choices
Top-pSamples from the smallest set whose combined probability reaches a thresholdAdapts 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:

  1. Predict a distribution.
  2. Select a token.
  3. Add it to the context.
  4. 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.

Checking your account…
Sources for this lesson (9)
  1. WaveNet: A Generative Model for Raw Audio — Aaron van den Oord et al. (2016)
  2. Music Transformer — Cheng-Zhi Anna Huang et al. (2018)
  3. Music Transformer: Generating Music with Long-Term Structure — Google Magenta (2018)
  4. AudioLM: A Language Modeling Approach to Audio Generation — Zalan Borsos et al. (2022)
  5. AudioLM Examples — Google Research (2022)
  6. Simple and Controllable Music Generation — Jade Copet et al. (2023)
  7. MusicGen: Simple and Controllable Music Generation — Meta AI
  8. CrossEntropyLoss — PyTorch contributors
  9. MusicCaps Dataset Card — Google (2023)
Browse all contextual sources →