PyTorch: The “Torch” You Need to Light Up Your Machine Learning Career

M Adnan A
16 min readOct 13, 2024

--

PyTorch Fundamentals

PyTorch is an open-source deep learning framework created by Facebook’s AI Research lab. It is widely used for building deep learning models because of its dynamic computational graph, flexibility, and Pythonic nature, making it intuitive.

Tensors: The Muscle of PyTorch

Imagine tensors as the “buff gym-goers” of PyTorch. They lift all the heavy data, whether customer purchase histories, airplane seat bookings, or cat memes (who doesn’t love those?).

Tensors are like fancy, souped-up versions of NumPy arrays. They’re multi-dimensional arrays that can store anything from simple numbers to complex matrices, and they come with superpowers — like doing all the math on GPUs for faster calculations. 💪

Example (E-commerce): You’re managing an online store and have the following weekly purchase data for customers:

import torch

# Customer purchase data (2 customers over 3 days)
customer_data = torch.tensor([[20, 35, 50], [40, 60, 75]])
print(customer_data)
  • Tensors: Like your warehouse that stores all the products (or memes, or flight data). They’re flexible and work great with GPUs, doing the heavy lifting faster than that one friend who always lifts the lightest box when moving.
  • Dynamic Computation Graph: Your neural network’s ability to handle spontaneous decisions. It doesn’t matter if the input data size changes mid-flight booking; PyTorch rolls with it.

Let’s say you’re running a travel app. Each user searches for flights in wildly different ways — some type slowly, others hit random keys like a toddler on a keyboard. PyTorch adapts and updates its graph dynamically based on the length and structure of input data.

Think of it as being flexible enough to handle both the meticulous planner and the last-minute, “I’m booking a flight in 3 hours” customer. ✈️

  • Autograd: Like your accountant, remembering every tiny detail and calculating how much profit you made (or lost) based on customer behavior — tracking gradients so you can update your neural network efficiently.

Real-world scenario (Finance): Suppose you’re training a model to predict stock prices based on a wild mix of historical data, market sentiment, and rumors about Elon Musk’s latest tweets.

Autograd is your helpful assistant that calculates the gradients (how much to tweak your model parameters) to ensure your predictions get better with time — like fine-tuning a guessing game for which stock Elon will mention next. 😜

  • Neural Networks (torch.nn): A build-a-bot workshop where you construct models that make decisions, like predicting customer purchases, flight bookings, or which stock is going to the moon. 🚀

Example (E-commerce): Let’s say you’re predicting how likely a customer is to buy a product based on their browsing habits. You stack layers in a model to process the inputs (like their clicks on 15 different kinds of socks — seriously, who needs that many?)

import torch.nn as nn

# A simple neural network with 2 layers
model = nn.Sequential(
nn.Linear(3, 5), # 3 input features, 5 hidden units
nn.ReLU(), # Activation function
nn.Linear(5, 1) # 5 hidden units, 1 output (purchase prediction)
)

print(model)

Lets Build a Bear (Model)

Let’s break down the PyTorch workflow step by step, covering all the key elements you mentioned. We’ll keep it practical with some real-world flavor.

PyTorch Workflow Overview

  1. Prepare the Dataset
  2. Build the Model
  3. Define the Loss Function & Optimizer
  4. Train the Model
  5. Make Predictions
  6. Evaluate the Model

1. Creating a Dataset with a Regression Formula

We’ll simulate a dataset where we predict, say, house prices based on some features like square footage and number of rooms.


import torch

# Number of samples (houses)
n_samples = 100

# Generate random data for features
square_footage = torch.randn(n_samples, 1) * 1000 + 2000 # House sizes
rooms = torch.randn(n_samples, 1) * 2 + 3 # Number of rooms

# Generate house prices based on the formula: price = 150 * square_footage + 5000 * rooms + noise
prices = 150 * square_footage + 5000 * rooms + torch.randn(n_samples, 1) * 10000 # Adding noise for realism

