FastAI Best Practices
Master the learning rate finder, mixed precision training, FastAI's callback system, model export, and deployment to production.
The Learning Rate Finder
The learning rate is the most important hyperparameter. FastAI's lr_find() systematically tests learning rates to help you find the optimal value:
# Run the learning rate finder learn.lr_find() # This plots loss vs learning rate # Pick a learning rate where: # - Loss is still decreasing (not yet diverging) # - Typically 1 order of magnitude before the minimum # Use the suggested learning rate learn.fit_one_cycle(5, 3e-3) # For fine-tuning, use discriminative learning rates learn.fine_tune(5, base_lr=1e-3, freeze_epochs=2)
One-Cycle Training
FastAI's fit_one_cycle() implements the one-cycle policy: the learning rate warms up then decays, achieving faster convergence and better generalization than constant learning rates:
# One-cycle: LR goes up then down, momentum goes down then up learn.fit_one_cycle( n_epoch=10, lr_max=3e-3, div=25.0, # Start LR = lr_max/25 div_final=1e5, # End LR = lr_max/1e5 pct_start=0.3, # 30% warmup, 70% decay )
Mixed Precision Training
# Enable mixed precision (fp16) - one line! learn = vision_learner(dls, resnet50, metrics=accuracy).to_fp16() # Train as usual - 2x faster with half the memory learn.fine_tune(5)
Callbacks
FastAI's callback system lets you hook into every event in the training loop:
# Built-in callbacks learn.fit_one_cycle(5, cbs=[ EarlyStoppingCallback(monitor='valid_loss', patience=3), SaveModelCallback(monitor='accuracy', fname='best_model'), CSVLogger(), # Log metrics to CSV ]) # Custom callback class PrintLossCallback(Callback): def after_batch(self): if self.iter % 100 == 0: print(f"Batch {self.iter}, Loss: {self.loss:.4f}")
Model Export and Deployment
# Export the model (includes preprocessing pipeline) learn.export('model.pkl') # Load and use in production learn_inf = load_learner('model.pkl') pred, idx, probs = learn_inf.predict('path/to/image.jpg') # Deploy as a simple web app with Gradio import gradio as gr def classify_image(img): pred, idx, probs = learn_inf.predict(img) return dict(zip(learn_inf.dls.vocab, map(float, probs))) iface = gr.Interface(fn=classify_image, inputs=gr.Image(), outputs=gr.Label()) iface.launch()
Quick Reference
| Technique | Code | Impact |
|---|---|---|
| LR Finder | learn.lr_find() | Find optimal learning rate |
| Mixed Precision | learn.to_fp16() | 2x faster, half memory |
| Progressive Resizing | Start small, increase image size | Faster training, better results |
| Test Time Augmentation | learn.tta() | Better predictions at inference |
| Discriminative LR | learn.fit(lr=slice(1e-5, 1e-3)) | Different LR per layer group |
| Gradient Accumulation | GradientAccumulation(n_acc=4) | Simulate larger batch sizes |
Course Complete!
You now know how to use FastAI for vision, tabular, and NLP tasks. Continue exploring with Hugging Face Transformers for cutting-edge NLP, or dive deeper into PyTorch to understand what FastAI does under the hood.
Next Course: HF Transformers →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX