0
0
PyTorchml~5 mins

Why CNNs detect spatial patterns in PyTorch

Choose your learning style9 modes available
Introduction

Convolutional Neural Networks (CNNs) are designed to find patterns in images or data that have a spatial layout, like shapes or edges. They look at small parts of the data at a time, which helps them understand where things are in the image.

When you want to recognize objects in photos, like cats or cars.
When you need to detect patterns in medical images, such as tumors in X-rays.
When analyzing satellite images to find roads or buildings.
When building systems that understand handwriting or text in pictures.
When working with any data that has a grid-like structure, like audio spectrograms.
Syntax
PyTorch
import torch
import torch.nn as nn

class SimpleCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3)

    def forward(self, x):
        return self.conv(x)

The nn.Conv2d layer looks at small 2D patches (called kernels) of the input.

It slides this kernel over the input image to detect spatial features like edges or textures.

Examples
This creates a convolutional layer that looks at color images with 3 channels and outputs 16 feature maps using 5x5 kernels.
PyTorch
conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5)
This runs a random 32x32 color image through the convolution and prints the output shape.
PyTorch
output = conv(torch.randn(1, 3, 32, 32))
print(output.shape)
Sample Model

This code creates a simple CNN with one convolutional layer. The kernel is set to detect horizontal edges. The input is a 5x5 image with a square pattern. The output shows where the horizontal edges are detected.

PyTorch
import torch
import torch.nn as nn

# Define a simple CNN
class SimpleCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(1, 1, 3, padding=1)  # 3x3 kernel, padding to keep size

    def forward(self, x):
        return self.conv(x)

# Create a sample 5x5 image with a simple pattern
image = torch.tensor([[[
    [0, 0, 0, 0, 0],
    [0, 1, 1, 1, 0],
    [0, 1, 0, 1, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
]]], dtype=torch.float32)

model = SimpleCNN()

# Manually set the kernel to detect horizontal edges
with torch.no_grad():
    model.conv.weight[:] = torch.tensor([[[[-1, -1, -1],
                                          [ 0,  0,  0],
                                          [ 1,  1,  1]]]])
    model.conv.bias[:] = 0

output = model(image)
print(output[0,0])
OutputSuccess
Important Notes

CNNs use small kernels to focus on local parts of the image, which helps them learn spatial patterns.

Padding keeps the output size the same as input, so spatial information is preserved.

Weights in the kernel act like filters that detect specific features like edges or textures.

Summary

CNNs detect spatial patterns by sliding small filters over images.

These filters learn to recognize features like edges, shapes, or textures.

This makes CNNs very good for image and spatial data tasks.