Free How much of your firm runs on you? Take the 2-minute Key Man Score.
Lesson reading
0%

Lesson 7 / Unit 2: Training Data

Batch Construction

Pad variable-length audio without letting padding become training evidence.

A batch needs rectangular tensors, but utterances have different frame counts. The solution is to pad the feature sequences and carry their true lengths through every stage that changes time resolution.

Batch Shapes

x = pad_sequence([r['x'] for r in examples], batch_first=True)
input_lengths = torch.tensor([len(r['x']) for r in examples])
y = torch.cat([r['y'] for r in examples])
target_lengths = torch.tensor([len(r['y']) for r in examples])

The lab packs targets end to end. The target-length vector tells the loss how to divide that stream back into examples. Padding targets with blank symbols would change the target sequence rather than merely filling space.

Length Accounting

Our two stride-two convolutions each produce ceil(length / 2) frames. The code computes this using integer arithmetic:

output_lengths = ((input_lengths + 1) // 2 + 1) // 2

Those are the lengths passed to CTC. The bidirectional GRU uses packed sequences so its backward pass does not treat padded frames as later audio. The convolution still has its declared boundary padding; the mask does not remove that architectural choice.

PyTorch’s CTC interface specifies the log-probability and length inputs. Keep a tiny batch where you can inspect all dimensions before running the full experiment.

Invalid Alignments

A target with adjacent repeated symbols needs separator frames under CTC. For example, 77 cannot be represented by only two consecutive frames labelled seven: they collapse to one. More aggressive time downsampling can make some targets impossible.

Your output is one batch with four inspected tensors: features, input lengths, targets and target lengths. Character Tokens explains the vocabulary; CTC Alignment explains the collapse rule.

We do this work for teams shipping voice agents: Self-Hosted Voice Agent .

Edit this lesson on GitHub ↗

Discussion

Questions, corrections and war stories welcome. Sign in with GitHub to comment.

Free guide

Be The 10x Entrepreneur

Get the firm out of your head.

  • 8 chapters
  • 7 worksheets
  • 30 day plan