# Dataset as a tuple of (features, target)
dataset = torch.cat([square_footage, rooms], dim=1), prices
print(dataset)

Here, we’ve created two features (square_footage and rooms) and simulated the house prices (prices) with some random noise. You can think of this as building your very own real estate prediction model.

2. First PyTorch Model for Linear Regression

Now, let’s build a simple linear regression model using PyTorch. The model will try to predict house prices based on square footage and the number of rooms.

Important Classes:

  • nn.Module: The base class for building models.
  • nn.Linear: A fully connected layer (in this case, for linear regression).
import torch.nn as nn

class LinearRegressionModel(nn.Module):
def __init__(self):
super(LinearRegressionModel, self).__init__()
# We have 2 inputs (square footage, rooms) and 1 output (price)
self.linear = nn.Linear(2, 1)

def forward(self, x):
return self.linear(x)

# Initialize model
model = LinearRegressionModel()

print(model)

In this model, we use nn.Linear(2, 1) because we have 2 input features (square footage and rooms) and 1 output (price). It’s a basic setup, but it forms the foundation of regression tasks.

3. Most Important PyTorch Model Building Classes

While building models in PyTorch, some of the key classes you’ll frequently use are:

  • nn.Module: The parent class for all neural networks in PyTorch. This is where you define the layers and how they interact.
  • nn.Linear: Fully connected layer. It computes the weighted sum of inputs, i.e., the linear part of a neural network.
  • torch.optim: Contains optimization algorithms (like gradient descent) to adjust the model’s weights and biases.
  • nn.MSELoss: For regression tasks, this is the loss function we use to calculate the mean squared error between predictions and actual values.

4. Making Predictions with Inference Mode

Once the model is trained, we can use it to make predictions. For this, we switch to inference mode. This tells PyTorch that we don’t need gradients, so it speeds up computation and saves memory.

# Create some test data
test_square_footage = torch.tensor([[2500, 4], [1500, 3]], dtype=torch.float32)

# Inference mode (no gradients)
with torch.no_grad():
predictions = model(test_square_footage)
print("Predicted prices: ", predictions)

5. Writing Code for a PyTorch Training Loop

This is the part where PyTorch shines! Training involves multiple steps repeated in a loop until the model performs well.

Steps:

  1. Forward pass: Compute predictions.
  2. Loss calculation: Compare predictions with actual targets.
  3. Backward pass: Compute gradients.
  4. Update weights: Adjust weights using the optimizer.
import torch.optim as optim

# Define the loss function and the optimizer
criterion = nn.MSELoss() # Mean Squared Error for regression
optimizer = optim.SGD(model.parameters(), lr=0.01) # Stochastic Gradient Descent

# Training loop
n_epochs = 1000 # Number of iterations
for epoch in range(n_epochs):
model.train() # Set model to training mode

# Forward pass: Compute predicted prices
predictions = model(dataset[0])

# Compute the loss (how bad is our prediction?)
loss = criterion(predictions, dataset[1])

# Backward pass: compute the gradients
optimizer.zero_grad() # Zero the gradients (important!)
loss.backward() # Backpropagation

# Update the weights
optimizer.step()

# Print loss every 100 epochs
if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {loss.item()}")

6. Evaluate the Model

Once training is done, it’s time to evaluate the model’s performance on unseen data.

model.eval()  # Set model to evaluation mode
with torch.no_grad():
predictions = model(test_square_footage)
print("Test Predictions: ", predictions)

Switching to eval() mode disables features like dropout and batch normalization, which are only needed during training.

Real-World Summary (Keeping It Light):

  • Dataset: You’re simulating house prices to create a mini real estate empire. 🏠
  • Linear Regression Model: Your first baby PyTorch model that predicts prices based on features (because you’re tired of online estimates!).
  • Training Loop: Like running on a treadmill — you’re repeatedly making predictions and adjusting based on errors, hoping you get fitter (or that your model does).
  • Inference: Finally, it’s like showing off your fitness gains (model’s accuracy) after weeks of hard work.

