Output mask in a guitar tablature neural network

Author

João Gabriel Oliveira

Published

September 17, 2026

Introduction

Unlike a piano, a pitch like C3 can be played in two positions on the guitar, on the 3rd fret of the A string, or on the 8th fret of the low E. The list of options is arithmetic, but which one to pick depends on where your hand already is, on “what’s idiomatic” for a real guitarist.

The problem with converting MIDI to guitar tablature automatically is that MIDI only signals which pitches sounded at a given time, and the converter has to decide where on the neck to play them.

I read a paper (Kaliakatsos-Papakostas et al. 2025) that does this conversion with a neural network. They feed it the last four positions and the current pitches, and it outputs a score for every (fret, string) combination. This means the network never sees which combinations are possible in the first place. That comes from a separate search step, which:

  • lists every fingering that produces the current pitches;
  • keeps the ones a hand can play (one note per string, no stretch wider than six frets);
  • picks whichever of those the network scored the highest.

Here I try a simpler approach: a smaller network, but one that already knows which combinations are possible before it scores them. The idea is to measure how much this helps the model learn idiomatic fingering, instead of spending gradient on a rule that’s just arithmetic.

Dataset

Training needs tab-labeled MIDI data. The paper uses DadaGP, which has 26,181 songs in GuitarPro (GP) format. GP format stores each note as a string and a fret, and the matching MIDI pitch can be derived from that.

Since DadaGP isn’t publicly downloadable, I’m using guitar_tab, a Hugging Face dataset built from GP files converted into a text format called alphaTex.

Tablature inputs

Each row in the dataset is a song. Its columns are file, the original GuitarPro filename, and text, the alphaTex song content.

import pandas as pd
df = pd.read_parquet("../data/raw/data.parquet")
df.head()
text file
0 \title "Hocus Pocus"\n\tempo 160\n.\n\track\n\... Focus_-_Hocus_Pocus_(main_riff).tex
1 \title "crackers"\n\tempo 120\n.\n\track\n\ins... Focus_-_Crackers.tex
2 \title "anonymous 2"\n\tempo 96\n.\n\track\n\i... Focus_-_Anonymous_2.tex
3 \title "Sylvia"\n\tempo 115\n.\n\track\n\instr... Focus_-_Sylvia_(2).tex
4 \title "euridice"\n\tempo 120\n.\n\track\n\ins... Focus_-_Euridice.tex


In a song, each track initializes a new instrument and has \instrument, \tuning, and \frets attributes; the example below only has one track.

example = df.iloc[0]
body = example["text"]
print(body)
\title "Hocus Pocus"
\tempo 160
.
\track
\instrument 29
\tuning E5 B4 G4 D4 A3 E3
\frets 24
:8 (5.4{h} 0.5) 7.4 (7.4 0.5) (7.4 0.5) (7.4 0.5).4 (7.4 0.5) (7.4 0.5) | 
(5.4{h} 0.5) 7.4 (7.4 0.5) (7.4 0.5) (7.4 0.5).4 (7.4 0.5) (7.4 0.5) | 
:4 (7.1 8.2 7.3 9.4 7.5) (8.1 10.2 9.3 10.4 8.5) r (8.2 9.3 9.4 8.6) | 
(0.2 0.3 0.4 0.6) (6.2 7.3 7.4 6.6) (8.2 7.3 6.4 7.5).2 | 
:8 (7.2 5.4{h} 0.5) 7.4 (7.4 0.5) (7.4 0.5) (7.4 0.5).4 (7.4 0.5) (7.4 0.5) | 
(5.4{h} 0.5) 7.4 (7.4 0.5) (7.4 0.5) (7.4 0.5).4 (7.4 0.5) (7.4 0.5) | 
:4 (7.1 9.2 9.3 9.4 7.5) (8.1 10.2 10.3 10.4 8.5) r (5.1 7.2 7.3 7.4 5.5) | 
(0.1 0.2 0.3 0.4 0.5) (7.1 7.2 8.3 9.4 9.5 7.6) (7.1 9.2 9.3 9.4 7.5) r | 
(0.1 2.2 2.3 2.4 0.5) (0.1{d} 2.2{d} 2.3{d} 2.4{d} 0.5{d}).8 (3.1 5.2 5.3 5.4 3.5) (0.1 2.2 2.3 2.4 0.5).16 (5.1 7.2 7.3 7.4 5.5) | 
:1 (0.1 0.2 0.3 0.4 0.5) | 
:4 (5.1 7.2 7.3 7.4 5.5) (5.1{d} 7.2{d} 7.3{d} 7.4{d} 5.5{d}).8 (8.1 10.2 10.3 10.4 8.5) (5.1 7.2 7.3 7.4 5.5).16 (10.1 12.2 12.3 12.4 10.5) | 
:1 (0.1 0.2 0.3 0.4 0.5) | 
:4 (8.2 7.3 6.4 7.5) r r.2


