Skip to main content

dnnlpy

dnnlpy is the companion Python package for Deep Learning Notes. It provides code examples, helper functions, and small utilities used throughout the tutorial, similar in spirit to the d2l package for Dive into Deep Learning.

The package structure is similar to PyTorch, but keeps a clear boundary between reusable neural network building blocks and complete model implementations:

  • dnnlpy.nn contains general neural network modules, such as attention layers, positional encodings, and other reusable components.
  • dnnlpy.nn.functional contains stateless helper functions, such as functional attention implementations.
  • dnnlpy.optim contains small optimizer implementations for teaching purposes, such as SGD and Adam.
  • dnnlpy.models contains higher-level model architectures or model-specific components, such as ViT, DDPM, or other models introduced in the notes.
  • dnnlpy.cs336 contains implementations and utilities for the CS336 assignments.
  • dnnlpy.tokenizers contains small tokenizer implementations for teaching purposes, such as a simple BPE tokenizer.

The APIs are designed to feel close to their PyTorch counterparts where practical, while still keeping the code lightweight and easy to read for tutorial purposes.

This package is intended as a lightweight code supplement rather than a general-purpose deep learning framework. Its goal is to make the examples in the notes easier to run, reuse, and extend.

What is this package for?

The dnnlpy package is designed to support the code in the Deep Learning Notes tutorial.

It can be used to:

  • Organize example code from the notes
  • Provide reusable utility functions
  • Reduce repeated boilerplate in notebooks and scripts
  • Make tutorial examples easier to reproduce

In short, this package serves as the code companion to the tutorial.

Requirements

  • Python 3.12 or newer
  • PyTorch 2.13 or newer

Installation

Install the published package from PyPI with:

uv pip install dnnlpy

To install the latest version directly from this repository, use:

uv pip install "git+https://github.com/jshn9515/deep-learning-notes.git#subdirectory=dnnlpy"

This project uses uv for local package development.

git clone https://github.com/jshn9515/deep-learning-notes.git
cd dnnlpy
uv pip install .

If you want to modify the package while working through the notes, editable installation is recommended:

uv pip install -e .

This way, changes to the source code take effect immediately without reinstalling the package each time.

Examples

After installation, you can import reusable neural network modules from dnnlpy.nn:

import dnnlpy.nn as dnn
import torch

attn = dnn.MultiheadAttention(embed_dim=16, num_heads=4)

query = torch.randn(2, 8, 16)
key = torch.randn(2, 8, 16)
value = torch.randn(2, 8, 16)

output = attn(query, key, value)

You can also import stateless functions from dnnlpy.nn.functional:

import dnnlpy.nn.functional as dF
import torch

query = torch.randn(2, 4, 8, 16)
key = torch.randn(2, 4, 8, 16)
value = torch.randn(2, 4, 8, 16)

output, weights = dF.scaled_dot_product_attention(
    query,
    key,
    value,
    need_weights=True,
)

Higher-level model architectures live under dnnlpy.models:

import torch
import dnnlpy.models.vit as vit

model = vit.ViTForImageClassification(
    image_size=224,
    patch_size=16,
    in_channels=3,
    num_classes=1000,
    embed_dim=768,
    num_heads=12,
    num_layers=12,
)

images = torch.randn(2, 3, 224, 224)
logits = model(images)

The dnnlpy.models.mlp package contains small NumPy modules for teaching manual forward and backward passes:

import dnnlpy.models.mlp as mlp
import numpy as np

model = mlp.MLP(input_dim=4, hidden_dim=8, num_classes=3)
loss_fn = mlp.CrossEntropyLoss()
optimizer = mlp.SGD(model.parameters(), lr=0.1)

x = np.random.randn(2, 4)
targets = np.array([0, 2])

logits = model(x)
loss = loss_fn(logits, targets)
model.backward(loss_fn.backward())

optimizer.step()
optimizer.zero_grad()

A simple rule of thumb is:

  • Use dnnlpy.nn when a component is reusable across many models.
  • Use dnnlpy.models when the code represents a complete architecture or is tightly coupled to one model family.

License

This project is licensed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dnnlpy-2026.8.24.tar.gz (92.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dnnlpy-2026.8.24-py3-none-any.whl (129.4 kB view details)

Uploaded Python 3

File details

Details for the file dnnlpy-2026.8.24.tar.gz.

File metadata

  • Download URL: dnnlpy-2026.8.24.tar.gz
  • Upload date:
  • Size: 92.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dnnlpy-2026.8.24.tar.gz
Algorithm Hash digest
SHA256 fac7cbed6af02669423677c3c0b1e95500420b28cb0f18eaea26d8b5a3288a28
MD5 adfd98363043ef44cf652bea21f12587
BLAKE2b-256 239c7469d6e34d7c1b0d5bb0cbe40adc905bb97eee6e3d79e474e1d22556f885

See more details on using hashes here.

File details

Details for the file dnnlpy-2026.8.24-py3-none-any.whl.

File metadata

  • Download URL: dnnlpy-2026.8.24-py3-none-any.whl
  • Upload date:
  • Size: 129.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dnnlpy-2026.8.24-py3-none-any.whl
Algorithm Hash digest
SHA256 5e0c4a597d237e0718a238cf9951d4b14311d7035c97b50dfb4e5e729ccacab1
MD5 524a25e5a1d437c596c1cf1a2a3a91df
BLAKE2b-256 ed7e623280d15a01460fde218c0b0d5efcc7ede2fa8968d06921891962c72c27

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2026.8.24 This release

2 files

2026.7.21

2 files

2026.6.11

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page