Building a Transformer from Scratch: My 6.

JordanCat Expert 2h ago Updated Jul 26, 2026 347 views 11 likes 2 min read

API calls and prompt engineering are great, but they keep you one abstraction layer away from actually understanding how LLMs function. To break out of that cycle, I decided to ditch OpenAI and HuggingFace endpoints for my recipe app, Rasaveda, and instead build a custom decoder-only transformer in PyTorch.

The goal was simple: create a model capable of reasoning about recipes and ingredients without relying on external vendors, rate limits, or per-token costs.

The Architecture

I designed RasavedaGPT as a tiny, decoder-only transformer. Since the domain is narrow, I didn't need a behemoth; I sized the model specifically for the task so it could run comfortably on a CPU within a FastAPI backend.

  • Total parameters: 6,392,320
  • Vocabulary size: 6,000 (custom BPE)
  • Context length: 512 tokens
  • Embedding dimension: 256
  • Attention heads: 8
  • Transformer layers: 6
  • Feed-forward dim: 1,024

Training Workflow

I found that starting from random initialization and jumping straight into recipe data was too difficult for the model to grasp both fluency and domain knowledge. Instead, I used a two-stage AI workflow.

Stage 1: General Language Acquisition
I pre-trained on WikiText-2 for 3 epochs using a cosine learning rate schedule. This was purely to teach the model the basic structure of English. By the end of this stage, the perplexity dropped from 345.3 to 114.3.

Stage 2: Domain Specialization
I then shifted to fine-tuning on a specific dataset of 2,139 recipe examples. I ran this for 12 epochs, repeating the data 8x per epoch to ensure the model locked in the patterns. The loss dropped significantly, from 2.439 in the first epoch to 0.787 by the third.

Real-World Takeaways

Building this from scratch provided a level of insight that prompt engineering simply can't. When your loss curve refuses to budge, you're forced to dive deep into positional embeddings and attention mechanisms to figure out why.

For anyone looking for a practical tutorial on LLM internals, I highly recommend trying a small-scale deployment like this. It proves that for specific, narrow tasks, a well-trained 6M parameter model can be more efficient and predictable than a general-purpose giant.

# The project is available here
https://github.com/cherimedz/Rasaveda
LLMmachinelearningpytorchLarge Language Model

All Replies (4)

D
Drew15 Expert 10h ago
I did this last year; manually coding the attention heads really makes the math click.
0 Reply
A
AlexTinkerer Advanced 10h ago
Try adding some dropout to the layers if you run into overfitting during training.
0 Reply
P
Pat31 Advanced 10h ago
Good call, I'll keep that in mind. Do you think 0.1 is usually enough for this scale?
0 Reply
M
MicroPanda Intermediate 10h ago
Did you use a custom weight initialization or just stick with Xavier/Kaiming for the linear layers?
0 Reply

Write a Reply

Markdown supported