0% found this document useful (0 votes)
4 views23 pages

Introduction to PyTorch Framework

The document provides an introduction to PyTorch, an open-source Python framework developed by Meta for machine learning, highlighting its capabilities such as tensor manipulation, automatic gradient calculation, and model creation. It covers essential operations, model and loss function definitions, data management, and training loops using PyTorch. Additionally, it includes code examples for implementing various functionalities, including data loading and mini-batch training.

Uploaded by

Tarik Toudert
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Introduction to PyTorch Framework

The document provides an introduction to PyTorch, an open-source Python framework developed by Meta for machine learning, highlighting its capabilities such as tensor manipulation, automatic gradient calculation, and model creation. It covers essential operations, model and loss function definitions, data management, and training loops using PyTorch. Additionally, it includes code examples for implementing various functionalities, including data loading and mini-batch training.

Uploaded by

Tarik Toudert
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to PyTorch

Chuan Xu, MCF


Université Côte d’Azur, Lucioles,

October 21, 2025

Chuan Xu, MCF Master 1 October 21, 2025 1 / 19


Introduction to PyTorch
An open-source Python framework
developed by Meta for machine learning.

Chuan Xu, MCF Master 1 October 21, 2025 2 / 19


Introduction to PyTorch
An open-source Python framework
developed by Meta for machine learning.
Replaces Numpy to harness the power of GPUs and other
accelerators (multiple machines).
Tensor: a generalization of a vector or matrix to an arbitrary
number of dimensions.

Chuan Xu, MCF Master 1 October 21, 2025 2 / 19


Introduction to PyTorch
An open-source Python framework
developed by Meta for machine learning.
Replaces Numpy to harness the power of GPUs and other
accelerators (multiple machines).
Tensor: a generalization of a vector or matrix to an arbitrary
number of dimensions.
Automatically calculate gradients and apply optimization
algorithms easily.
Easy debugging, coding flexibility, and parallelism.
[Link]
1thbROHkU6LuY5v3T-7jtYS3L94Sq3J1K?usp=sharing
Chuan Xu, MCF Master 1 October 21, 2025 2 / 19
Tensor in PyTorch
import torch
import numpy as np

# Conversion of numpy to PyTorch tensor


data = [Link](4).reshape(2,2)
x_data = [Link](data, dtype=torch.float64)
x_data_1 = [Link](4).reshape(2,2)
x_data.dtype # torch.float64
x_data.shape # [Link]([2,2])
x_data.device # device(type='cuda', index=0)
x_data.to(torch.float32) # Change the tensor type

# Check if GPU is available


if [Link].is_available(): x_data = x_data.to("cuda")

# Conversion of PyTorch tensor to numpy


np_array = x_data.numpy()

# Conversion of PyTorch tensor to list


liste = x_data.tolist()

Chuan Xu, MCF Master 1 October 21, 2025 3 / 19


Operations
Similar to numpy, element-wise.
# Create PyTorch matrix
shape = (3,3)
rand_tensor = [Link](shape)
ones_tensor = [Link](shape)
zeros_tensor = [Link](shape)

r = ones_tensor * ones_tensor
v = ones_tensor @ ones_tensor

[Link](ones_tensor, axis = 1) # Sum for each row


somme = [Link](ones_tensor) # Total sum, returns a tensor
[Link]() # Convert to a Python float value

Chuan Xu, MCF Master 1 October 21, 2025 4 / 19


Gradient Calculation
[Link] provides automatic gradient
calculation for a function with respect to its parameters.
# Define a random tensor
x = [Link](2, requires_grad=True)
# When a tensor is created with requires_grad=True,
# each operation performed on it is tracked

# The function with parameters x


out = [Link](2).sum()

# Step to calculate the gradients of 'out' with respect to x


[Link]()

# The gradient is stored in [Link];


print([Link])

# In-place update of x while keeping requires_grad=True


with torch.no_grad():
x -= [Link]

Chuan Xu, MCF Master 1 October 21, 2025 5 / 19


Model and Loss Creation
[Link] provides choices of models and loss functions.

Chuan Xu, MCF Master 1 October 21, 2025 6 / 19


