Examples

Learn from running code

These examples are linked to the project source. The site does not invent benchmark output or mock a training result.

MNIST MLP

Flatten 28×28 images, apply a 784 → 128 dense layer with ReLU, then produce 10 class logits. The repository example adds batches, Adam, sparse-label cross entropy, and evaluation.

Model definitionexamples/mnist/model.ts
export class SimpleMLP extends nn.Module {
  private readonly flatten: nn.Flatten;
  private readonly fc1: nn.Linear;
  private readonly relu: nn.ReLU;
  private readonly dropout: nn.Dropout;
  private readonly fc2: nn.Linear;

  constructor() {
    super();
    this.flatten = this.register("flatten", new nn.Flatten());
    this.fc1 = this.register("fc1", new nn.Linear(784, 128));
    this.relu = this.register("relu", new nn.ReLU());
    this.dropout = this.register("dropout", new nn.Dropout(0.2));
    this.fc2 = this.register("fc2", new nn.Linear(128, 10));
  }

  forward(input: nn.Tensor): nn.Tensor {
    return this.fc2.forward(this.dropout.forward(
      this.relu.forward(this.fc1.forward(this.flatten.forward(input))),
    ));
  }
}

Browse MNIST source ↗

Tensor operations

The getting-started and Playground examples use rank-2 MatMul, elementwise operations, activation, and explicit result reads. Use them to check CPU and available browser backends with the same input values.

Open the Playground

More source examples