Skip to main content
This tutorial classifies opaque .serva byte streams without decoding, parsing, or reshaping them into images. The pipeline:
  1. Zero-pad raw bytes to the next power of 2
  2. Fast Walsh-Hadamard Transform (FWHT) — a fixed, orthogonal projection that preserves distances in high-dimensional byte space
  3. Truncate to the first 4 096 low-frequency coefficients
  4. MLP classifier with LayerNorm, GELU, and Dropout
No convolutions, no spatial assumptions, no pretrained weights. The transform exploits the fact that .serva files live in a high-dimensional byte space where distance-preserving projections (Johnson-Lindenstrauss) retain class structure.

Before you start

Dependencies: torch only. No torchvision, no numpy at runtime. Use a GPU when available (CUDA on NVIDIA, MPS on Apple Silicon). Dataset layout: one subfolder per class, with .serva files inside each:
Each .serva file is one training example. Repeat the same class folder names under train/ and val/ so label indices stay aligned. To create .serva files, use the Serva web app, upload sources, encode, and download.
Every .serva file may have a different byte length. The loader pads shorter files with zeros to the next power of 2 so every sample has the same feature dimension. No bytes are truncated.

Load data: read .serva bytes into tensors

All files are read into memory once. For small-to-medium datasets this is fast and avoids per-epoch I/O. For very large datasets, wrap the loading in a Dataset that reads on demand.

Feature extraction: Walsh-Hadamard transform

The Fast Walsh-Hadamard Transform is an orthogonal, distance-preserving projection — analogous to an FFT but over {+1, −1} basis vectors. It maps variable-length byte sequences into a fixed-size feature vector without learning any parameters.
Truncating to the first 4 096 coefficients keeps the lowest-frequency components of the Hadamard spectrum. This acts as dimensionality reduction while preserving the most informative structure. The truncation point was validated empirically — 4 096 is the sweet spot for this dataset size.

Model: MLP classifier

A two-hidden-layer MLP with LayerNorm on the input (the Hadamard coefficients have varying scale), GELU activations, and Dropout for regularization.
pin_memory=True in the DataLoader is only useful with CUDA; on MPS (Apple GPU), omit it. running_corrects.float() in the training loop keeps accuracy math in float32, which works on CUDA, MPS, and CPU — .double() can error on MPS (no float64).

Training loop

Same phase pattern as the standard PyTorch classification tutorial: train / val, model.train() / model.eval(), torch.set_grad_enabled, scheduler step after each train epoch, keep best val accuracy weights.
On the hymenoptera dataset (244 train, 152 val), this reaches ~69% val accuracy in about 10 seconds on a modern GPU. The Hadamard features are precomputed, so the training loop is purely MLP forward/backward — very fast.