Getting Started with PyTorch for Deep Learning

Introduction

PyTorch is a popular open-source machine learning framework developed by Facebook AI Research. It’s widely adopted in research and production for developing deep learning models due to its ease of use, flexibility, and powerful GPU support.

Why PyTorch?

  • Dynamic Computation Graph: Build and modify neural networks on-the-fly.
  • Pythonic: Intuitive syntax similar to NumPy.
  • GPU Acceleration: Supports CUDA for faster training.
  • Research and Production Ready: Extensively used in both academia and industry.
  • Rich Ecosystem: Includes TorchVision, TorchText, TorchAudio, and more.

Installing PyTorch

To install PyTorch using pip:

pip install torch torchvision torchaudio

Using conda:

conda install pytorch torchvision torchaudio -c pytorch

Refer to the official guide for platform-specific instructions: pytorch.org/get-started

PyTorch vs TensorFlow

FeaturePyTorchTensorFlow
Computation GraphDynamic (eager)Static (default)
SyntaxPythonicGraph-oriented
DebuggingIntuitive (Python tools)More complex
Learning CurveBeginner friendlyModerate

Your First PyTorch Program

import torch

# Create tensors
a = torch.tensor([2.0, 3.0])
b = torch.tensor([4.0, 5.0])

# Add tensors
c = a + b
print("Sum:", c)

# Check GPU availability and move tensors
if torch.cuda.is_available():
    a = a.to('cuda')
    b = b.to('cuda')
    print("Tensors moved to GPU")

Creating and Reshaping Tensors

import torch

# Create a 2D tensor
matrix = torch.tensor([[1, 2], [3, 4]])

# Reshape into 1D
flattened = matrix.view(-1)

# Reshape into 2x2 again
reshaped = flattened.view(2, 2)

print("Original Matrix:", matrix)
print("Flattened:", flattened)
print("Reshaped:", reshaped)
  • matrix is a 2x2 tensor initialized with integers.
  • We use .view(-1) to flatten the tensor into a 1D array.
  • The .view(2, 2) operation reshapes the flat tensor back to its original 2x2 form.

Building a Simple Neural Network

import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(2, 2)
    
    def forward(self, x):
        x = torch.relu(self.fc1(x))
        return x

model = Net()
input_tensor = torch.tensor([[1.0, 2.0]])
output = model(input_tensor)
print(output)
  • A simple neural network class Net is defined with one fully connected layer using nn.Linear.
  • We apply ReLU activation to introduce non-linearity.
  • An input tensor of shape (1, 2) is passed through the network, producing an output tensor.

Using Loss Functions and Optimizers

import torch.optim as optim

criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

target = torch.tensor([[0.0, 1.0]])

loss = criterion(output, target)
print("Loss:", loss.item())

loss.backward()
optimizer.step()
  • nn.MSELoss() is used to compute the mean squared error between prediction and target.
  • SGD optimizer is created with learning rate 0.01.
  • After computing loss, loss.backward() calculates gradients.
  • optimizer.step() updates weights using the gradients.

Training Loop Example

for epoch in range(5):
    optimizer.zero_grad()
    output = model(input_tensor)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
    print("Epoch", epoch, "Loss:", loss.item())
Epoch 0 Loss: 0.8562
Epoch 1 Loss: 0.7037
Epoch 2 Loss: 0.5891
Epoch 3 Loss: 0.5035
Epoch 4 Loss: 0.4397
  • The training loop runs for 5 epochs, simulating multiple updates to the model.
  • In each iteration, gradients are zeroed, loss is calculated, and weights are updated.
  • Loss is printed for each epoch to monitor progress.

Use Case: Predicting Binary Output

# Sample binary classification
class BinaryClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer = nn.Linear(2, 1)

    def forward(self, x):
        return torch.sigmoid(self.layer(x))

model = BinaryClassifier()
data = torch.tensor([[0.5, 0.8]])
prediction = model(data)
print("Predicted value:", prediction.item())
  • This binary classifier uses a single-layer neural network with sigmoid activation.
  • The output ranges between 0 and 1, ideal for binary classification tasks (e.g., yes/no, spam/ham).
  • We pass one sample input and print the model’s prediction value.
🚢 Titanic Dataset – A Classic for AI Beginners

The Titanic dataset is one of the most popular introductory datasets for learning classification tasks in AI and machine learning. Using features like age, gender, and class, the goal is to predict whether a passenger survived. It's widely used with PyTorch to demonstrate model training, evaluation, and improvement techniques.

Run the PyTorch Code at Colab ( or download from Github )


🌸 Iris Dataset – The Go-To for Classification Practice

The Iris dataset is a classic in machine learning, featuring measurements of three types of iris flowers. It’s ideal for beginners learning how to train and test classification models with PyTorch, thanks to its simplicity and well-separated classes.

Run the Iris Classifier at Colab ( or download from Github )

Conclusion

PyTorch offers a great starting point for learning deep learning due to its simplicity and flexibility. With strong community support and rich libraries, it’s a go-to tool for modern AI development.


Next: Working with Tensors in PyTorch »


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
    ©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer