liveBook · Manning

Music piece

As explained in Learn Generative AI with PyTorch

A musical work represented here as a sequence of musical notes and events for Transformer training.

A music piece is a musical work represented for machine learning as notes and events. Depending on the model, it becomes either a multidimensional tensor or an indexed event sequence. section 13.3.2 section 14.2

Two representations support different generation workflows:

  • MuseGAN represents a piece as a 4D object with dimensions (4, 2, 16, 84): four tracks, two bars, sixteen steps per bar, and 84 pitch values. section 13.4 section 13.4.1
  • A music Transformer represents a piece as performance-based musical events, assigns each unique event an index, and standardizes sequences to 2,048 indexes. section 14.2
  • Musically, pieces can combine chords, style, melody, and groove, which contribute harmonic structure, identity, recognizable notes, and rhythmic feel. section 13.2.1

In MuseGAN, a generator creates a music piece and a critic scores it from negative infinity to positive infinity. Higher scores indicate greater similarity to real training pieces; convolutional layers extract its spatial features. section 13.4 section 13.4.1

Turning a tensor into playable music

Convert one-hot pitch vectors into pitch numbers from 0 to 83, reshape them into track columns, merge repeated pitches into notes with durations, and write the result as a MIDI file. Repeating the process for a second training song produces a playable exercise artifact. section 13.3.2

Listing 13.2 Converting pitch numbers to a MIDI file
for i in range(4):                                         #1
    last_x = int(midi_note_score[:, i][0])
    s = stream.Part()
    dur = 0
    for idx, x in enumerate(midi_note_score[:, i]):        #2
        x = int(x)
        if (x != last_x or idx % 4 == 0) and idx > 0:
            n = note.Note(last_x)
            n.duration = duration.Duration(dur)
            s.append(n)
            dur = 0
        last_x = x
        dur = dur + 0.25                                   #3
    n = note.Note(last_x)
    n.duration = duration.Duration(dur)
    s.append(n)                                            #4
    parts.append(s)  
parts.write("midi","files/first_song.midi")

Use the conversion listing to inspect how tracks become music21 parts, durations, notes, and a MIDI file. section 13.3.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines tensor and event-sequence structures, model roles, and MIDI conversion, but defers detailed musical composition theory.
Examples
Uses four-track, two-bar tensors; pitch-number tables; MIDI files; and chord, style, melody, and groove controls.
What you can do after
Build and inspect playable MIDI pieces from tensor representations, or prepare indexed event sequences for Transformer batches.
How it connects
Connects MIDI, tokenization, Transformers, MuseGAN, generators, critics, convolutional neural networks, chords, melody, style, and groove.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.