PyTorch nn Conv2d [With 12 Examples]

In this Python tutorial, we will learn about PyTorch nn Conv2d in Python. The conv2d is defined as a convolution operation that is performed on the 2d matrix which is provided in the system.

And additionally, we will also cover different examples related to PyTorch nn Conv2d. And we will cover these topics.

  • PyTorch nn conv2d
  • PyTorch nn conv2d example
  • PyTorch nn functional conv2d
  • PyTorch nn conv2d padding same
  • PyTorch nn conv2d group
  • PyTorch nn conv2d dilation
  • PyTorch nn conv2d in CNN
  • PyTorch nn conv2d bias
  • PyTorch nn conv2d transpose
  • Pytorch nn conv2d parameters
  • PyTorch nn conv2d weight
  • PyTorch nn conv2d input channels and output channels

PyTorch nn conv2d

In this section, we will learn about the PyTorch nn conv2d in python.

The PyTorch nn conv2d is defined as a Two-dimensional convolution that is applied over an input that is specified by the user and the particular shape of the input is given in the form of channels, length, and width, and output is in the form of convoluted manner.

Syntax:

The syntax of PyTorch nn conv2d is:

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)

Parameters:

The following are the parameters of PyTorch nn conv2d:

  • in_channels is used as several channels in the input image.
  • out_channels is used as the number of channels produced by the convolution.
  • kernel_size is used to define the size of the convolving kernel.
  • stride is used to control the stride for the cross-correlation, a single number, or a tuple. the default value of the stride is 1.
  • padding is used to control the amount of padding applied to the input. It can be either string or a tuple of giving the amount of implicit padding. The default value of padding is 0.
  • dilation is used to control the spacing between the kernel elements and the default value of the dilation is 1.
  • groups are used to control the connection between the inputs and outputs. The default value of groups is 1.
  • bias: The default value of the bias is True. If it is true it adds a learnable bias to the output. If it is false it does not add any learnable bias to the output.

So. with this we understood about the PyTorch nn conv2d.

Read: PyTorch Hyperparameter Tuning

PyTorch nn conv2d example

In this section, we will learn how to implement the PyTorch nn conv2d with the help of an example in python.

The PyTorch nn conv2d applies a 2d convolution over an input signal composed of several input planes.

It is an easy arithmetic operation inside this we skid a matrix or kernel of weights above two-dimensional data and perform the element-wise multiplication of data.

Code:

In the following code, we will import all the necessary libraries such as import torch, import torch.nn as nn.

  • n = nn.Conv2d(18, 35, 5, stride=2) is used with square kernels and equal stride.
  • input = torch.randn(22, 18, 52, 102) is used to describe the variable by using torch.random() function.
  • output = n(input) is used to declare the output variable.
  • print(output) is used to print the output by using the print() function.
# Importing Libraries
import torch
import torch. nn as nn

# With square kernels and equal stride
n = nn.Conv2d(18, 35, 5, stride=2)

# Describe the variable
input = torch.randn(22, 18, 52, 102)

# Declare the output variable
output = n(input)
# Print output
print(output)

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn conv2d with square kernel and square stride value is printed on the screen.

PyTorch nn conv2d example
PyTorch nn conv2d example

This is how the implementation of the PyTorch nn conv2d is done.

Read: PyTorch Leaky ReLU

PyTorch nn functional conv2d

In this section, we will learn about the PyTorch nn functional conv2d in python.

The PyTorch nn functional conv2d applies a 2D convolution over an input image collected from several input planes.

Syntax:

The syntax of PyTorch nn functional conv2d:

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

Parameter:

The following are the parameter of functional conv2d

  • input: Input is defined as an input tensor of shape(minibatch, in_channels).
  • weight: Weight is defined as a filter of shape(out_channels)
  • bias: Bias is defined as an optional bias tensor of shape(out_channels). The default value of bias is None.
  • stride: Stride of the convolving kernel. The default value of stride is 1.
  • padding: is used to control the amount of padding applied to the input. It can be either string or a tuple of giving the amount of implicit padding. The default value of padding is 0.
  • dilation: is used to control the spacing between the kernel elements and the default value of the dilation is 1.
  • groups are used to control the connection between the inputs and outputs. The default value of groups is 1.

So, with this, we understood the PyTorch nn functional conv2d.

Read: PyTorch Activation Function

