Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Applied Generative AI with GANS/8 CNN_Mathematical_Explanation.ipynb
4780 views
Kernel: Python 3 (ipykernel)

Convolutional Neural Networks (CNN)

Step-by-Step Mathematical Explanation

This notebook explains CNNs with mathematical intuition and simple examples.

1. What is a Convolution?

A convolution is a mathematical operation between an input matrix (image) and a kernel (filter).

Formula: image.png

2. Example Input Image and Kernel

We use a 3×3 image and a 2×2 kernel to explain convolution.

import numpy as np image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) kernel = np.array([[1, 0], [0, -1]]) image, kernel
(array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), array([[ 1, 0], [ 0, -1]]))

3. Convolution Calculation (Manually)

Each output value is computed by element-wise multiplication followed by summation.

output = np.zeros((2,2)) for i in range(2): for j in range(2): region = image[i:i+2, j:j+2] output[i,j] = np.sum(region * kernel) output
array([[-4., -4.], [-4., -4.]])
[1,1] f(x)= x*filter*w+b

4. Activation Function (ReLU)

ReLU introduces non-linearity.

image.png

relu_output = np.maximum(0, output) relu_output
array([[0., 0.], [0., 0.]])
2*2 (0,0,0,0): 4

5. Pooling (Max Pooling)

Pooling reduces spatial dimensions.

image.png

import numpy as np b=[1,2,89] c=np.array(b) p=np.max(c) p
89
pooled = np.max(relu_output) pooled

6. Fully Connected Layer (Mathematics)

Flatten the feature map and apply:

image.png

Where:C = number of classes

x = relu_output.flatten() W = np.array([0.5, -0.2, 0.1, 0.4]) b = 0.1 y = np.dot(W, x) + b y

7. Summary

  • Convolution extracts features

  • ReLU adds non-linearity

  • Pooling reduces size

  • Fully connected layers perform classification

A CNN learns convolutional filters that extract hierarchical spatial features and optimizes them end-to-end using backpropagation to minimize classification loss.

image.png

image-2.png

image-3.png