What is Computer Vision?

Imagine you’re building an e-commerce platform. You want the system to recognize images of products (clothes, shoes, electronics) or even identify damaged items in a warehouse. That’s where computer vision comes into play.

Computer vision is all about teaching machines to see and interpret images, videos, and the world around them. You can use it to:

  • Recognize objects in image (e.g., identify shoes in product photos).
  • Detect objects (e.g., find all instances of a product in a warehouse).
  • Segment images (e.g., highlight damaged areas in a product).

In PyTorch, we can easily build models that process images, thanks to Convolutional Neural Networks (CNNs).

Why CNNs?

CNNs are the superstars of computer vision. Traditional neural networks treat images like flat arrays of numbers, but CNNs treat them as 2D grids (like they are). They’re good at extracting spatial features such as edges, textures, and shapes from images.

1. Image as Input

When working with images, we treat them as 3D tensors: [Channels, Height, Width].

  • Channels: RGB has 3 channels (Red, Green, Blue).
  • Height and Width: Dimensions of the image.

For example, an image that is 32x32 pixels with 3 color channels would be represented as a tensor of shape [3, 32, 32].

# Example of loading an image (using torchvision)
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Transformation: resize to 32x32 and convert to tensor
transform = transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor()])

# Load dataset (CIFAR-10 for example)
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)

# Get a sample batch of images
images, labels = next(iter(train_loader))
print(images.shape) # [Batch_size, Channels, Height, Width]

This code snippet loads the CIFAR-10 dataset (a collection of small images) and transforms them into tensors that PyTorch can work with.

2. Convolutional Layers

A convolutional layer is the core building block of a CNN. It slides a small window (called a filter or kernel) across the image to extract features like edges or textures. This is like how our eyes focus on small parts of an image to understand the bigger picture.

Key Techniques in Computer Vision with PyTorch:

  • Data Augmentation: To prevent overfitting, augment your dataset by flipping, rotating, or changing the brightness of images.
  • Dropout: Randomly disable neurons during training to prevent overfitting.
  • Batch Normalization: Normalize activations to improve training speed and stability.

Real-world Application Fun!

Imagine you’re using CNNs to:

  • Automatically categorize e-commerce product images into categories (shoes, shirts, electronics).
  • Detect product defects in a warehouse using object detection models.
  • Create a travel app that suggests destinations based on images of the places the user likes.

Let’s build a CNN classification model on the CIFAR-10 dataset (a popular dataset for image classification). I’ll provide the full source code that you can easily run in a Jupyter notebook. The dataset can be directly downloaded from PyTorch’s torchvision library, so you don't have to worry about sourcing it separately.

Full Source Code: CNN for CIFAR-10 Image Classification

# Import necessary libraries
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

# Step 1: Set up CIFAR-10 Dataset and DataLoader

# Data transformations for CIFAR-10 (normalization and augmentations)
transform = transforms.Compose([
transforms.RandomHorizontalFlip(), # Randomly flip the image horizontally
transforms.RandomCrop(32, padding=4), # Randomly crop with padding
transforms.ToTensor(), # Convert image to tensor
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize to [-1, 1]
])

# Download CIFAR-10 dataset for training and testing
train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

# DataLoader for batching the data
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

# Step 2: Define CNN Model Architecture

class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
# Convolutional layers
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 8 * 8, 512) # Fully connected layer
self.fc2 = nn.Linear(512, 10) # Output layer (10 classes for CIFAR-10)

def forward(self, x):
# Forward pass through convolutional and pooling layers
x = self.pool(torch.relu(self.conv1(x)))
x = self.pool(torch.relu(self.conv2(x)))
x = x.view(-1, 64 * 8 * 8) # Flatten the output for fully connected layers
x = torch.relu(self.fc1(x))
x = self.fc2(x) # No activation function for the final layer (handled by loss function)
return x

# Step 3: Initialize Model, Loss Function, and Optimizer

model = SimpleCNN()

