The primary components of a neural network in PyTorch are the following:
1. **Tensor:**
A tensor is the fundamental data structure in PyTorch that is used to store data. It is a multi-dimensional array that can hold any data type in PyTorch. Tensors are the inputs and outputs of neural networks.
2. **Module:**
In PyTorch, a module is the base class for all the neural network modules, and it provides a convenient way of organizing the parameters that are involved in the training of a neural network. A PyTorch module has three important methods: ‘forward‘, ‘backward‘, and ‘zero_grad‘. The ‘forward‘ method is used to compute the output of the network for a given input, the ‘backward‘ method is used to compute the gradients of the loss with respect to the module’s parameters, and the ‘zero_grad‘ method is used to reset the gradients to zero.
3. **Loss Function:**
The loss function computes the difference between the predicted output of the network and the ground truth. In PyTorch, various loss functions are available, such as mean squared error (MSE), cross-entropy loss, and binary cross-entropy loss.
4. **Optimizer:**
The optimizer updates the parameters of the neural network based on the gradients computed by the loss function during the backward pass. In PyTorch, various optimizers are available, such as stochastic gradient descent (SGD), Adam, and Adagrad.
Here’s an example:
import torch.nn as nn
import torch.optim as optim
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 20)
self.fc2 = nn.Linear(20, 1)
def forward(self, x):
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
net = Net()
criterion = nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9)
In the example above, ‘Net‘ is a neural network with two fully connected layers, ‘fc1‘ and ‘fc2‘. ‘criterion‘ is an instance of the mean squared error (MSE) loss function, and ‘optimizer‘ is an instance of SGD optimizer with a learning rate of 0.1 and a momentum of 0.9.