PyTorch nn conv2d padding same

In this section, we will learn about the PyTorch nn conv2d padding same in python.

The PyTorch nn conv2d padding same is defined as a parameter that is used to control the amount of padding that is applied to the input.

It can be either string or tuple of the given amount of implicit padding and the default value of the padding is 0.

Code:

In the following code, we will import all the necessary libraries such as import torch, import torch.nn as nn.

  • a = nn.Conv2d(20, 37, 7, stride=2) is used with square kernels and equal stride.
  • a = nn.Conv2d(20, 37, (7, 9), stride=(2, 1), padding=(4, 2)) is used with non-square kernels and unequal stride and with padding.
  • input = torch.randn(24, 20 , 54, 104): Here we are describing the input variable by using torch.randn() function.
  • print(output) is used to print the output by using the print() function.
# Importing Libraries
import torch
import torch. nn as nn

# Declaring a variable by using square kernels and equal stride
a = nn.Conv2d(20, 37, 7, stride=2)

# Declaring a variable by using Non-square kernels and unequal stride and padding
a = nn.Conv2d(20, 37, (7, 9), stride=(2, 1), padding=(4, 2))

# Describe the variable
input = torch.randn(24, 20 , 54, 104)
output = a(input)

# Print output
print(output)

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn conv2d padding value is printed on the screen.

PyTorch nn conv2d padding same
PyTorch nn conv2d padding same

So, with this, we understood about the PyTorch conv2d padding same in python.

Read: PyTorch fully connected layer

PyTorch nn conv2d group

In this section, we will learn about python’s PyTorch nn conv2d group.

The PyTorch nn conv2d group is defined as a parameter that is used to control the connection between the inputs and outputs and the default value of the group is 1.

Code:

In the following code, firstly we will import all the necessary libraries such as import torch and import torch.nn as nn.

  • n = nn.Conv2d(22, 37, 7, stride=2) is used with square kernels and equal stride.
  • input = torch.randn(26, 22, 48, 48) is used to describe the variable by using torch.random() function.
  • output = n(input) is used to declare the output variable.
  • print(output) is used to print the output by using the print() function.
# Importing libraries
import torch
import torch. nn as nn

# With square kernels and equal stride
n = nn.Conv2d(22, 37, 7, stride=2)

# Describe the input variable
input = torch.randn(26, 22, 48, 48)
output = n(input)

# Print the output
print(output)

Output:

In the below output, you can see that the PyTorch nn conv2d group values are printed on the screen.

PyTorch nn conv2d group
PyTorch nn conv2d group

Read: PyTorch Binary Cross Entropy

PyTorch nn conv2d dilation

In this section, we will learn about python’s PyTorch nn conv2d dilation.

The PyTorch nn conv2d dilation is defined as a parameter that is used to control the spacing between the kernel elements and the default value of the dilation is 1.

Code:

In the following code, we will import some necessary libraries such as import torch, import torch.nn as nn.

  • n = nn.Conv2d(19, 36, 6, stride=2) is used with square kernels and equal stride.
  • m = nn.Conv2d(19, 36, (6, 8), stride=(2, 1), padding=(7, 5)) is used as non-square kernels and unequal stride and with padding.
  • m = nn.Conv2d(19, 36, (6, 8), stride=(2, 1), padding=(7, 5), dilation=(6, 4)) is used as non-square kernels and unequal stride and with padding and dilation.
  • input = torch.randn(23, 19, 53, 103): Here declaring the input variable by using torch.randn() method.
  • print(output) is used to print the output by using the output() function.
# Importing Libraries
import torch
import torch.nn as nn

# Declaring a variable by using square kernels and equal stride
n = nn.Conv2d(19, 36, 6, stride=2)

# Declaring a variable by using non-square kernels and unequal stride and padding
m = nn.Conv2d(19, 36, (6, 8), stride=(2, 1), padding=(7, 5))

# Declaring a variable by using non-square kernels, unequal stride, padding and dilation
m = nn.Conv2d(19, 36, (6, 8), stride=(2, 1), padding=(7, 5), dilation=(6, 4))

# Declaring the variable
input = torch.randn(23, 19, 53, 103)
output = m(input)

# Print the output
print(output)

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn conv2d dilation values are printed on the screen.

PyTorch nn conv2d dilation
PyTorch nn conv2d dilation

So, with this, we understood about the PyTorch nn con2d dilation.

