GitHub

EvoTransformer

EvoTransformer v0.5 β€” Self-Correcting, Self-Evolving Language Model

EvoTransformer is a lightweight Transformer-based language model that features a built-in self-correction mechanism and self-evolving architecture search. Inspired by biological evolution, the model refines its own predictions through a dedicated CorrectionHead and evolves its own hyperparameters via neuroevolution during training.

Spec Value
Parameters 614K (0.000614B)
Architecture Transformer + CorrectionHead
Training Data Tiny Shakespeare (~1.1MB)
Tokenizer Character-level (67 tokens)
Context Window 64 tokens
Device CPU / CUDA (auto-detected)
Framework PyTorch
License MIT

Model Description

EvoTransformer is not just a standard language model β€” it has two unique biological-inspired features:

Self-Correction Loop

The model has a CorrectionHead that reviews and refines its own initial predictions. Think of it as the model proofreading its own output:

Input β†’ Transformer Blocks β†’ Initial Prediction (first guess)
                ↓                        ↓
         Hidden States + Initial Prediction
                ↓
         CorrectionHead β†’ Corrected Prediction (refined)

Multiple correction passes can be applied iteratively, with each pass further refining the output using a blending strategy that increases trust in corrections over successive passes.

Self-Evolution

An evolutionary algorithm runs alongside gradient-based training:

  • A population of candidate architectures (varying d_model, n_heads, n_layers, d_ff, lr, dropout) is maintained
  • Every 5 epochs, candidates are evaluated on validation data
  • The best architectures survive, the worst are replaced
  • New architectures emerge via mutation and crossover
  • If an evolved architecture outperforms the current model, the model switches to the better one

Architecture Details

EvoTransformer (614,150 parameters)
β”œβ”€β”€ Embedding Layer (67 β†’ 128)
β”œβ”€β”€ Sinusoidal Positional Encoding (parameter-free)
β”œβ”€β”€ 4Γ— Transformer Blocks
β”‚   β”œβ”€β”€ LayerNorm β†’ Multi-Head Attention (4 heads, d=128)
β”‚   └── LayerNorm β†’ Feed-Forward (GELU, 256 hidden)
β”œβ”€β”€ Output LayerNorm
β”œβ”€β”€ Output Head (128 β†’ 67) β€” Initial logits
└── CorrectionHead β€” Corrected logits
    β”œβ”€β”€ Prediction Projection (67 β†’ 128)
    β”œβ”€β”€ Merge Layer (256 β†’ 128, GELU)
    └── Correction Layer (128 β†’ 128 β†’ 67)

Training Details

Hyperparameter Value
Optimizer AdamW
Learning Rate 3e-4 (cosine decay with warmup)
Warmup Steps 100
Batch Size 32
Sequence Length 64
Weight Decay 0.01
Gradient Clipping 1.0
Correction Loss Weight 0.3
Dropout 0.1

Loss Function: Combined loss = (0.7 Γ— base_cross_entropy) + (0.3 Γ— correction_cross_entropy)

Training Results (20 epochs, CPU):

Metric Value
Train Loss 1.6021
Validation Loss 1.6661
Base Accuracy 51.6%
Corrected Accuracy 50.8%
Training Time ~78 minutes (CPU, 4 cores)

Usage

Installation

pip install torch numpy

Training from Scratch

# Train for 20 epochs (dataset downloads automatically)
python train.py --epochs 20

# Stop anytime with Ctrl+C (checkpoint saved automatically)

# Resume training later
python train.py --resume --epochs 50

Chat Interface

# Interactive chat with self-correction comparison
python chat.py

# Chat without comparison display
python chat.py --no-compare

# Adjust generation settings
python chat.py --temperature 0.6 --max-len 300 --passes 3

Chat Commands:

Command Description
/temp N Set sampling temperature (0.1–2.0)
/len N Set max generation length
/passes N Set correction passes (1–5)
/compare Toggle initial vs corrected comparison
/clear Clear conversation context
/quit Exit

Inference (Programmatic)

import torch
from model import EvoTransformer
from dataset import CharTokenizer
from self_correction import SelfCorrectionLoop

# Load
device = torch.device("cpu")
ckpt = torch.load("checkpoints/evo_transformer_ckpt.pt", map_location=device, weights_only=False)
model = EvoTransformer.from_genome(ckpt["genome"], dropout=0.0).to(device)
model.load_state_dict(ckpt["model_state_dict"])
model.eval()

tokenizer = CharTokenizer()
tokenizer.load("data/tokenizer.json")

# Generate
prompt = "ROMEO:"
tokens = tokenizer.encode(prompt)
input_ids = torch.tensor([tokens], device=device)

corrector = SelfCorrectionLoop(n_passes=2)
with torch.no_grad():
    for _ in range(200):
        ctx = input_ids[:, -model.max_seq_len:]
        result = corrector.correct(model, ctx)
        logits = result["corrected_logits"][:, -1, :] / 0.8
        probs = torch.softmax(logits, dim=-1)
        next_token = torch.multinomial(probs, 1)
        input_ids = torch.cat([input_ids, next_token], dim=1)

print(tokenizer.decode(input_ids[0].tolist()))

Sample Output (after 20 epochs)

KING RICHARD II:
Thy lord, an thou shalt see the crown
That makes the gentle king to weep...

My could begs to the four hope love,
So Edward, and new, what you shall see...

Comparison with Other Models

Feature EvoTransformer v0.5 GPT-2 Small Llama 3 8B BERT Base
Parameters 614K 124M 8B 110M
Runs on CPU βœ… Slow ❌ Slow
Self-Correction βœ… ❌ ❌ ❌
Self-Evolution βœ… ❌ ❌ ❌
Train from Scratch ~1-2 hours (CPU) Days (GPU) Months (cluster) Days (GPU)
Memory ~100 MB ~500 MB ~16 GB+ ~500 MB

Limitations

  • Small model: 614K parameters limits the complexity and coherence of generated text
  • Character-level tokenizer: Less efficient than BPE/WordPiece tokenizers used by larger models
  • Single dataset: Trained only on Tiny Shakespeare; limited generalization to other domains
  • Not conversational: Generates text continuations, not dialogue responses
  • Correction head effectiveness: At this model size, the correction head shows modest improvements; benefits increase with model scale

Use Cases

  • βœ… Learning how Transformer language models work internally
  • βœ… Experimenting with self-correction and neuroevolution concepts
  • βœ… Running AI experiments on CPU-only machines
  • βœ… Rapid prototyping and research on efficient architectures
  • ❌ Not suitable for production chatbot or real-world NLP tasks

Files

File Description
config.py Centralized hyperparameters and device detection
model.py EvoTransformer architecture with CorrectionHead
evolution.py Evolutionary algorithm (mutation, crossover, selection)
self_correction.py Multi-pass self-correction loop
dataset.py Dataset download, character tokenizer, data pipeline
train.py Training with checkpoint save/resume and Ctrl+C safety
chat.py Interactive chat with self-correction comparison
inference.py Batch inference and demo script

Citation

@misc{evotransformer2026,
  title={EvoTransformer: A Self-Correcting, Self-Evolving Language Model},
  author={lordhet},
  year={2026},
  url={https://huggingface.co/lordhet/EvoTransformer-v0.5-0.000614b}
}
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for lordhet/EvoTransformer-v0.5-0.000614b

Unable to build the model tree, the base model loops to the model itself. Learn more.

Dataset used to train lordhet/EvoTransformer-v0.5-0.000614b