What is Pytorch
It’s an open source library based on Python
The three components of Pythorch
Figure A.1 PyTorch’s three main components include a tensor library as a fundamental building block for computing, automatic differentiation for model optimization, and deep learning utility functions, making it easier to implement and train deep neural network models.
- Tensor Library: extends the concept of the array-oriented programming library NumPy
- Automatic differentiation engine: that enables the automatic computation of gradients for tensor operations, simplifying backpropagation and model optimization
- Deep learning library: It offers modular, flexible, and efficient building blocks, including pretrained models, loss functions, and optimizers, for designing and training a wide range of deep learning models
Definding deep learning
We define a few terms for more clarity
AI: a computer program with the capability to perform human tasks like understanding natural language, pattern recognition, make decisions.
Machine Learning: a subfield of AI, Machine learning is to enable computers to learn from data and make predictions or decisions without being explicitly programmed to perform the task
Deep Learning: is a subcategory of machine learning that focuses on the training and application of deep neural networks. The “deep” in deep learning refers to the multiple hidden layers of artificial neurons or nodes that allow them to model complex, nonlinear relationships in the data. Unlike traditional machine learning techniques that excel at simple pattern recognition, deep learning is particularly good at handling unstructured data like images, audio, or text, so it is particularly well suited for LLMs.
Understanding Tensors
Tensors represent a mathematical concept that generalizes vectors and matrices to potentially higher dimensions. Tensors can be characterized by order (or rank) that provides the number of dimensions.
From a computational perspective, tensors serve as data containers. For instance, they hold multidimensional data, where each dimension represents a different feature.
Scalars, vectors, matrices and tensors
Tensors Data Types
PyTorch adopts the default 64-bit integer data type from Python. If we create tensors from Python floats, PyTorch creates tensors with a 32-bit precision.
A 32-bit floating-point number offers sufficient precision for most deep learning tasks while consuming less memory and computational resources than a 64-bit floatingpoint number. Moreover, GPU architectures are optimized for 32-bit computations, and using this data type can significantly speed up model training and inference.
Implementing multilayer neural networks
Setting up efficient data loaders
We create a dataset and the test-data with 5 training examples, each with two features. Then, we create a tensor with the labels.
Now that we’ve defined a PyTorch Dataset class we can use for our toy dataset, we can use PyTorch’s DataLoader class to sample from it.
- dataset: this provides the data set
- shuffle: if the data is mixed
- batch_size: numbers of samples
- num_workers: specifies how many parallel processes should load the data