Read: PyTorch Dataloader + Examples

PyTorch nn conv2d in CNN

In this section, we will learn about the PyTorch nn conv2d CNN in python.

Before moving forward we should have some piece of knowledge about Convolutional Neural networks (CNN).

Convolutional Neural Network is a type of artificial neural network that is used in image recognition.

Here we are using conv2d to deal with a convolutional neural network. It is a simple mathematical operation in which we skid a matrix or kernel of weights over 2d data and performs element-wise multiplication with the data.

Code:

In the following code, we will import some libraries such as import torch, and import Variable from torch.autograd, import torchvision.datasets, import torch.nn.init.

  • batch_size = 34 is used as a hyperparameter.
  • traindata = dtsets.MNIST(root=’MNIST_data/’, train=True, transform=transforms.ToTensor(), download=True) is used as training mnist dataset.
  • dataloader = torch.utils.data.DataLoader(dataset=traindata, batch_size=batch_size, shuffle=True) is used as a dataset loader.
  • class CNN(torch.nn.Module): Here we are creating a model class with the help of init() and forward() methods.
  • CNNmodel = CNN() is used to create an instance of the model.
import torch
from torch.autograd import Variable
import torchvision.datasets as dtsets
import torchvision.transforms as transforms
import torch.nn.init

# hyperparameters
batch_size = 34
keepprobab = 1

# MNIST dataset
traindata = dtsets.MNIST(root='MNIST_data/',
                          train=True,
                          transform=transforms.ToTensor(),
                          download=True)

testdata = dtsets.MNIST(root='MNIST_data/',
                         train=False,
                         transform=transforms.ToTensor(),
                         download=True)

# dataset loader
dataloader = torch.utils.data.DataLoader(dataset=traindata,
                                          batch_size=batch_size,
                                          shuffle=True)

# Display informations about the dataset
print('Train dataset:\t',traindata)
print('\nTest dataset:\t',testdata)

# Implementation of CNN/Convnet Model
class CNN(torch.nn.Module):

    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = torch.nn.Sequential(
            torch.nn.Conv2d(1, 34, kernel_size=3, stride=1, padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size=2, stride=2),
            torch.nn.Dropout(p=1 - keepprobab))
        self.layer2 = torch.nn.Sequential(
            torch.nn.Conv2d(34, 66, kernel_size=3, stride=1, padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size=2, stride=2),
            torch.nn.Dropout(p=1 - keepprobab))
        self.layer3 = torch.nn.Sequential(
            torch.nn.Conv2d(66, 130, 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 - keepprobab))

        # L4 FC 4x4x130 inputs -> 627 outputs
        self.fc1 = torch.nn.Linear(4 * 4 * 130, 627, bias=True)
        torch.nn.init.xavier_uniform(self.fc1.weight)
        self.layer4 = torch.nn.Sequential(
            self.fc1,
            torch.nn.ReLU(),
            torch.nn.Dropout(p=1 - keepprobab))
        # L5 Final FC 627 inputs -> 12 outputs
        self.fc2 = torch.nn.Linear(627, 12, bias=True)
        torch.nn.init.xavier_uniform_(self.fc2.weight) # initialize parameters

    def forward(self, y):
        output = self.layer1(y)
        output = self.layer2(output)
        output = self.layer3(output)
        output = output.view(output.size(0), -1)   # Flatten them for FC
        output = self.fc1(output)
        output = self.fc2(output)
        return output


#instantiate CNN model
CNNmodel = CNN()
CNNmodel

Output:

After running the above code we get the following output in which we can see that the PyTorch nn conv2d in CNN values is printed on the screen.

PyTorch nn conv2d in CNN
PyTorch nn conv2d in CNN

This is how we can use the PyTorch nn conv2d in CNN.

Read: PyTorch Pretrained Model

PyTorch nn conv2d bias

In this section, we will learn about python’s PyTorch nn conv2d bias.

The PyTorch nn conv2d bias is defined as an optional bias tensor of shape(out_channels). The default value of bias is None.

Code:

In the following code, firstly we will import libraries such as import torch.

  • input = torch.ones(2,2,7,7): Here we are describing the input variable by using torch.ones() function.
  • print(“Input = “,input) is used to print the input by using print() function.
  • m = torch.nn.Conv2d(in_channels = 1, out_channels = 1, kernel_size = 3): Here we are using conv2d module.
  • print(“Parameters = “,list(m.parameters())) is used to print the list of the parameters.
  • print(“bias = “,m.bias) is used to print the bias.
# Import library
import torch

# Describe the input variable
input = torch.ones(2,2,7,7)

print("Input = ",input)

# Using conv2d module
m = torch.nn.Conv2d(in_channels = 1, out_channels = 1, kernel_size = 3)

# Print the list of the parametrs
print("Net = ",m)
print("Parameters = ",list(m.parameters()))

# Print the bias
print("bias = ",m.bias)

Output:

After running the above code we get the following output in which we can see that the PyTorch nn conv2d bias value is printed on the screen.

PyTorch nn conv2d bias
PyTorch nn conv2d bias

So, with this, we understood about the PyTorch n n conv2d bias.

Read: Keras Vs PyTorch – Key Differences

PyTorch nn conv2d transpose

In this section, we will learn about the PyTorch nn conv2d transpose in python.

The PyTorch convtranspose2d applies a 2D transpose convolution operator over an input image collected from some input planes.

Syntax:

The syntax of PyTorch nn conv2d transpose

torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros', device=None, dtype=None)

Parameters:

The following are the parameters

  • in_channels used as several channels in the input images.
  • out_channels is used as several channels produced by the convolutions.
  • kernel_size is used as the size of the convolving kernel.
  • stride is used to control the stride from the cross-correlation.
  • padding: It controls the amount of implicit zero padding.
  • output_padding: It controls the additional size added to one side of the output shape.
  • dilation: It controls the spacing between the kernel points.
  • groups: It controls the connections between the inputs and outputs.

Example:

In the following code, firstly we will import all the necessary libraries such as import torch, import torch.nn as nn.

  • a = nn.ConvTranspose2d(18, 35, 5, stride=2) is used as a square kernel and equal stride.
  • a = nn.ConvTranspose2d(18, 35, (3, 5), stride=(2, 1), padding=(4, 2)) is used as a non-square kernels and unequal stride with padding.
  • inp = torch.randn(1, 18, 14, 14) is used as an exact output size that can also be specified as an argument.
  • torch.Size([1, 18, 8, 8]) is used to print the size on the screen.
#Importing libraries
import torch 
import torch.nn as nn
# With square kernels and equal stride
a = nn.ConvTranspose2d(18, 35, 5, stride=2)
# non-square kernels and unequal stride and with padding
a = nn.ConvTranspose2d(18, 35, (3, 5), stride=(2, 1), padding=(4, 2))
inp = torch.randn(22, 18, 52, 102)
out = a(inp)
# exact output size can be also specified as an argument
inp = torch.randn(1, 18, 14, 14)
dsample = nn.Conv2d(18, 18, 5, stride=2, padding=1)
usample = nn.ConvTranspose2d(18, 18, 5, stride=2, padding=1)
m = dsample(inp)
m.size()
torch.Size([1, 18, 8, 8])
out = usample(m, output_size=inp.size())
out.size()

Output:

In the below output you can see that the PyTorch nn conv2d transpose size is printed on the screen.

PyTorch nn conv2d transpose
PyTorch nn conv2d transpose

So, with this, we understood about the PyTorch nn conv2d transpose.

Read: PyTorch Batch Normalization

Pytorch nn conv2d parameters

In this section, we will learn about the PyTorch nn conv2d parameters in python.

Before moving forward we should have a piece of knowledge about parameters. Parameters are measurable factors established as one of the sets that describe the system or set the condition of its operations.

Code:

In the following code, we will import all the necessary libraries such as import torch, import torch.nn as nn.

  • p = nn.Conv2d(22, 39, 5, stride=2) is used with square kernels and equal stride and the square kernel and equal stride are the parameter are are used in nn.conv2d() method.
  • inp = torch.randn(26, 22, 56, 106) is used to describe the variable by using a torch.random() function.
  • out = p(inp) is used to declare the output variable.
  • print(out) is used to print the output by using the print() function.
# Importing Libraries
import torch
import torch. nn as nn

# With square kernels and equal stride
p = nn.Conv2d(22, 39, 5, stride=2)

# Describe the variable
inp = torch.randn(26, 22, 56, 106)

# Declare the output variable
out = p(inp)
# Print output
print(out)

Output:

