This article demystifies PyTorch, a machine learning library for Python.

Machine Learning



PyTorch is an open source deep learning framework developed by Meta AI (formerly Facebook AI), now part of the Linux Foundation. This library was built on the Torch library, but is no longer in development as of this writing. Technology blogger Obyte provides a visual guide to PyTorch, from basic components to neural network implementation.

PyTorch overview | 0 bytes

https://0byte.io/articles/pytorch_introduction.html

Visual introduction to PyTorch – YouTube

The most basic building block of PyTorch is a data type called a tensor. These are special containers for storing numbers used in machine learning, and are like multidimensional arrays that are more powerful than regular lists or arrays.

Various functions can be used to initialize a tensor, including torch.rand() to generate a random number between 0 and 1, torch.randn() to get a normally distributed random number centered around 0, and torch.ones() to set all elements to 1. Additionally, you can use torch.zeros() to pad zeros, torch.eye() to create an identity matrix, and torch.empty() to allocate memory. Some applications do not initialize the value.

To calculate with PyTorch, all real-world data must be converted into a numerical representation. For text data, the simplest method is to assign a unique ID to each word and convert it to a number; for image data, it is converted to a tensor, a grid of pixels that holds RGB color information. For example, a 28×28 pixel grayscale image becomes a tensor of the following shape: [28, 28]3D mesh data is managed in a shape like . [1000, 3] Holds the x/y/z coordinates of vertices.

PyTorch comes with over 100 predefined operations that make it easy to perform basic arithmetic operations as well as aggregation operations such as sum() to calculate a sum and means() to calculate an average. It also provides a wide range of activation functions that play an important role in neural networks. For example, the most common ReLU function replaces negative values ​​with 0, Sigmoid normalizes values ​​between 0 and 1, and Tanh normalizes values ​​between -1 and 1.

The core feature that supports PyTorch’s computational power is the autograd automatic differentiation engine, also known as the neural network engine. Differentiation is a fundamental calculation used in all aspects of science and engineering that describes how a function changes with respect to a particular variable.

For example, the slope of the line tangent to the parabola f(x) = is found by differentiating f(x). The slope f'(x) of this straight line can be said to represent the instantaneous rate of change of f(x) corresponding to x.

A gradient is a set of derivatives with respect to all variables, indicating the slope in all directions at once. Visualizing modern networks with millions of parameters is difficult, but autograd allows you to quickly automate these complex differential calculations with the help of GPUs.

Algorithms such as gradient descent are used to train the model. Even modern networks with millions of parameters can be trained quickly without manual calculations using autograd and GPU assistance.

In actual model construction, define a class that inherits from nn.Module to describe the network structure. Taking London property price prediction as an example, a model is built that takes 87 features as input, processes them in two hidden layers, and outputs the final price.

The learning loop repeats the following steps hundreds to thousands of times. Forward propagation. Involves passing data to the model to obtain predictions. Loss calculations that measure the error in predictions. Backpropagation using autograd. Updating weights using optimization algorithms such as Adam.

When this model was trained for 100 epochs, the mean absolute error (MAE) on unknown data was 330,000 pounds (approximately 66 million yen). The mean absolute percentage error (MAPE) was 18.6%, and Obyte reported that in 37.0% of cases, the predicted value was within 10.0% of the error.

A key feature of PyTorch is the ability to perform the entire process from data preparation to training and evaluation in a single, integrated way. However, Obyte says, “The final accuracy depends more on the quality of the appropriate features (data) than on the structure of the model.” This is the reality of machine learning. ”





Source link