0tokens

Apply for AI Grants India

Financial support for innovators building the future of AI in India.

Apply now

Chat · how to build ai applications with python and pytorch

How to Build AI Applications with Python and PyTorch

  1. aigi

    Python and PyTorch are a strong combination for building AI products: Python keeps experimentation fast, while PyTorch gives you control over data pipelines, model architecture, training, and deployment. The most useful workflow is not simply “train a neural network”. It is to define a measurable problem, build a reproducible baseline, evaluate it on realistic data, and then wrap the model in an application that people can use.

    For Indian builders, that may mean classifying documents, detecting defects in images, transcribing speech, or supporting AI apps for the next billion users in India. This guide uses handwritten-digit classification to explain the complete loop, then shows how to adapt the same structure to more serious projects.

    What PyTorch provides

    PyTorch is an open-source deep-learning framework built around tensors, automatic differentiation, neural-network modules, and GPU acceleration. Its imperative programming model makes it straightforward to inspect tensors, debug errors, and change a model while developing.

    The core pieces are:

    • Tensors: Multidimensional arrays that can run on CPUs or GPUs.
    • Autograd: Automatic calculation of gradients during backpropagation.
    • `torch.nn`: Layers, loss functions, and reusable model components.
    • Optimisers: Algorithms such as Adam and SGD that update model weights.
    • Data utilities: Dataset and DataLoader abstractions for batching and shuffling data.
    • TorchVision and related libraries: Datasets, transforms, pretrained vision models, and evaluation utilities.

    PyTorch is useful for both research and production, but a framework does not solve data quality, leakage, latency, privacy, or monitoring. Treat those as first-class engineering concerns from the start.

    Set up a reproducible environment

    Use Python 3.10 or newer where compatible with your project, and create an isolated environment rather than installing packages globally:

    python -m venv .venv
    source .venv/bin/activate        # macOS/Linux
    # .venv\\Scripts\\activate       # Windows
    python -m pip install --upgrade pip

    Install the PyTorch build recommended for your operating system and hardware from the official PyTorch installation selector. The correct command depends on CPU, CUDA, or ROCm support; copying an old CUDA command can produce incompatible packages.

    For a CPU-friendly MNIST project, install:

    pip install torch torchvision matplotlib scikit-learn

    Record dependencies in requirements.txt or a lockfile, use Git, and keep configuration outside the training script. A practical project layout is:

    ai-app/
      data/
      src/
        data.py
        model.py
        train.py
        evaluate.py
      tests/
      requirements.txt
      README.md

    Check your runtime before training:

    import torch
    
    print(torch.__version__)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(device)

    Build a complete baseline with MNIST

    MNIST is intentionally simple. Its purpose is to teach the application structure, not to demonstrate state-of-the-art computer vision. Start by loading separate training and test sets:

    import torch
    from torchvision import datasets, transforms
    from torch.utils.data import DataLoader
    
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.1307,), (0.3081,))
    ])
    
    train_data = datasets.MNIST("data", train=True, download=True, transform=transform)
    test_data = datasets.MNIST("data", train=False, download=True, transform=transform)
    
    train_loader = DataLoader(train_data, batch_size=128, shuffle=True, num_workers=2)
    test_loader = DataLoader(test_data, batch_size=256, shuffle=False, num_workers=2)

    Define a model whose output has one logit for each of the ten classes:

    import torch.nn as nn
    
    class DigitClassifier(nn.Module):
        def __init__(self):
            super().__init__()
            self.network = nn.Sequential(
                nn.Flatten(),
                nn.Linear(28 * 28, 128),
                nn.ReLU(),
                nn.Dropout(0.2),
                nn.Linear(128, 10)
            )
    
        def forward(self, x):
            return self.network(x)

    CrossEntropyLoss expects raw logits, so do not apply softmax inside the model during training. Train on the selected device and save the best checkpoint rather than only the final weights:

    import torch.optim as optim
    
    model = DigitClassifier().to(device)
    loss_fn = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=1e-3)
    
    for epoch in range(5):
        model.train()
        for images, labels in train_loader:
            images, labels = images.to(device), labels.to(device)
            optimizer.zero_grad(set_to_none=True)
            logits = model(images)
            loss = loss_fn(logits, labels)
            loss.backward()
            optimizer.step()
    
    torch.save({"model": model.state_dict()}, "digit_classifier.pt")

    For a production experiment, add a validation split, log loss and accuracy per epoch, seed random number generators, and preserve the dataset version and configuration alongside the checkpoint.

    Evaluate more carefully than accuracy

    Testing must happen on data the model never saw during training:

    model.eval()
    correct = total = 0
    
    with torch.no_grad():
        for images, labels in test_loader:
            images, labels = images.to(device), labels.to(device)
            predictions = model(images).argmax(dim=1)
            correct += (predictions == labels).sum().item()
            total += labels.size(0)
    
    print(f"Accuracy: {correct / total:.3f}")

    Accuracy alone can hide failures. For an imbalanced Indian-language, healthcare, finance, or public-service dataset, inspect precision, recall, F1 score, confusion matrices, calibration, and performance by language, region, device, or image quality. Keep a small “hard examples” set for regression testing whenever you change preprocessing or model weights.

    Turn a model into an application

    A trained checkpoint is not yet an AI application. Add an inference boundary that validates inputs, applies exactly the same preprocessing used during training, selects the device, and returns a predictable response. For a web service, FastAPI is a practical option; package the service with Docker and load the model once at startup rather than for every request.

    Design the interface around product needs:

    • Set file-size, token-length, and request-rate limits.
    • Return confidence carefully; a softmax score is not automatically calibrated certainty.
    • Log latency, errors, model version, and anonymised request metadata.
    • Keep sensitive data out of logs and define retention policies.
    • Add timeouts, fallback behaviour, and human review for high-impact decisions.

    If inference becomes a bottleneck, profile before optimising. Batch requests where latency permits, use mixed precision on supported hardware, and consider quantisation or export formats such as ONNX. For larger workloads, plan scaling backend infrastructure for AI applications around queues, autoscaling, caching, observability, and GPU availability.

    Move from tutorial to an India-ready project

    Replace MNIST with a narrow, measurable use case and collect representative data with permission. For Indic text, account for script variation, code-mixing, spelling differences, and limited labelled data; the low-resource Indic NLP builder’s guide covers those constraints in more depth. For voice or conversational products, measure background noise, accents, latency, and failure recovery rather than relying only on benchmark scores.

    Before launch, write down:

    • The user, decision, and failure cost.
    • The training, validation, and test split strategy.
    • Data licences, consent, retention, and deletion procedures.
    • Target latency, throughput, hardware, and monthly cost.
    • Quality thresholds and a rollback plan.
    • Monitoring owners and a schedule for refreshing the model.

    Common mistakes to avoid

    • Training and testing on overlapping data: This creates misleading scores.
    • Inconsistent preprocessing: Save transforms with the model configuration.
    • Ignoring class imbalance: Use suitable metrics, sampling, or class-weighted loss.
    • Hard-coding device assumptions: Support CPU fallback and test on deployment hardware.
    • Shipping notebooks as services: Separate data, model, evaluation, and serving code.
    • Skipping baseline models: Compare against heuristics, linear models, or pretrained networks.
    • Treating confidence as truth: Add abstention and human escalation paths.

    Final checklist

    A credible PyTorch application has a reproducible environment, versioned data, a documented baseline, held-out evaluation, saved checkpoints, an inference API, monitoring, and a clear response to failures. Start with the smallest useful model, prove value on representative Indian data, and scale only after profiling the real bottleneck.

    Last updated 23 September 2026

AIGI may be inaccurate. Replies seeded from the guide above.