After running the above code we get the following output in which we can see that the PyTorch nn conv2d parameters values are printed on the screen.

PyTorch nn conv2d parameters
PyTorch nn conv2d parameters

So, with this, we understood about the PyTorch nn conv2d parameters.

Read: PyTorch Load Model + Examples

PyTorch nn conv2d weight

In this section, we will learn about the Pytorch nn conv2d weight in python.

The PyTorch nn conv2d weight is used to execute the convolution to a 2d data and some additional options like padding etc.

Code:

In the following code, we will import the library such as import torch.

  • inp = torch.ones(1,1,6,6) is used to describe the input variable by using torch.ones() function.
  • print(“Input = “,inp) is used to print the input values.
  • print(“Parameters = “,list(m.parameters())) is used to print the list of the parameters.
  • print(“Weight = “,m.weight) is used to print the weights.
  • print(“bias = “,m.bias) is used to print the bias.
# Import library
import torch

# Describe the input variable
inp = torch.ones(1,1,6,6)

# Print input
print("Input = ",inp)

m = torch.nn.Conv2d(in_channels = 1, out_channels = 1, kernel_size = 3)

# Print the parameter list
print("net = ",m)
print("Parameters = ",list(m.parameters()))
# Print the weight
print("Weight = ",m.weight)
# Print the bias
print("bias = ",m.bias)

out = m(inp)

print("Output = ",out)

Output:

After running the above code we get the following output in which we can see that the PyTorch nn conv2d weight is printed on the screen.

PyTorch nn conv2d weigh
PyTorch nn conv2d weigh

So, with this, we understood about the PyTorch nn conv2d weight.

Read: Cross Entropy Loss PyTorch

PyTorch nn conv2d input channels and output channels

In this section, we will learn about the PyTorch nn conv2d input channels and out channels in python.

The PyTorch nn conv2d input channels are used as several channels in the input image.

The PyTorch conv2d output channels are used as the number of channels produced by the convolution.

Code:

In the following output, we will import all the necessary libraries such as import torch, and import Variable from torch.autograd, import torch.nn as nn, and import torch.nn.functional.

  • class model(nn.Module): Here we are creating a model class with the help of init() and forward() functions.
  • model = model() is used to create an instance of the model.
  • print(model) is used to print the model with the help of the print() function.
# Importing Libraries
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as fun

# Create model class
class model(nn.Module):

    def __init__(self):
        super(model, self).__init__()
        # 1 input image channel, 8 output channels, 7x7 square convolution
        self.conv1 = nn.Conv2d(1, 8, 7)
        self.conv2 = nn.Conv2d(8, 18, 7)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(18 * 7 * 7, 140)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, y):
        # Max pooling over a (2, 2) window
        y = fun.max_pool2d(fun.relu(self.conv1(y)), (2, 2))
        # If the size is a square you can only specify a single number
        y = fun.max_pool2d(fun.relu(self.conv2(y)), 2)
        y = y.view(-1, self.numflat_featurs(y))
        y = fun.relu(self.fc1(y))
        y = fun.relu(self.fc2(y))
        y = self.fc3(y)
        return y

    def numflat_featurs(self, y):
      # all dimensions except the batch dimension
        size = y.size()[1:]  
        numfeaturs = 1
        for i in size:
            numfeaturs *= i
        return numfeaturs

# Instantiate the model
model = model()
# Print the model
print(model)

Output:

After running the above code we get the following output in which we can see that the PyTorch nn conv2d input channels and output channels are printed on the screen.

PyTorch nn conv2d input channels and output channels
PyTorch nn conv2d input channels and output channels

So, with this, we understood about the PyTorch nn conv2d input channels and output channels.

Also, take a look at some more PyTorch tutorials.

So, in this tutorial, we discussed Pytorch nn conv2d and we have also covered different examples related to its implementation. Here is the list of models that we have covered.

  • PyTorch nn conv2d
  • PyTorch nn conv2d example
  • PyTorch nn functional conv2d
  • PyTorch nn conv2d padding same
  • PyTorch nn conv2d group
  • PyTorch nn conv2d dilation
  • PyTorch nn conv2d in CNN
  • PyTorch nn conv2d bias
  • PyTorch nn conv2d transpose
  • Pytorch nn conv2d parameters
  • PyTorch nn conv2d weights
  • PyTorch nn conv2d input channels and output channels