AlphaTex uses these notation tokens:

  • :N: sets the base duration for the following notes (e.g., :8 eighth notes, :4 quarter notes);
  • fret.string: a single note (e.g., 7.4 is fret 7 on string 4);
  • (...): multiple notes played together;
  • .N attached to a note/chord overrides its duration, ignoring the current :N;
  • {...}: a technique on a note (e.g., {h} for hammer-on);
  • r: a rest;
  • |: bar line.

Tidying tracks

The steps:

  • keep only eligible tracks, meaning standard tuning (E5 B4 G4 D4 A3 E3) with 24 frets;
  • drop tracks whose notes are identical to one already kept;
  • keep only the (fret, string) positions, dropping the rest of the tokens;
  • trim the leading rests and drop the tracks that are nothing but rests.

The third step is what changes the format. It tokenizes the body, drops anything that isn’t a note or a chord, simplifies technique annotations, and encodes survivors as lists of tuples:

After the encoding, each track is a list of frames, each one a variable-length list of (fret, string) positions.

from src.transform import (
    split_alphatex_into_track_blocks,
    parse_alphatex_track,
    convert_track_body_to_tab_frames
)

track = parse_alphatex_track(split_alphatex_into_track_blocks(body)[0])
tab_frames = convert_track_body_to_tab_frames(track["body"])

print(tab_frames)
[[(5, 4), (0, 5)], [(7, 4)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(5, 4), (0, 5)], [(7, 4)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 1), (8, 2), (7, 3), (9, 4), (7, 5)], [(8, 1), (10, 2), (9, 3), (10, 4), (8, 5)], [], [(8, 2), (9, 3), (9, 4), (8, 6)], [(0, 2), (0, 3), (0, 4), (0, 6)], [(6, 2), (7, 3), (7, 4), (6, 6)], [(8, 2), (7, 3), (6, 4), (7, 5)], [(7, 2), (5, 4), (0, 5)], [(7, 4)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(5, 4), (0, 5)], [(7, 4)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 4), (0, 5)], [(7, 1), (9, 2), (9, 3), (9, 4), (7, 5)], [(8, 1), (10, 2), (10, 3), (10, 4), (8, 5)], [], [(5, 1), (7, 2), (7, 3), (7, 4), (5, 5)], [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)], [(7, 1), (7, 2), (8, 3), (9, 4), (9, 5), (7, 6)], [(7, 1), (9, 2), (9, 3), (9, 4), (7, 5)], [], [(0, 1), (2, 2), (2, 3), (2, 4), (0, 5)], [(0, 1), (2, 2), (2, 3), (2, 4), (0, 5)], [(3, 1), (5, 2), (5, 3), (5, 4), (3, 5)], [(0, 1), (2, 2), (2, 3), (2, 4), (0, 5)], [(5, 1), (7, 2), (7, 3), (7, 4), (5, 5)], [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)], [(5, 1), (7, 2), (7, 3), (7, 4), (5, 5)], [(5, 1), (7, 2), (7, 3), (7, 4), (5, 5)], [(8, 1), (10, 2), (10, 3), (10, 4), (8, 5)], [(5, 1), (7, 2), (7, 3), (7, 4), (5, 5)], [(10, 1), (12, 2), (12, 3), (12, 4), (10, 5)], [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)], [(8, 2), (7, 3), (6, 4), (7, 5)], [], []]


