What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can get started with PyTorch on a CPU—no GPU is required to learn the basics. Follow these five steps to install a suitable build, understand tensors, load data, create a small model, and train and save its weights. PyTorch’s official beginner workflow uses FashionMNIST, a dataset of clothing images labeled across ten categories, and can run in a hosted Colab notebook or on your computer.

1. Choose an environment and install PyTorch

Choose CPU or accelerator support based on the computer you will use. For learning the basic workflow, CPU is sufficient. If you want GPU acceleration, your system needs compatible hardware and software: PyTorch offers builds for NVIDIA CUDA and AMD ROCm where supported.

Because available builds and supported environments can change, use the official PyTorch installation selector. Select your operating system, package manager, Python version, and compute platform, then run the command it provides. Don’t copy an old command without checking that it matches your system.

Choose where to run the tutorial

  • Hosted notebook: The official beginner guide offers Colab notebooks, which avoid installing PyTorch locally.
  • Local computer: Install PyTorch and TorchVision using the selector, then run the tutorial code in your Python environment.

The official “Learn the Basics” guide describes both options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the installation

In Python, create and print a tensor to confirm that PyTorch imports and runs:

import torch

x = torch.rand(2, 3)
print(x)

To check whether PyTorch can access a CUDA device, run:

print(torch.cuda.is_available())

A result of False means CUDA is not available to this installation; it does not prevent you from learning or running a CPU workflow. This check is for CUDA and does not establish whether an AMD ROCm setup is available.

2. Learn the tensor basics

A tensor is PyTorch’s core data structure: it holds the values that enter a model, pass through its calculations, and come out as predictions. Model parameters are tensors too. If you know NumPy, the array-like shape and indexing will feel familiar. PyTorch tensors can also run on supported accelerators and participate in automatic differentiation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, a grayscale image can be represented by a tensor of pixel values. A batch of images adds a dimension for the number of images; knowing those dimensions helps you check that data matches the model’s expected input.

The official tensor tutorial introduces tensor creation, attributes, operations, and movement between devices.

3. Load data with Dataset and DataLoader

PyTorch separates the definition of a dataset from the process of iterating through it:

  • Dataset represents samples and their labels.
  • DataLoader wraps a dataset so your training loop can retrieve samples in batches.

The official beginner workflow uses FashionMNIST, a dataset of clothing images and labels for ten clothing categories. It provides a consistent example for learning how data flows into a model; it is not a performance benchmark.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Follow the official data tutorial to load the dataset and create a data loader. Inspect a batch and its dimensions before building the model: that makes it easier to match the input layer to the data.

4. Build a small neural network

The torch.nn namespace provides layers and other building blocks for neural networks. A model is assembled from modules, with each layer transforming its input. The official model tutorial builds a classifier for the FashionMNIST example.

For the ten-category classification task, the model must ultimately produce ten scores per image—one for each class. Check that the output’s final dimension is ten and that the model’s input shape is compatible with the batches from the data loader. The tutorial walks through defining the model and inspecting its layers and output.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

5. Train the model and save its weights

Training connects the model’s predictions to parameter updates. For each batch, the model makes a forward prediction, a loss function measures the difference between predictions and labels, and automatic differentiation calculates gradients from the operations in that forward pass. The optimizer uses those gradients to update the model parameters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Clear old gradients: Reset the optimizer’s gradients before processing the next batch.
  2. Run the forward pass: Pass the batch through the model to get predictions.
  3. Calculate loss: Compare the predictions with the correct labels.
  4. Backpropagate: Call loss.backward() to calculate gradients.
  5. Update parameters: Call the optimizer’s step method to apply the update.

The optimization tutorial explains the training loop, loss, and optimizer. The autograd tutorial explains how PyTorch tracks operations and computes gradients.

Save and reload a state_dict

A state_dict stores a model’s learned parameters. Save it after training:

torch.save(model.state_dict(), "model_weights.pth")

To use those weights later, recreate the same model architecture first, then load the saved state and switch to evaluation mode before making predictions:

model = YourModelClass()
state_dict = torch.load("model_weights.pth", weights_only=True)
model.load_state_dict(state_dict)
model.eval()

Replace YourModelClass with the class that defines your model. Saving a state_dict does not save that architecture, so the model definition must be available when you reload the weights. The official save-and-load tutorial covers the full workflow.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.