← all repositories
srush/Tensor-Puzzles

Learn PyTorch by forbidding PyTorch

A collection of 21 one-line puzzles that break your reflex to reach for `torch.sum` by forcing you to rebuild it from broadcasting primitives.

4.2k stars Jupyter Notebook Learning
Tensor-Puzzles
Not currently ranked — collecting fresh signals.
star history

What it does

Tensor Puzzles is a notebook of 21 exercises—styled like chess puzzles—where each asks you to reimplement a single NumPy standard-library function (such as cumsum, outer, or sequence_mask) in one line of PyTorch. The catch is a strict whitelist: you may use only broadcasting, arithmetic, comparison, indexing, and a handful of primitives like arange and where. Standard helpers such as view, sum, squeeze, or even the tensor constructor are off-limits.

The interesting bit

The project treats tensor programming as a language to be spoken fluently rather than a library to be memorized. By stripping away the “magic” convenience functions, you are forced to manipulate shape and broadcasting directly, which is exactly the skill that separates copy-paste StackOverflow solutions from actual comprehension.

Key highlights

  • 21 puzzles ranging from ones and diag to scatter_add and sequence_mask
  • Each solution must fit in a single line under 80 columns
  • Visual SVG diagrams illustrate the input-output relationship for every puzzle
  • Includes a YouTube walkthrough if you get stuck
  • Designed to run in Google Colab with minimal dependencies

Caveats

  • The README cuts off mid-list during the scatter_add description, so the full roster of later puzzles is incomplete in the source
  • Some puzzle descriptions are extremely terse (e.g., “Compute pad_to”), so you may need to infer exact behavior from the reference spec

Verdict

Grab this if you are teaching PyTorch, interviewing for ML engineering roles, or simply tired of cargo-culting tensor code you do not fully understand. Skip it if you are looking for a reference manual or production-ready utilities; this is strictly a pedagogical constraint engine.

Frequently asked

What is srush/Tensor-Puzzles?
A collection of 21 one-line puzzles that break your reflex to reach for `torch.sum` by forcing you to rebuild it from broadcasting primitives.
Is Tensor-Puzzles open source?
Yes — srush/Tensor-Puzzles is open source, released under the MIT license.
What language is Tensor-Puzzles written in?
srush/Tensor-Puzzles is primarily written in Jupyter Notebook.
How popular is Tensor-Puzzles?
srush/Tensor-Puzzles has 4.2k stars on GitHub.
Where can I find Tensor-Puzzles?
srush/Tensor-Puzzles is on GitHub at https://github.com/srush/Tensor-Puzzles.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.