Intro to PyTorch 01 PDF
Document Details
Uploaded by BlissfulMood
Université de Batna 1
Tags
Summary
This document provides a basic introduction to the deep learning framework PyTorch. It covers topics such as tensors, data types, functions, and automatic differentiation.
Full Transcript
Intelligence Artificielle Avancée Outline Introduction How PyTorch Works? Data types Functions Differentiation in Autograd PyTorch with GPU’s Neural Networks Learning PyTorch with Examples Introduction PyTorch is an open source machine learning framework t...
Intelligence Artificielle Avancée Outline Introduction How PyTorch Works? Data types Functions Differentiation in Autograd PyTorch with GPU’s Neural Networks Learning PyTorch with Examples Introduction PyTorch is an open source machine learning framework that accelerates the path from research to production Developed primarily by Facebook AI and introduced in 2016 Introduction The framework combines the efficient GPU-accelerated backend libraries from Torch with Python frontend Focuses on rapid prototyping, readable code, and support for the variety of deep learning models It allows deep learning models to be expressed in the Python programming language Important Properties of PyTorch Python support PyTorch is based on Python, it can be used with popular libraries and packages such as NumPy, SciPy, Numba and Cython Offers developers an easy-to-learn, simple-to-code structure that's based on Python Enables easy debugging with popular Python tools TorchScript Production environment of PyTorch that enables users to transition between modes TorchScript optimizes functionality, speed, ease of use and flexibility Important Properties of PyTorch Offers scalability and is well-supported on major cloud platforms It supports CPU, GPU, and parallel processing, as well as distributed training The PyTorch Hub is a repository of pre-trained models that can be invoked, in some cases with just a single line of code It has a large collection of tools and libraries in areas ranging from computer vision to reinforcement learning Installing How to Download For installation, first, you have to choose your preference and then run the install command From pytorch.org/get-started/locally you can install by following instructions Installing Visual Studio Code (VSCode) Jupyter Notebook environment. pip install torch Introduction to PyTorch How PyTorch Works? - Tensors Fundamentally, it’s a library for programming with tensors Tensors are the fundamental building blocks of neural networks in PyTorch Tensors are a specialized data structure that are very similar to arrays and matrices Which are basically just multidimensional arrays! In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters Tensors are similar to NumPy’s ndarrays, except that tensors can run on GPUs Tensors Tensors Tensors can be initialized in various ways The simplest way to create a tensor is with the torch.empty() call: Created tensor x is 2-dimensional, with 3 rows and 4 columns By default, PyTorch tensors are 32-bit floating numbers torch.empty() allocates memory for the tensor, but does not initialize it with any values Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Introduction to PyTorch Tensors More often we’ll want to initialize our tensor with some value Common cases are: All zeros All ones Random values torch module provides methods for all these! A tensor full of zeros A tensor full of ones A tensor with random values between 0 and 1 Tensors While initializing tensors, such as a model’s learning weights, random values are common But we need reproducibility of our results Manually setting your random number generator (torch.manual_seed()) is the way to do this Tensors Most of the time, when we are performing operations on more than one tensor, we need to have them of the same shape Having the same number of dimensions, same number of cells in each dimension To initialize a tensor with the same shape as another tensor, we are using torch.*_like() methods: torch.empty_like(another_tensor) torch.zeros_like(another_tensor) torch.ones_like(another_tensor) torch.rand_like(another_tensor) Tensors We can also specify the data of the tensor directly from a Pytorch collection: torch.tensor() is the most straightforward way to create a tensor if we already have data torch.tensor() creates a copy of the data Most of the time, our data starts out in NumPy arrays or pandas DataFrames We have to convert these data types to tensors using torch.tensor() torch.tensor() method takes two arguments: numerical data (NumPy array, Python list, or Python numeric variable) and desired data type (the dtype parameter) Tensors We can also get the same tensor in our specified data type using methods such as float(), long() etc. We can also use tensor.FloatTensor, tensor.LongTensor, tensor.Tensor classes to instantiate a tensor of particular type Or using.to() Available data types include: torch.bool torch.int8 torch.uint16 torch.float torch.double Tensors torch.arange(end): Returns a 1-D tensor with elements ranging from 0 to end-1 We can use the optional start and step parameters to create tensors with different ranges Attributes of a Tensor Shape Datatype Float32, Float64, Integer, Boolean Device GPU/CPU Properties of Tensor We can get the size of a particular dimension with the size() method x.size(0) get’s the size of 0th dimension Change the shape of a tensor with the view() method x_view = x.view(3, 2) (x_view shares the same memory as x, so changing one changes the other) We can also use torch.reshape() method for a similar purpose x_reshaped = torch.reshape(x, (2, 3)) Properties of Tensor We can use torch.unsqueeze(x, dim) function to add a dimension of size 1 to the provided dim We can also use the corresponding use torch.squeeze(x), which removes the dimensions of size 1 If we want to get the total number of elements in a tensor, we can use the numel() method