Examples (full code)

Calling public APIs is easy, but you pay for it with high costs, slow responses, limited control, and no real ownership - real control comes from building your own models. When you need faster, cheaper, domain-specific AI, training your own model gives you the edge.

These guides show you how to go from calling APIs to building your own custom models - fast, cleanly, and without needing to become a PyTorch or infra expert. Lightning turns PyTorch into a low-code experience with easy scaling via the Lightning platform or your self-hosted hardware.

Why train your own models

Public LLMs are powerful generalists, but they aren't always the best fit for specific tasks like protein folding, financial forecasting, document classification, recommendation systems, or any domain where precision, speed, cost, or privacy really matter. When you need domain expertise, faster responses, lower costs, or full control over your AI system, training your own model gives you an edge.

Why full code matters

While low-code tools are great for quick prototypes, full code gives you the flexibility, control, and scalability needed for real-world AI. With full code, you can customize models, optimize training for speed and cost, scale across any hardware, and adapt to new research without limits. Lightning makes full-code development just as simple as low-code - but without sacrificing power when you need to go deeper.

Why PyTorch?

PyTorch is the most popular framework for building and training custom AI models. It gives you full flexibility to design, modify, and experiment - whether you're training small models for mobile apps or giant foundation models.

But PyTorch by itself is low-level. You have to manually manage device placement (CPU, GPU, TPU), optimizer steps, loss computation, logging, checkpointing, and scaling - all while keeping your code clean and modular. This can quickly become fragile and intimidating, especially if you're not already a deep AI expert.

Better with PyTorch Lightning

Lightning makes PyTorch approachable, scalable, and production-ready.

  • You keep full control of your PyTorch code.
  • Lightning organizes your model, data, and training into clean, reusable components.
  • It automates the messy infrastructure: multi-GPU training, mixed precision, checkpointing, logging, and more.

You focus on building great models - Lightning handles the rest.

PyTorch Lightning hello world

Here's a hello world example using PyTorch Lightning.

1/6
2/6
3/6
4/6
5/6
6/6
import torch
import lightning as L

class ToyExample(L.LightningModule):
    def __init__(self, model):
        super().__init__()
        self.model = model

    def training_step(self, batch):
        loss = self.model(batch).sum()
        return loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.model.parameters())

if __name__ == "__main__":
    model = torch.nn.Linear(32, 2)
    pl_module = ToyExample(model)
    train_dataloader = torch.utils.data.DataLoader(torch.randn(8, 32))

    trainer = L.Trainer()
    trainer.fit(pl_module, train_dataloader)

When training a model is needed

While public LLMs are impressive generalists, they aren't optimized for every task. Training your own models unlocks possibilities that off-the-shelf LLMs can't handle well:

  • High-accuracy classification: Build compact models that outperform LLMs on tasks like document triage, customer intent detection, or specialized anomaly detection.
  • Real-time systems: Deploy lightweight models that respond in milliseconds — ideal for robotics, mobile apps, finance, and edge AI.
  • Recommendation engines: Train custom ranking models that adapt to your users faster and cheaper than fine-tuning massive language models.
  • Private or sensitive data: Keep all training and inference in-house for medical, legal, or financial applications where using external APIs isn't acceptable.
  • Scientific modeling: Predict protein structures, material properties, weather patterns, or other physical phenomena - tasks where LLMs aren't even relevant.
  • Cost-optimized production systems: Run efficient models that cost 10–100x less than hosting LLMs, especially when inference volume scales.

When precision, speed, cost, privacy, or domain expertise really matter, training your own models gives you an undeniable advantage.

What about RAG?

Use RAG when the model is good enough and you just need to feed it better information. It's ideal for adding knowledge to a public model without retraining, quickly updating documents or data, and handling language-centric tasks like question answering or summarization.

However, RAG doesn't solve everything. It can't fix precision issues - models can still hallucinate or misunderstand. It doesn't improve latency - LLMs remain slower and more expensive than small custom models. It also doesn't guarantee privacy unless fully self-hosted, and it can't handle non-language tasks like classification, recommendations, or anomaly detection.

When you need better reasoning, faster performance, lower costs, stronger privacy, or task specialization, training your own model is the better path.

Was this page helpful?

What went wrong?

↑↓ Navigate↵ Open⌘↵ New tabesc Close