# Define loss function (cross entropy for multi-class classification)
criterion = nn.CrossEntropyLoss()

# Use stochastic gradient descent (SGD) optimizer
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Step 4: Training the CNN Model

n_epochs = 10 # You can change this to a higher value for better accuracy

for epoch in range(n_epochs):
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data

# Zero the parameter gradients
optimizer.zero_grad()

# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimize
loss.backward()
optimizer.step()

running_loss += loss.item()

if i % 100 == 99: # Print every 100 mini-batches
print(f"[Epoch {epoch + 1}, Batch {i + 1}] loss: {running_loss / 100:.3f}")
running_loss = 0.0

print("Training Finished!")

# Step 5: Evaluating the Model

correct = 0
total = 0
model.eval() # Set the model to evaluation mode

with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1) # Get the predicted class
total += labels.size(0)
correct += (predicted == labels).sum().item()

print(f"Accuracy of the network on the 10,000 test images: {100 * correct / total:.2f}%")

# Step 6: Save the Trained Model (Optional)

torch.save(model.state_dict(), 'cnn_cifar10.pth')
print("Model saved!")

Key Steps in the Code:

  1. Dataset & DataLoader: We use torchvision.datasets.CIFAR10 to download and load the CIFAR-10 dataset. We also apply random augmentations (like horizontal flips and random cropping) to improve the model's generalization.
  2. CNN Model: We define a simple CNN with two convolutional layers, two pooling layers, and two fully connected layers. The final output layer has 10 nodes (for 10 CIFAR-10 categories).
  3. Training Loop: We train the model for 10 epochs (you can change it to more for better results). The optimizer updates the model parameters using Stochastic Gradient Descent (SGD), and we compute the loss with CrossEntropyLoss.
  4. Evaluation: After training, we evaluate the model on the test dataset and calculate its accuracy.
  5. Model Saving: At the end of training, the model can be saved to disk using torch.save(). This way, you can reload and reuse the model later without retraining it.

Running the Code

You can copy and paste this code into your Jupyter notebook. The CIFAR-10 dataset will be downloaded automatically when you run the code. Training may take a while depending on your machine (GPU will speed it up a lot).

Once the model is trained, you can try experimenting with different architectures, more layers, or fine-tuning hyperparameters like learning rate and optimizer to see how they affect the performance!

Inference on a Single Custom Image

If you have your own image and want to test the model on that, you can load it, apply the same transformations, and pass it through the model for classification:

from PIL import Image

# Load and preprocess your custom image
def preprocess_image(image_path):
img = Image.open(image_path) # Load image
transform = transforms.Compose([
transforms.Resize((32, 32)), # Resize the image to 32x32 (CIFAR-10 input size)
transforms.ToTensor(), # Convert to tensor
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize as done during training
])
img = transform(img) # Apply transformations
img = img.unsqueeze(0) # Add a batch dimension (model expects batches)
return img

# Use your own image path here.. change name below accordingly
image_path = 'kutta_bili_jahaz_gari.jpg'
img = preprocess_image(image_path)

# Set the model to evaluation mode and make a prediction
model.eval()
with torch.no_grad(): # Disable gradient calculation for inference
output = model(img)
_, predicted = torch.max(output.data, 1) # Get the predicted class

# Print the predicted class
print(f'Predicted class: {train_dataset.classes[predicted.item()]}')

Summary of How to Test the Model:

  1. Run the accuracy check on the test set: This gives you a general idea of how the model performs across all test data.
  2. Test on random images from the test set: You can display images and see the ground truth and predicted labels side by side.
  3. Test on a custom image: Use your own image, apply the required preprocessing, and make predictions.

Transfer Learning with PyTorch

Transfer learning involves taking a pre-trained model (a model trained on a large dataset) and using it as a starting point for a task where you have less data or less computational power to train a model from scratch. It’s super useful when training a model from scratch would be overkill.

