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.cs224n contains implementations and utilities for the CS224N assignments.
  • dnnlpy.cs336 contains implementations and utilities for the CS336 assignments.
  • dnnlpy.models contains higher-level model architectures or model-specific components, such as MiniGPT and other models introduced in the notes.
  • 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.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.14 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.gpt as gpt

model = gpt.MiniGPT(
    vocab_size=1000,
    block_size=128,
    embed_dim=256,
    num_layers=4,
    num_heads=4,
    hidden_dim=1024,
)

input_ids = torch.randint(0, 1000, (2, 32))
logits = model(input_ids)

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.

Metadata

Release files for dnnlpy 2026.10.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dnnlpy 2026.10.2
File Size Uploaded
dnnlpy-2026.10.2.tar.gz 92.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dnnlpy 2026.10.2
File Interpreter ABI Platform
dnnlpy-2026.10.2-py3-none-any.whl Python 3 none any Details

Total release size: 221.1 kB

Release files / dnnlpy-2026.10.2.tar.gz

Download URL dnnlpy-2026.10.2.tar.gz
Size 92.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ea80567773c0ee42f71a5c67f30199c37f88c90cf7977b651247c3aed169228b
BLAKE2b-256 checksum
How to use checksums
1533d0db085c15968e16f096fa577619dc447ab93244a2b0ea9d4cdf445b2619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","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}

Release files / dnnlpy-2026.10.2-py3-none-any.whl

Download URL dnnlpy-2026.10.2-py3-none-any.whl
Size 128.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b39693babb673be88cc14586ed33c7ac7eaebc6ba8284c4381e772eaaa2eac45
BLAKE2b-256 checksum
How to use checksums
2be35179ac7c4b3e79fba7da942694100137587a71772c4d35f7ad30860ae539
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","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}
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