Example Library
Tensor creation, arithmetic, autograd from scratch. Learn the core building block of LowMind — the Tensor and its gradient computation.
Linear regression with SGD and a custom training loop. Learn how to define parameters manually and update them with gradients.
XOR classification with Sequential model, Adam optimizer, and DataLoader. Demonstrates the full training workflow for classification.
Full pipeline: MicroMLP + high-level Trainer + EarlyStopping + ModelCheckpoint. The recommended pattern for most classification tasks.
MicroCNN for image classification. Covers Conv2d, BatchNorm, MaxPool, and the full CNN → flatten → Linear pipeline.
Benchmark SGD vs Adam vs RMSprop vs AdaGrad on the same task. Visualize convergence speed and final accuracy differences.
Build a custom attention layer, LayerNorm, and transformer block by subclassing lm.Module. Shows the full extensibility model.
Save/load weights with gzip compression, access state_dict, perform transfer learning. Essential for deployment on Pi.
Compare 6 LR scheduler strategies: StepLR, CosineAnnealing, ReduceLROnPlateau, Cyclic, MultiStep, LinearWarmup.
System monitoring, memory tracing, health scoring. Shows how to use LowMind's Pi-specific features for safe training on constrained hardware.
Sequence modeling with LSTM and GRU layers. Demonstrates multi-layer RNN setup, sequence batching, and no_grad context during inference.
Compare Xavier, Kaiming, and orthogonal initialization strategies. See how weight init affects convergence speed and final accuracy.
End-to-end production inference: load model, optimize memory, batch inference with no_grad, monitor system health. Perfect Pi deployment template.
How to Run
# Clone the repository first git clone https://github.com/dhaval-vedra/lowmind.git cd lowmind pip install -e . # Run any example python examples/01_basic_tensors.py python examples/04_mnist_like.py python examples/05_cnn_image.py python examples/10_raspberry_pi_monitor.py python examples/11_lstm_sequence.py
Complete Training Pipeline
Here's a full end-to-end example showing the recommended pattern for training and evaluating a model with LowMind.
import lowmind as lm import numpy as np # ── 1. Data Preparation ────────────────────────────── X = np.random.randn(2000, 20).astype(np.float32) y = (X[:, 0] + X[:, 1] > 0).astype(np.int64) X_train, X_val, y_train, y_val = lm.train_test_split( X, y, test_size=0.2, seed=42 ) train_loader = lm.DataLoader( lm.TensorDataset(X_train, y_train), batch_size=64, shuffle=True ) val_loader = lm.DataLoader( lm.TensorDataset(X_val, y_val), batch_size=64 ) # ── 2. Model Architecture ──────────────────────────── model = lm.Sequential( lm.Linear(20, 64), lm.BatchNorm1d(64), lm.ReLU(), lm.Dropout(0.3), lm.Linear(64, 32), lm.ReLU(), lm.Linear(32, 2), ) print(f"Parameters: {model.num_parameters():,}") # ── 3. Trainer Setup ───────────────────────────────── optimizer = lm.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4) scheduler = lm.CosineAnnealingLR(optimizer, T_max=50) trainer = lm.Trainer( model=model, optimizer=optimizer, loss_fn=lm.cross_entropy_loss, callbacks=[ lm.EarlyStopping(patience=15, mode='max'), lm.ModelCheckpoint('/tmp/best.lmz', monitor='val_acc', mode='max'), lm.LRSchedulerCallback(scheduler), lm.History(), ], clip_grad=1.0, verbose=5, ) # ── 4. Train ────────────────────────────────────────── history = trainer.fit(train_loader, val_loader, epochs=100) # ── 5. Evaluate & Inference ─────────────────────────── loss, acc = trainer.evaluate(val_loader) print(f"Val Accuracy: {acc:.4f}") # Inference with softmax probabilities probs = trainer.predict_proba(X_val) # (N, 2) probabilities preds = trainer.predict(X_val) # (N,) class indices # Metrics print(f"F1: {lm.f1_score(preds, y_val, num_classes=2):.4f}") print(lm.confusion_matrix(preds, y_val)) # ── 6. Save final model ─────────────────────────────── model.save('/tmp/final_model.lmz') print("✓ Model saved!")
Custom Module Example
# Build a custom self-attention layer class SelfAttention(lm.Module): def __init__(self, dim): super().__init__() self.q = lm.Linear(dim, dim) self.k = lm.Linear(dim, dim) self.v = lm.Linear(dim, dim) self.scale = dim ** -0.5 def forward(self, x): # x: (B, T, D) Q = self.q(x) K = self.k(x) V = self.v(x) # Scaled dot-product attention attn = (Q @ K.T * self.scale).softmax(axis=-1) return attn @ V # Use in a model attn = SelfAttention(64) print(f"Parameters: {attn.num_parameters():,}")
Production Inference on Pi
import lowmind as lm import numpy as np # Configure for Raspberry Pi lm.configure_memory(max_mb=192) # Check system health before inference monitor = lm.SystemMonitor() health = monitor.health_score() print(f"System health: {health}/100") if health < 40: print("Warning: system under stress, consider waiting") # Load model model = lm.MicroMLP(input_size=20, hidden_sizes=[32], output_size=2) model.load('/path/to/model.lmz') model.eval() # Optimize memory for inference lm.memory_manager.optimize_for_inference() # Batch inference with no_grad + memory tracing results = [] with lm.memory_trace("Batch Inference"): with lm.no_grad(): for batch in get_batches(data, size=32): out = model(lm.Tensor(batch)) results.append(out.numpy()) predictions = np.concatenate(results) print(f"Predicted {len(predictions)} samples")