Examples¶
Walkthroughs and tutorials for using Eggroll Trainer.
Available Examples¶
- Basic Usage - Simple example with both trainers
- MNIST Classification - Full EGGROLL vs SGD comparison
- Custom Trainers - Creating your own ES algorithm
3D Reinforcement Learning¶
Train agents in 3D MuJoCo environments:
See examples/animals_3d/ for 3D RL examples including:
- Simple locomotion (Ant, HalfCheetah, Humanoid)
- Multi-agent scenarios (flocking, predator-prey)
- Advanced physics (underwater, aerial, cloth simulation)
Running Examples¶
All examples are in the examples/ directory:
# Basic example
python examples/basic_example.py
# MNIST comparison
python examples/mnist_comparison.py
# Multi-architecture comparison
python examples/run_all_comparisons.py
Example Structure¶
Each example demonstrates:
- Model Definition - PyTorch model architecture
- Fitness Function - How to evaluate model performance
- Trainer Setup - Configuring the trainer
- Training Loop - Running the training
- Results - Analyzing performance
Quick Example¶
import torch
import torch.nn as nn
from eggroll_trainer import EGGROLLTrainer
# Model
class SimpleModel(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(10, 1)
def forward(self, x):
return self.fc(x)
# Fitness function
def fitness_fn(model):
x = torch.randn(32, 10)
y_pred = model(x)
return y_pred.abs().mean().item()
# Train
model = SimpleModel()
trainer = EGGROLLTrainer(
model.parameters(),
model=model,
fitness_fn=fitness_fn,
population_size=256,
learning_rate=0.01,
sigma=0.1,
)
trainer.train(num_generations=100)
Next Steps¶
- Start with Basic Usage
- See User Guide for detailed explanations
- Check API Reference for API details