Key Concepts:

  • Pre-trained Models: Models trained on large datasets like ImageNet.
  • Transfer Learning: Using these pre-trained models on new tasks, adapting them to your specific dataset.
  • Fine-Tuning: Adjusting some or all of the model’s pre-trained weights slightly by continuing training on your own dataset.

How Transfer Learning Differs from Fine-Tuning:

  • Transfer Learning: You usually freeze the early layers of the pre-trained model and only retrain the last layer(s) to adapt to your specific task.
  • Fine-Tuning: You allow some or all layers to be retrained with your dataset, which means the pre-trained weights are “fine-tuned” to better fit your new data.

How to Use Transfer Learning in PyTorch

Let’s say you want to classify images in a custom dataset but don’t have millions of images to train from scratch. You can use a model like ResNet-50 (a model pre-trained on ImageNet, a dataset with 1.2 million images and 1,000 categories) and adapt it for your specific classification problem.

Steps for Transfer Learning:

  1. Download a Pre-Trained Model: PyTorch provides a library of pre-trained models in torchvision.models, which are trained on large datasets like ImageNet.
  2. Freeze the Pre-Trained Layers: During transfer learning, you freeze (disable training) for most layers and only update the final classification layer for your specific task.
  3. Train the Final Layer: Replace the final fully connected layer with a new one that has the correct number of outputs for your custom dataset.

Code Example: Transfer Learning with ResNet on CIFAR-10

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torchvision import models

# Step 1: Load CIFAR-10 Dataset
transform = transforms.Compose([
transforms.Resize(224), # Resizing to 224x224 for ResNet input size
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize to [-1, 1]
])

train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)

# Step 2: Load Pre-Trained ResNet Model
model = models.resnet50(pretrained=True)

# Step 3: Freeze All Layers Except Final Fully Connected Layer
for param in model.parameters():
param.requires_grad = False # Freeze parameters (i.e., don't backpropagate through them)

# Replace the final fully connected layer (original has 1,000 output classes for ImageNet)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10) # 10 output classes for CIFAR-10

# Step 4: Define Loss Function and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.fc.parameters(), lr=0.001, momentum=0.9)

# Step 5: Train the Model (only the final layer is updated)
n_epochs = 5
model.train()

for epoch in range(n_epochs):
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data

optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()

if i % 100 == 99:
print(f"[Epoch {epoch + 1}, Batch {i + 1}] loss: {running_loss / 100:.3f}")
running_loss = 0.0

print("Finished Training!")

# Step 6: Evaluate the Model
correct = 0
total = 0
model.eval()

with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print(f"Accuracy on test images: {100 * correct / total:.2f}%")

Breakdown of the Code:

  1. Load CIFAR-10 Dataset: We resize CIFAR-10 images to 224x224 to match the input size of ResNet and normalize them.
  2. Load Pre-Trained Model: We load a pre-trained ResNet-50 model using torchvision.models. Since it’s pre-trained on ImageNet, it already knows about many generic features (edges, textures, etc.).
  3. Freeze Layers: We freeze all the model parameters using param.requires_grad = False. This means we won't update any of the pre-trained layers.
  4. Modify the Final Layer: We replace the final fully connected layer of ResNet-50 with a new one tailored for CIFAR-10, which has 10 classes.
  5. Train the Model: Only the last fully connected layer is trained, and the rest of the model remains unchanged.
  6. Evaluate the Model: We test the model’s accuracy on the CIFAR-10 test set.

How to Get Pre-Trained Models

PyTorch has an easy-to-use torchvision.models library that provides many pre-trained models like:

  • ResNet
  • VGG
  • DenseNet
  • AlexNet
  • MobileNet
  • EfficientNet

Transfer Learning vs Fine-Tuning

  • Transfer Learning: You freeze the backbone (all layers except the final one) and retrain just the last layer. This is good when your new task is similar to the original one (e.g., classifying new types of animals).
  • Fine-Tuning: You unfreeze some or all layers and retrain the model. This is useful when your dataset is somewhat different from the pre-trained dataset, and you want the model to adjust to your specific task.

