Train a Transformer on real genome sequences — from scratch, in PyTorch. Every concept explained the first time it appears.
About
I think this technology is fascinating. I want to explore modeling biological sequences with the recent novel architectures. I felt like I need to start small here. I put this together in a tutorial format to help me easily retrace some concepts when I need to. I'd be glad to get some feedback or help on some helpful resources to keep exploring. This is a hands-on tutorial for building a DNA language model — the same architecture behind DNABERT, Nucleotide Transformer, and Evo — using nothing but a plain CPU (or Apple Silicon GPU) and open-access genome data.
Downloads human mitochondrial DNA and a C. elegans fragment directly from public FASTA files — no login required.
~50 readable lines of PyTorch. Causal attention, masked self-attention, blocks — every part built and explained step by step.
Trains in ~2–3 min on CPU. Automatically uses CUDA, Apple MPS, or CPU — whichever is available.
Tracks perplexity against meaningful baselines. Generates novel DNA and verifies it reproduces real 3-mer frequency patterns.
Fetch real FASTA files, measure GC content and base composition.
Character-level tokenization: map A C G T to integers.
Multi-head causal self-attention, feed-forward blocks, LayerNorm.
AdamW optimizer, cosine LR schedule, live loss/perplexity curve.
Sample new sequences; compare 3-mer frequencies to real genomes.
Pointers to DNABERT, Nucleotide Transformer, HyenaDNA, and Evo.
Project Log
Updated as the project evolves. Newest entries at the top.
A C G TKMP_DUPLICATE_LIB_OK)Current Results
Trained on ~20 k bases of real genome sequence, ~2 min on CPU.
Beating both baselines confirms the model learned real sequential structure in DNA — not just letter frequencies.
Figures
The full DNAGPT stack: token embedding + positional encoding → N× transformer blocks (pre-LayerNorm, causal multi-head self-attention, feed-forward with residual connections) → final LayerNorm → language model head.
Left: cross-entropy loss dropping below the random baseline (ln 4 ≈ 1.39). Right: validation perplexity falling well below both the uniform (4.0) and base-composition (~3.87) baselines across 3 000 iterations.
Side-by-side comparison of every 3-mer frequency in real genome sequence (blue) vs. 6 000 bases generated by the model (orange). High correlation (~0.96) shows the model internalized real DNA sequence structure.
Quick Start
# 1. Clone
git clone https://github.com/EnuhBlaise/DNA_LargeLanguageModel.git
cd DNA_LargeLanguageModel
# 2. Install dependencies
pip install torch matplotlib requests jupyter
# 3. Launch
jupyter notebook dna_language_model_tutorial.ipynb
macOS / Anaconda users:
if the kernel crashes on import torch, the notebook already
applies the fix automatically — see the note cell inside the notebook.
Apple Silicon:
MPS GPU acceleration is detected and used automatically.
Google Colab:
upload the .ipynb → Runtime → Run all.
For GPU: Runtime → Change runtime type → T4 GPU.