Updated April 17, 2023
Introduction to PyTorch Conv2d
Two-dimensional convolution is applied over an input given by the user where the specific shape of the input is given in the form of size, length, width, channels, and hence the output must be in a convoluted manner is called PyTorch Conv2d. Conv2d is the function to do any changes in the convolution of two-dimensional data and it mainly pertains to an image in the system where we can apply regularizations too.
What is PyTorch Conv2d?
A convolution operation is performed on the 2D matrix provided in the system where any operations on a matrix such as matrix inversion or MAC operation is carried out with Conv2d function in the PyTorch module. This belongs to torch.nn package where all the neural networks functions are available thus managing the tensors and convolutions of matrices. An image is modified and made into two where the product of these two must help in reporting the value in the output.
How to Use Conv2d?
We are adding Conv2d to the layers of the neural network and in PyTorch, it is an instance of the nn module. These layers become the first layers in the network where parameters are very much necessary. A number of channels of the input data to be declared in the parameters along with the number of feature maps in the above layer. Instead of a number of channels, we can use the number of feature maps as input which has to be generated in the output. It is simple as we have to import all the required libraries along with the number of strides that must be given in the code.
The syntax must be like this.
a = Conv2d(in_channels, out_channels, kernel_size=(n, n), stride, padding, bias)
PyTorch conv2d – Parameters
The following parameters are used in PyTorch Conv2d. in_channels are used to describe how many channels are present in the input image whereas out_channels are used to describe the number of channels present after convolution happened in the system. The breadth and height of the filter is provided by the kernel. The field stride explains the stride of the convolution that happens in the system. The amount of implicit paddings is controlled by the field padding where a number of points are explained in each dimension.
If there is a bias happens in the result and if the user knows it beforehand, it is better to give in the code beforehand using the field bias. The output we receive from the Conv2d is always a tensor. Padding_mode is another field that explains how padding happens in the code and the default value happens to be zero. The dilation field explains the space between kernel elements and the default value is 1. Groups can be used if there are any values blocked from input to output and hence it does not appear in the output.
PyTorch Conv2d Example
The first step is to import the torch libraries into the system.
import torch
import torch. nn as nn
Conv2d instance must be created where the value and stride of the parameter have to be passed in the system. These values are then applied to the input generated data.
a = nn.Conv2d(4, 16, 6, stride=2)
input_data = torch.randn(15, 20, 48, 48)
output_value = a(input)
When we use square kernels, the code must be like this.
a = nn.Conv2d(2, 22, 2, stride=2)
when the kernels are not squared, the code should be similar to the following.
a = nn.Conv2d(2, 22, (2, 3), stride=(2, 1), padding=(4, 2))
a = nn.Conv2d(2, 22, (2, 3), stride=(2, 1), padding=(4, 2) , dilation=(3, 1))
We can build a neural network using Conv2d layer. First, we have to load the libraries in the system.
import torch
from torch. autograd import Variable
import torchvision. datasets as dsets
import torchvision. transforms as transforms
import torch.nn.init
The next step is to set the parameters.
Batch_lot = 16
Prob_k = 1
We are considering MNIST dataset for training and testing here.
mnist_train = dsets.MNIST(root='MNIST_data/',
train=True,
transform=transforms.ToTensor(),
download=True)
mnist_test = dsets.MNIST(root='MNIST_data/',
train=False,
transform=transforms.ToTensor(),
download=True)
data_loader = torch.utils.data.DataLoader(dataset=mnist_train,
batch_lot=batch_lot,
shuffle=True)
We are identifying the dataset.
print('The training dataset:\t',mnist_train)
print('\nThe testing dataset:\t',mnist_test)
Now we are about to create CNN model based on Conv2d layer.
Class neural(torch.nn.Module):
def __init__(self):
super(neural, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Dropout(p=1 - keep_prob))
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Dropout(p=1 - keep_prob))
self.layer3 = torch.nn.Sequential(
torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=1),
torch.nn.Dropout(p=1 - keep_prob))
torch.nn.init.xavier_uniform(self.fc1.weight)
self.layer4 = torch.nn.Sequential(
self.fc1,
torch.nn.ReLU(),
torch.nn.Dropout(p=1 - keep_prob))
self.fc2 = torch.nn.Linear(625, 10, bias=True)
torch.nn.init.xavier_uniform_(self.fc2.weight)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = self.fc2(out)
return out
model = neural()
model
we must set the learning rate, optimizer and loss function.
lrng_rate = 0.01
criterion = torch.nn.CrossEntropyLoss()
optimzr = torch.optim.Adam(params=model.parameters(), lr=lrng_rate)
We must train the model for any epochs.
print('Training the Deep Learning network ...')
train_cost = []
train_accu = []
training_epochs = 10
total_batch = len(mnist_train) // batch_size
print('Size of the training dataset is {}'.format(mnist_train.data.size()))
print('Size of the testing dataset'.format(mnist_test.data.size()))
print('Batch size is : {}'.format(batch_size))
print('Total number of batches is : {0:2.0f}'.format(total_batch))
print('\nTotal number of epochs is : {0:2.0f}'.format(training_epochs))
for epoch in range(training_epochs):
avg_cost = 0
for i, (batch_X, batch_Y) in enumerate(data_loader):
X = Variable(batch_X)
Y = Variable(batch_Y)
optimizer.zero_grad()
hypothesis = model(X)
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
prediction = hypothesis.data.max(dim=1)[1]
train_accu.append(((prediction.data == Y.data).float().mean()).item())
train_cost.append(cost.item())
if i % 200 == 0:
print("Epoch= {},\t batch = {},\t cost = {:2.4f},\t accuracy = {}".format(epoch+1, i, train_cost[-1], train_accu[-1]))
avg_cost += cost.data / total_batch
print("[Epoch: {:>4}], averaged cost = {:>.9}".format(epoch + 1, avg_cost.item()))
print('Learning Finished!')
we can visualize using matplotlib library.
from matplotlib import pylab as plt
import numpy as np
plt.figure(figsize=(20,10))
plt.subplot(121), plt.plot(np.arange(len(train_cost)), train_cost), plt.ylim([0,10])
plt.subplot(122), plt.plot(np.arange(len(train_accu)), 100 * torch.as_tensor(train_accu).numpy()), plt.ylim([0,100])
Conclusion
Convolution helps data scientists with its varied features where if we use it in data wrangling helps us to manage huge data with its dimension management and hence to get the results faster. The training set is treated well and hence overfitting can be avoided using Conv2d with the process of regularization.
Recommended Articles
We hope that this EDUCBA information on “PyTorch Conv2d” was beneficial to you. You can view EDUCBA’s recommended articles for more information.