When to Use Transfer Learning?

  • Small Dataset: When you don’t have enough data to train a deep network from scratch.
  • Limited Resources: When you don’t have enough computational power to train large models.
  • New Task but Similar Domain: Your task shares similarities with the original task the model was pre-trained on.

Final Words on Transfer Learning

Transfer learning is a powerful way to leverage existing knowledge (pre-trained models) and apply it to new tasks. You get the benefits of huge, well-trained models without the need for huge datasets or massive computing resources!

Experiment Tracking in Machine Learning

Experiment tracking is a process used to record various aspects of your machine learning experiments to keep track of different versions of models, datasets, hyperparameters, and results. This becomes especially important when you’re iterating on models, comparing results, and need to reproduce outcomes. Think of it as the diary of your AI journey — a way to log everything that went right (or wrong) so that you can easily retrace your steps or share findings with others.

Why Do You Need Experiment Tracking?

Imagine you’re working on a deep learning project, trying out multiple versions of a model:

  • You tweak hyperparameters like learning rate, batch size, or optimizer.
  • You modify the architecture, add or remove layers, change the activation functions.
  • You use different datasets, split data differently, or apply various preprocessing steps.
  • You train the model on multiple machines or use distributed computing.

Without tracking, it’s easy to lose track of what you’ve tried, which settings led to the best results, or even which dataset version you used!

PyTorch Model Deployment: The Essentials

When you’ve trained a great machine learning model, the next challenge is deployment — getting that model into the hands of users or systems where it can actually be useful. Let’s break it down.

1. Where Is My Model Going to Go?

This depends on your application and the audience for your model. Broadly, you have a few options:

  • Cloud Services: Deploying your model to cloud services (AWS, Google Cloud, Azure) is common for scalable applications. They can handle large workloads and allow you to serve predictions via APIs.
  • Edge Devices: For real-time applications or IoT (e.g., phones, cameras), you can deploy models on devices where predictions need to be made locally. Optimizing for resource constraints (memory, CPU) is key here.
  • Web Apps: Using platforms like Hugging Face Spaces or streamlit, you can create web applications to let users interact with your model (e.g., image classifiers, NLP models).
  • Local Deployment: For development or small-scale use, you might deploy locally on a server or personal machine.

2. How Is My Model Going to Function?

Once deployed, your model needs a way to interact with the world. Typically, it functions by:

  • Receiving Input: The user or system sends input (e.g., images, text, data points) to the model.
  • Making Predictions: The model processes the input, performs inference, and returns the predicted output.
  • Returning Results: The model sends the prediction back to the user/system.

If you deploy a PyTorch model as an API, the workflow might look like this:

  • The model is wrapped in an API (e.g., using Flask, FastAPI).
  • Input is sent to an endpoint, where it’s preprocessed and passed to the model.
  • The model runs inference and sends back the result.

Tools and Platforms to Deploy Machine Learning Models

Here are some of the common tools and platforms to deploy your model:

  • Hugging Face Spaces: An easy-to-use platform to deploy your PyTorch models as web apps with Gradio or Streamlit interfaces. Great for sharing models with others interactively.
  • AWS SageMaker: Amazon’s fully managed service for deploying machine learning models. It handles all infrastructure, including scaling, load balancing, and monitoring.
  • Google Cloud AI Platform: Provides a managed environment for deploying machine learning models built with various frameworks, including PyTorch.
  • Azure Machine Learning: Microsoft’s service for deploying machine learning models on the cloud, integrated with tools like MLflow and containers.
  • Streamlit: If you want to deploy interactive machine learning models in web apps quickly, Streamlit is a great Python tool. You can deploy models locally or on platforms like Streamlit Sharing.
  • TorchServe: PyTorch’s model serving library that helps deploy models on a scalable server environment. It can handle batch inference, multi-model serving, and integrates with cloud services like AWS.

--

--

M Adnan A

Love writing, learning, sharing and love being sarcastic :)