Model and Loss Creation
[Link] provides choices of models and loss functions.

# The "Sequential" module is a "container"


# that defines a feed-forward network
model = [Link](
[Link](n, 1), # Linear function z = Wx
[Link]() # Sigmoid function
)

# X is the dataset
prediction = model(X)

fn_perte = [Link]() # Define log loss function


perte = fn_perte(prediction, label)

Chuan Xu, MCF Master 1 October 21, 2025 6 / 19


Model Class

import [Link] as nn
class LinearModel([Link]):
def __init__(self, input_dim, output_dim, bias=True):
super(LinearModel, self).__init__()
self.input_dimension = input_dim
self.num_classes = output_dim
[Link] = [Link](self.input_dimension, self.num_classes, bias=bias)

def forward(self, x):


return [Link](x)

model = LinearModel(40,10)

Chuan Xu, MCF Master 1 October 21, 2025 7 / 19


Convolutional Layers in PyTorch

# Convolution layer
# [Link].Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0)
[Link].Conv2d(1, 64, 5, stride=2)

# Max-pooling layer
# [Link].MaxPool2d(window_size, stride=sliding_window_step)
[Link].MaxPool2d(2)

# [Link] flattens a tensor


[Link]()

Chuan Xu, MCF Master 1 October 21, 2025 8 / 19


Defining the Optimizer

[Link] provides popular algorithms.


# Gradient descent algorithm
optimizer = [Link]([Link](),
lr=learning_rate)

# Other advanced algorithms


optimizer = [Link]([Link](), lr=0.0001)
optimizer = [Link]([Link](), lr=0.0001)
optimizer = [Link]([Link](), lr=0.0001)

Chuan Xu, MCF Master 1 October 21, 2025 9 / 19


Training Loop
Logistic regression using gradient descent.

X = [Link]((10,3))
label = [Link](0,2,(10,1)).to(torch.float32)
model = [Link](
[Link](3, 1), # Linear function
[Link]() # Sigmoid function
)
fn_perte = [Link]()
optimizer = [Link]([Link](), lr=0.1)

for t in range(100):
optimizer.zero_grad() # Reset gradients
prediction = model(X) # Compute predictions
perte = fn_perte(prediction, label) # Compute loss
[Link]() # Compute gradient
[Link]() # Update model parameters
print(f"Iteration {t}: Loss is {[Link]():.2f}")

Chuan Xu, MCF Master 1 October 21, 2025 10 / 19


Data gestion
[Link] allows you to write your data class,
making it easier to use mini-batches (multi-processing)
First, let’s write the init function that initializes this class.
We store important information there.

import torch
class Donnees([Link]):
# Caractérise un jeu de données pour PyTorch'
def __init__(self, nom_instances, nom_attributs):
# Initialisation
self.X = [Link]((nom_instances, nom_attributs))
[Link] = [Link](0,2,(nom_instances,1)).to(torch.float32)

donnee_train = Donnes(10,3)
donnee_test = Donnes(5,3)

Chuan Xu, MCF Master 1 October 21, 2025 11 / 19


Data gestion [CSV]
[Link] allows you to write your data class,
making it easier to use mini-batches (multi-processing)
First, let’s write the init function that initializes this
class. We store important information there.

import torch
import pandas as pd
class Donnees([Link]):
# Caractérise un jeu de données pour PyTorch'
def __init__(self, nom_fichier):
# Initialisation
data_frame = pd.read_csv(nom_fichier)
self.X = [Link](data_frame.iloc[:,0:8].values,
dtype=torch.float32)
[Link] = [Link](data_frame.iloc[:,8].values,
dtype=torch.float32)

donnee_train = Donnees("path/nom_fichier_train")
donnee_test = Donnees("path/nom_fichier_test")
Chuan Xu, MCF Master 1 October 21, 2025 12 / 19
Data gestion
Next, let’s write the len function, which represents the
size of the data.

import torch
class Donnees([Link]):
# Caractérise un jeu de données pour PyTorch'
def __init__(self, nom_instances, nom_attributs):
# Initialisation
self.X = [Link]((nom_instances, nom_attributs))
[Link] = [Link](0,2,(nom_instances,1)).to(torch.float32)

