PyTorch in a lot of ways behaves like the arrays we love from Numpy. These Numpy arrays, after all, are just tensors. PyTorch takes these tensors and makes it simple to move them to GPUs for the faster processing needed when training neural networks. It also provides a module that automatically calculates gradients (for backpropagation) and another module specifically for building neural networks. All together, PyTorch ends up being more flexible with Python and the Numpy stack compared to TensorFlow and other frameworks.
Deep Learning is based on artificial neural networks which have been around in some form since the late 1950s. The networks are built from individual parts approximating neurons, typically called units or simply “neurons.” Each unit has some number of weighted inputs. These weighted inputs are summed together (a linear combination) then passed through an activation function to get the unit’s output.
Below is an example of a simple neural net.
It turns out neural network computations are just a bunch of linear algebra operations on tensors, which are a generalization of matrices. A vector is a 1-dimensional tensor, a matrix is a 2-dimensional tensor, an array with three indices is a 3-dimensional tensor. The fundamental data structure for neural networks are tensors and PyTorch is built around tensors.
It’s time to explore how we can use PyTorch to build a simple neural network.
Define an activation function(sigmoid) to compute the linear output
features = torch.randn((1, 5))creates a tensor with shape (1, 5), one row and five columns, that contains values randomly distributed according to the normal distribution with a mean of zero and standard deviation of one.
weights = torch.randn_like(features)creates another tensor with the same shape as features, again containing values from a normal distribution.
bias = torch.randn((1, 1))creates a single value from a normal distribution.
Now we calculate the output of the network using matrix multiplication.
That’s how we can calculate the output for a single neuron. The real power of this algorithm happens when you start stacking these individual units into layers and stacks of layers, into a network of neurons. The output of one layer of neurons becomes the input for the next layer. With multiple input units and output units, we now need to express the weights as a matrix.
We define the structure of neural network and initialize the weights and biases.
Now we can calculate the output for this multi-layer network using the weights W1 & W2, and the biases, B1 & B2.
- Introduction to Multi-Task Learning(MTL) for Deep Learning
- Introduction to Deep Learning
- Deep Learning | Introduction to Long Short Term Memory
- Difference Between Artificial Intelligence vs Machine Learning vs Deep Learning
- Artificial intelligence vs Machine Learning vs Deep Learning
- Deep Q-Learning
- Differential Privacy and Deep Learning
- Implementing Deep Q-Learning using Tensorflow
- ML - List of Deep Learning Layers
- ML - Saving a Deep Learning model in Keras
- ML | Natural Language Processing using Deep Learning
- Prediction of Wine type using Deep Learning
- Human Activity Recognition - Using Deep Learning Model
- Avengers Endgame and Deep learning | Image Caption Generation using the Avengers EndGames Characters
- An introduction to Machine Learning
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.