Everything imported from src lives in the article’s repository.

Representing a frame

A natural way to represent a frame is a 6x25 binary grid (one row per string and one column per fret, counting the open string). But dense layers need a fixed-size 1D input, so that grid gets flattened into a 150-element vector. Here’s the whole track playing frame by frame:

Training pairs

Each training example is a flat 728-element vector:

  • the last four tablature frames (4x150 = 600 binary digits);
  • the current frame’s pitches, derived from its tab positions (128 binary digits).

The target is the current tablature frame itself, the 150-element vector the network has to predict.


Zoomed out, the pipeline looks like this:

%%{
    init: {
        'themeVariables': {
            'fontFamily': 'Space Grotesk',
            'fontSize': '15px'
        },
        'flowchart': {
            'curve': 'stepBefore',
            'nodeSpacing': 30,
            'rankSpacing': 30
        }
    }
}%%

flowchart LR
    dataset[("dataset row")]

    subgraph trackstack[" "]
        direction TB
        dotsTop["<span style=font-size:28px>⋮</span>"]
        trackJPlus1("track j+1")
        trackJ("track j")
        trackJMinus1("track j-1")
        dotsBottom["<span style=font-size:28px>⋮</span>"]
    end

    frames("tab_frames")

    tabi("positions at i (150)")

    subgraph positionsGroup[" "]
        direction TB
        tabi1("positions at i-1 (150)")
        tabi2("positions at i-2 (150)")
        tabi3("positions at i-3 (150)")
        tabi4("positions at i-4 (150)")
    end

    context("context (600)")
    pitches("pitches (128)")
    input("input (728)")
    network("network")
    output("output (150)")

    dataset --- trackJ
    trackJ --- frames
    frames --- tabi
    frames --- positionsGroup
    tabi1 ~~~ tabi2
    tabi2 ~~~ tabi3
    tabi3 ~~~ tabi4
    positionsGroup --- context
    tabi -- "extract pitches from current position" --- pitches
    pitches --- input
    context --- input
    input --- network
    network --- output

    class dataset,frames,trackJ,tabi1,tabi2,tabi3,tabi4,tabi,network,output neutral
    class trackJPlus1,trackJMinus1 unused
    class dotsTop,dotsBottom dots
    class context,pitches,input current

    style positionsGroup fill:#ffffff,stroke:#525252

    style trackstack fill:none,stroke:none

    classDef current fill:#ffe8e6,stroke:#FF160A,color:#FF160A
    classDef history fill:#f5f5f5,stroke:#525252,color:#525252
    classDef unused fill:#ffffff,stroke:#a3a3a3,color:#a3a3a3
    classDef neutral fill:#ffffff,stroke:#525252,color:#525252
    classDef dots fill:none,stroke:none,color:#a3a3a3

Scale

guitar_tab has 47,299 songs, which turn into 100,780 eligible tracks and just under 47 million frames, a median of 352 per track. Every frame is one training example, so precomputing all of them means 47 million rows, each one with 728 numbers of input and 150 of target, around 41GB.

That’s why the cache on disk keeps the compact (fret, string) positions instead, and the dense arrays get built at train time.

Training the model

The models get trained at six data sizes (500 to 16,000 tracks). Every run occurs outside this document, and the results are loaded here.

The candidate mask

For a given pitch, the list of possible positions is a lookup from the tuning. Take the C3 from the intro, MIDI pitch 48:

from src.transform import candidate_positions
candidate_positions(48)
[(3, 5), (8, 6)]


Precisely the 3rd fret on the 5th string, and the 8th fret on the 6th.

For every frame we build a candidate mask with candidate_positions, a 150-element vector marking every position that could produce that frame’s pitches:


Given only the 728 input, the network learns idiomatic playing but also spends gradient pushing down positions that arithmetic already rules out.

The mask goes into the model alongside the input. From there:

  • the network still scores all 150 positions;
  • inside the forward pass, before the loss is computed, PyTorch’s masked_fill method replaces every score outside the mask by -1e9;
  • the sigmoid turns that into zero, so an impossible position never gets predicted.

Training dataset

