Python / Python Deep Learning and Neural Networks Interview Questions
Explain how convolutional layers work and why they are well-suited to image data.
A convolutional layer applies a set of learnable filters (kernels) by sliding each filter over the spatial dimensions of the input and computing a dot product at each position. For a 2D image, a kernel of size k×k with C_in input channels and C_out output channels has k×k×C_in×C_out parameters total. This produces one feature map per output channel, where each value represents the response of that filter at a specific spatial location.
CNNs are powerful for images because of two structural inductive biases they encode: (1) translation equivariance — the same filter is applied everywhere, so if an object moves in the image, the corresponding feature map activation moves identically; (2) parameter sharing — instead of a separate weight per input-output pixel pair (as a fully-connected layer would require), the filter weights are shared across all spatial locations, drastically reducing parameters and improving sample efficiency.
import torch
import torch.nn as nn
# Standard Conv2d usage
# Input: (batch, C_in, H, W)
# Output: (batch, C_out, H', W')
conv = nn.Conv2d(
in_channels=3, # RGB image
out_channels=64, # 64 filters
kernel_size=3, # 3x3 kernel
stride=1,
padding=1, # 'same' padding — preserves H and W
)
x = torch.randn(8, 3, 32, 32) # batch of 8 RGB 32x32 images
out = conv(x)
print(out.shape) # torch.Size([8, 64, 32, 32])
n_params = 3 * 64 * 3 * 3 + 64 # weights + biases
print('Parameters:', n_params) # 1792
# Compare: FC layer 3*32*32 -> 64*32*32 would be 3*32*32*64*32*32 = 603M!
# Typical CNN block:
cnn_block = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2), # halves spatial dims
)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
