Introduction
A Conv2d layer helps a computer see patterns in images by sliding small filters over the picture to find edges, shapes, or colors.
Jump into concepts and practice - no test required
nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')
conv = nn.Conv2d(3, 16, 3)
conv = nn.Conv2d(1, 32, 5, stride=2, padding=2)
conv = nn.Conv2d(10, 20, (3, 5), bias=False)
import torch import torch.nn as nn # Create a Conv2d layer conv = nn.Conv2d(in_channels=1, out_channels=2, kernel_size=3, stride=1, padding=1) # Create a dummy grayscale image batch: batch size 1, 1 channel, 5x5 pixels input_tensor = torch.arange(25, dtype=torch.float32).reshape(1, 1, 5, 5) # Apply the Conv2d layer output = conv(input_tensor) # Print output shape and values print('Output shape:', output.shape) print('Output tensor:', output) # Print layer weights shape print('Weights shape:', conv.weight.shape) print('Bias shape:', conv.bias.shape)
nn.Conv2d layer in PyTorch primarily do?conv = nn.Conv2d(3, 6, kernel_size=5) output = conv(torch.randn(1, 3, 32, 32)) print(output.shape)
conv = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=3) output = conv(torch.randn(1, 3, 28, 28)) print(output.shape)