def __len__(self):
# Représente le nombre total d'exemples du jeu de données'
return len([Link])

Chuan Xu, MCF Master 1 October 21, 2025 13 / 19


Générer les données
After that, let’s write the getitem function, which returns
the data associated with the index.

import torch
class Donnees([Link]):
def __init__(self, nom_instances, nom_attributs):
self.X = [Link]((nom_instances, nom_attributs))
[Link] = [Link](0,2,(nom_instances,1)).to(torch.float32)

def __len__(self):
# Représente le nombre total d'exemples du jeu de données'
return len([Link])

def __getitem__(self, indice):


# Sélection de l'exemple
return self.X[indice], [Link][indice]

donnee_train = Donnees(10,3)
x, l = donnee_train[0]
print(len(donnee_train))
Chuan Xu, MCF Master 1 October 21, 2025 14 / 19
Data gestion : Images
import torch
import os
from skimage import io

class Donnees([Link]):
def __init__(self, liste_images_noms, liste_label, chemin):
[Link] = liste_images_noms
[Link] = liste_label
[Link] = chemin

def __len__(self):
return len([Link])

def __getitem__(self, indice):


# Sélection de l'exemple
f_nom = [Link][indice]
f = [Link]([Link], f_nom)
image = [Link]([Link](f), dtype=torch.float32)

return image, [Link][indice]

Chuan Xu, MCF Master 1 October 21, 2025 15 / 19


Data already integrated in PyTorch
[Link] provide standard dataset (of type
[Link]) that you can utilize directement:
[Link]

import torch
import torchvision
from [Link] import ToTensor

training_data = [Link](root="data", train=True,


download=True,
transform=ToTensor(),
)

Chuan Xu, MCF Master 1 October 21, 2025 16 / 19


Efficient Data Access
The [Link] class provides access to the underlying data and
efficiently utilizes multi-core processors.

Chuan Xu, MCF Master 1 October 21, 2025 17 / 19


Efficient Data Access
The [Link] class provides access to the underlying data and
efficiently utilizes multi-core processors.
batch size denotes the number of instances contained in each batch. Default is 1.
shuffle True: shuffle indices at each epoch. Default is False.
num workers represents the number of threads generating batches of data in parallel.
Default is 0.
drop last True: drop the last batch if it’s incomplete (if the dataset size is not
divisible by the batch size). Default is False.

donnee_train = Donnees(10,3)
donnee_loader = [Link](donnee_train,
batch_size = 3,
shuffle = True,
num_workers = 2,
drop_last = False)

for xs, ls in donnee_loader:


print([Link], [Link])

Chuan Xu, MCF Master 1 October 21, 2025 17 / 19


Mini-batch training loop
Logistic regression
import torch
class Donnees([Link]):
def __init__(self, nom_instances, nom_attributs):
self.X = [Link]((nom_instances, nom_attributs))
[Link] = [Link](0,2,(nom_instances,1)).to(torch.float32)
def __len__(self):
return len([Link])
def __getitem__(self, indice):
return self.X[indice], [Link][indice]
donnee_train = Donnees(10,3)
donnee_loader = [Link](donnee_train, batch_size = 3, shuffle = True, num_workers = 2)
model = [Link]([Link](3, 1),[Link]())
fn_perte = [Link]()
optimizer = [Link]([Link](), lr=0.1)

for e in range(100):
for xs, ls in donnee_loader:
optimizer.zero_grad() #Réinitialiser les gradients
prediction = model(xs) #Caculer les prédictions pour le jeu de données
perte = fn_perte(prediction, ls) #Caculer le perte
[Link]() #Caculer le gradient
[Link]() #Mettre à jours les paramètres de modèle
print(f"Epoque {e}: Le perte est {[Link]():.2f}")

Chuan Xu, MCF Master 1 October 21, 2025 18 / 19


TP

[Link]
exercice/-/tree/main?ref_type=heads

Chuan Xu, MCF Master 1 October 21, 2025 19 / 19

You might also like