Sample:

  • 500 tracks for validation and 500 for test, the same in every run;
  • training sets from 500 to 16,000 tracks, each one containing the smaller ones.

To avoid leakage, the split is done by song, not by track (one song can hold several eligible tracks and they can be very similar to each other).

from src.dataset import load_processed_tracks_dataset, split_tracks_by_song
from src.transform import convert_tracks_to_input_and_candidate_mask_arrays

path = "../data/processed/tab_tracks.parquet"
df_files = pd.read_parquet(path, columns=["file"])

# val and test are drawn before train
_, val_files, test_files = split_tracks_by_song(df_files, n_train=1, n_val=500, n_test=500, seed=1)

df_heldout = load_processed_tracks_dataset(path, files=val_files + test_files)
df_val = df_heldout[df_heldout["file"].isin(val_files)]
df_test = df_heldout[df_heldout["file"].isin(test_files)]

X_val, M_val, y_val = convert_tracks_to_input_and_candidate_mask_arrays(df_val["tab_frames"])
X_test, M_test, y_test = convert_tracks_to_input_and_candidate_mask_arrays(df_test["tab_frames"])

The builder also returns M, the candidate mask for each frame. Both models will see the same X and y; only one of them looks at M.

Each training size works out to:

tracks frames
500 216,759
1,000 462,810
2,000 932,773
4,000 1,896,702
8,000 3,791,513
16,000 7,547,967

Model definition

The paper uses convolutions to learn fingering shapes, building the fretboard as a 6x25 grid inside the network, so the filters pick up local patterns on it.

Here the focus is measuring the impact of the mask, so we start with a plain MLP, three Linear layers funneling 728 down to 150 with ReLU in between. Both take (x, mask), so the same training loop works for either, but the baseline will ignore the mask.

import torch
import torch.nn as nn

def build_net(n_input):
    return nn.Sequential(
        nn.Linear(n_input, 512),
        nn.ReLU(),
        nn.Linear(512, 256),
        nn.ReLU(),
        nn.Linear(256, 150),
    )

class BaselineModel(nn.Module):
    def __init__(self, n_input):
        super().__init__()
        self.net = build_net(n_input)

    def forward(self, x, _mask):
        return self.net(x)

class MaskedModel(nn.Module):
    def __init__(self, n_input):
        super().__init__()
        self.net = build_net(n_input)

    def forward(self, x, mask):
        logits = self.net(x)
        return logits.masked_fill(mask == 0, -1e9)

Parameters and loss function

A frame can have zero, one or several positions active at once, so BCEWithLogitsLoss scores each of the 150 outputs on its own instead of picking one winner. It applies the sigmoid internally, so the last layer has no activation.

n_epochs = 15
learning_rate = 0.1
criterion = nn.BCEWithLogitsLoss()

Training runs

Training function
if torch.backends.mps.is_available():
    device = "mps"
elif torch.cuda.is_available():
    device = "cuda"
else:
    device = "cpu"

def evaluate(model, data_loader, metric_fn, aggregate_fn=torch.mean):
    model.eval()
    metrics = []

    with torch.no_grad():
        for X_batch, mask_batch, y_batch in data_loader:
            X_batch = X_batch.to(device).float()
            mask_batch = mask_batch.to(device).float()
            y_batch = y_batch.to(device).float()
            metrics.append(metric_fn(model(X_batch, mask_batch), y_batch))

    return aggregate_fn(torch.stack(metrics))


def train(model, optimizer, criterion, train_loader, val_loader, n_epochs):
    best_val_loss = float("inf")
    best_state = None
    history = []

    for epoch in range(n_epochs):
        model.train()
        total_loss = 0.

        for X_batch, mask_batch, y_batch in train_loader:
            X_batch = X_batch.to(device).float()
            mask_batch = mask_batch.to(device).float()
            y_batch = y_batch.to(device).float()

            loss = criterion(model(X_batch, mask_batch), y_batch)
            total_loss += loss.item()
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

        train_loss = total_loss / len(train_loader)
        val_loss = evaluate(model, val_loader, criterion).item()
        history.append({"epoch": epoch + 1, "train_loss": train_loss, "val_loss": val_loss})

        print(f"epoch {epoch + 1} / {n_epochs}, train {train_loss:.4f}, val {val_loss:.4f}")

        if val_loss < best_val_loss:
            best_val_loss = val_loss
            best_state = {k: v.clone() for k, v in model.state_dict().items()}

    model.load_state_dict(best_state)
    return history
