Skip to main content

A character-level language model built from scratch using only numpy.

Project description

pokepy :)

Alt text

a character-level language model built completely from scratch using only numpy that generates pokémon sounding names. no PyTorch, no autograd, no deep learning frameworks — every forward pass, backward pass, and gradient update is manually implemented.

this project started as a way to understand how neural networks actually work under the hood. I first built a simple MLP, then expanded it into a WaveNet-style architecture to explore how increasing context length changes what a model can learn.

demo

this project is fully distributed on PyPI. follow these steps to set up an isolated sandbox environment and run the demo across macOS, Linux, or Windows.

initialize a venv

to bypass native system file locks on macOS and ensure dependencies install safely, create and activate a localized virtual environment:

On macOS/Linux:

python3 -m venv .venv
source .venv/bin/activate 

On windows:
python -m venv .venv
.venv\Scripts\activate

### install the package
pip install pokepy-generator

### launch the app

Run the terminal execution shortcut from anywhere in the activated enviornment:
pokepy

Then open your browser and head to the local link:



---

# what is this

I wanted to understand the foundations behind language models, so I built a mini character-level text generator completely from scratch.

instead of relying on existing machine learning libraries, I manually implemented:

* embeddings
* linear layers
* batch normalization
* tanh activations
* softmax
* cross entropy loss
* backpropagation
* gradient descent

everything runs only with numpy.

the model learns character patterns from pokémon names and generates new names based on the relationships it discovers.

---

# model 1 — MLP

## architecture

the first version was a simple multilayer perceptron:

* character embedding layer (10-dimensional vectors)
* linear layer
* batch normalization
* tanh activation
* output linear layer
* softmax + cross entropy loss

hidden size:

200 neurons


context length:

3 characters


this means the model only looks at the previous 3 characters to predict the next one.

example:

pik → a ika → next character


the context continuously shifts as the model generates.

---

## MLP results

training:

train loss: 1.294 validation loss: 3.504


generated names:

blipedeedo rosalini lect dartic star vigus swannon hippowdon the larvinerao


the MLP learned basic character relationships, but the limited context window made it difficult to understand longer patterns inside names.

---

# model 2 — WaveNet

## why I built this

the biggest limitation of the MLP was context length.

with only 3 characters of context, the model could only see a small part of each name.

for example:

charizard

cha har ari riz


the model does not understand the larger structure of the word.

WaveNet improves this by gradually combining groups of characters, allowing the model to build larger representations without massively increasing the number of parameters.

---

## architecture

WaveNet-style architecture:

* character embedding layer (10 dimensions)
* FlattenConsecutive layers
* multiple linear layers
* batch normalization
* tanh activations
* final output layer
* softmax + cross entropy loss

context length:

8 characters


the model builds information hierarchically:

characters

combined character groups

higher level features

next character prediction


---

## WaveNet results

training:

train loss: 1.949 validation loss: 2.748


generated names:

gropinig pyghislacat poloun hoongel spuspiniyan ongtover kasato xel felspipon linmatie asherron beatdiqdule madstutf drudona rouzslra liwsywunk galeon magnoslaws araidono lickopt


WaveNet produced longer and more structured generations because it had access to a larger context window.

---

# MLP vs WaveNet

|                  | MLP                 | WaveNet             |
| ---------------- | ------------------- | ------------------- |
| Context size     | 3 characters        | 8 characters        |
| Hidden size      | 200                 | 32                  |
| Training steps   | 300,000             | 10,000              |
| Architecture     | Single hidden layer | Hierarchical layers |
| Feature learning | Direct              | Progressive         |
| Main advantage   | Simple baseline     | Larger context      |

---

# challenges

## context length

one of the biggest lessons from this project was understanding why context matters.

a model with a smaller context window can only learn local patterns, while larger context allows it to understand longer relationships.

the MLP used:

3 character context


while WaveNet increased this to:

8 character context


which allowed it to capture more structure from names.

---

## batch normalization

implementing batch normalization manually was one of the hardest parts.

I had to handle:

* batch mean
* batch variance
* running mean
* running variance

training and inference use different statistics, so saving the running values was required for the deployed model to generate correctly.

---

## backpropagation

instead of using:

```python
loss.backward()

I manually calculated gradients for:

  • embeddings
  • linear layers
  • batch normalization
  • tanh activations
  • softmax cross entropy

this helped me understand how neural networks actually learn instead of treating them as black boxes.


random generation

generation is probabilistic.

even with the same trained model, outputs change because the next character is sampled from the model's probability distribution.


training details

MLP

dataset:

pokemon names

training:

  • optimizer: SGD
  • batch size: 32
  • steps: 300,000
  • learning rate: 0.1 → 0.01 after 100k steps

parameters:

C
W1
W2
b2
bngain
bnbias

WaveNet

dataset:

pokemon names

training:

  • optimizer: SGD
  • batch size: 32
  • steps: 10,000
  • learning rate: 0.1 → 0.01 after 8000 steps

parameters:

embeddings
linear layers
batch normalization parameters

deployment

the model is deployed using Hugging Face Spaces with Gradio.

the demo loads the trained numpy weights and runs inference without PyTorch or external ML frameworks.

the deployed model uses:

  • trained embeddings
  • linear layer weights
  • batch normalization parameters
  • vocabulary mappings

the entire inference pipeline runs using manually implemented numpy layers.


usage

install dependencies:

pip install numpy

run:

python mlp.py
python wavenet.py

you will need:

data/

└── pokemon.txt

with one pokémon name per line.


what I learned

this project taught me how language models are built from the ground up.

the biggest takeaway was that improving a model is not always about making it bigger. changing the architecture and giving the model better ways to understand context can have a larger impact than simply adding more parameters.


license

MIT

heavily inspired by Andrej Karpathy's makemore series — highly recommend if you want to understand neural networks from the inside out :)

Project details


Download files

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

Source Distribution

pokepy_generator-1.0.5.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

pokepy_generator-1.0.5-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file pokepy_generator-1.0.5.tar.gz.

File metadata

  • Download URL: pokepy_generator-1.0.5.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pokepy_generator-1.0.5.tar.gz
Algorithm Hash digest
SHA256 52bb148d02fb923597dd8021fd455767404b7afa0b09616bd4d5d38dffc35cca
MD5 7dad9e4e106381e2676783e3d2c828ea
BLAKE2b-256 119a9d51029179a9c9bf991200c274abd27df4bc1360c410074195fe24ae9bb5

See more details on using hashes here.

File details

Details for the file pokepy_generator-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pokepy_generator-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pokepy_generator-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9cc32e8b62bb8337c587cfd5c73a1c6ee21f2c5ac3ed167f17f171e6bffe59cb
MD5 98a1dae5ccad487a3a41a4a5db968b7c
BLAKE2b-256 917dcca3b00f28b45ea0c218f04586e1e0c72217e0f7e64eeb13588b01308535

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page