Training loop
from torch.utils.data import TensorDataset, DataLoader

for n_train in sizes:
    train_files, _, _ = split_tracks_by_song(df_files, n_train=n_train, n_val=500, n_test=500, seed=1)
    df_train = load_processed_tracks_dataset(path, files=train_files)
    X_train, M_train, y_train = convert_tracks_to_input_and_candidate_mask_arrays(df_train["tab_frames"])

    train_loader = DataLoader(
        TensorDataset(torch.from_numpy(X_train), torch.from_numpy(M_train), torch.from_numpy(y_train)),
        batch_size=32,
        shuffle=True,
    )

    for Model in (BaselineModel, MaskedModel):
        torch.manual_seed(42)
        model = Model(n_input=728).to(device)

        train(
            model,
            torch.optim.SGD(model.parameters(), lr=learning_rate),
            criterion,
            train_loader,
            val_loader,
            n_epochs=n_epochs,
        )

Results

Loss curves

At 500 tracks the loss curves never meet, but by 16,000 they almost do. In every run validation stays just above train and both are still falling at the last epoch, so nothing is overfitting.

That’s kind of expected. About 1% of the 150 target cells are active per frame on average (at most 6 can ever be on at once, and usually it’s 1-2), and BCEWithLogitsLoss weighs every cell equally, so most of the gradient pushes the model toward saying zero everywhere. A loss curve dropping fast can just mean the model is getting rewarded for the inactive cells.

The candidate mask takes most of those inactive cells out of the loss. What’s left is the choice between positions that can actually produce the pitch. So the curves aren’t measuring the same thing, and the gap between them doesn’t tell us whether the mask made the model better.

To actually tell, we’ll use torchmetrics’ MultilabelExactMatch. It thresholds each of the 150 outputs at 0.5 and counts a frame only when all of them match the target, then reports the fraction of frames that pass.

Exact match analysis

What exact match reveals is that the mask mostly changes how fast the model learns.

  • with 500 tracks the masked model is already at 73%, and 32x more data only takes it to 92%;
  • the baseline starts at 47% and only reaches that same 73% at around 1,400 tracks.

Much of the baseline’s climb is tuning arithmetic learned from examples. The masked model is already handed it, so it learns the idiomatic part of the problem directly, which is why its curve is flatter the whole way.


However, the baseline ends up learning the tuning on its own anyway, as the training set grows. Even so, the 16,000-track models disagree somewhere:

frames
both right 221,706 (90.3%)
only the masked model 3,697 (1.5%)
only the baseline 2,213 (0.9%)
both wrong 17,978 (7.3%)
total 245,594

With more training instances (~7.5 million frames) the masked model still comes out ahead. One might expect the frames it fixes to be pitch errors, but surprisingly that is not the case:

error baseline masked
played the correct pitch somewhere else 15,365 15,833
missed a note 6,230 4,358
predicted an impossible position 80 0
total 21,675 20,191

A few examples:

Conclusion

Both models reach around 91% exact match accuracy on the test set.

Most of what’s left is the idiomatic choice, the right notes played somewhere the original player didn’t. More data lowers it a bit, and the paper also has the search step, which keeps only the fingerings a hand can play, one note per string and no stretch wider than six frets.

The rest is where the mask pays off. A low-cost implementation that

  • never predicts an impossible position, by construction;
  • learns idiomatic playing faster (especially when training data is short), since cells outside the mask carry no gradient and it trains only on the candidates, while the baseline spreads the same signal over 150 cells.

References

Kaliakatsos-Papakostas, Maximos, Gregoris Bastas, Dimos Makris, Dorien Herremans, Vassilis Katsouros, and Petros Maragos. 2025. A Machine Learning Approach for MIDI to Guitar Tablature Conversion. https://arxiv.org/abs/2510.10619.