# Convolutional Neural Networks, Simplified

#4 Jun 3, 2024 Architecture

https://www.aibutsimple.com/p/convolutional-neural-networks-simplified

A Convolutional Neural Network (CNN) is a type of neural network that specializes in processing data that has a grid-like shape, such as an image.

CNNs are one of the main types of neural networks that are commonly studied in the world today.

- Other types include RNN, LSTM, GAN, Transformers, etc.

CNN’s are widely used (sort of an industry standard): anything from facial recognition to medical imaging can be tackled using this network.

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/9cc8a025-04d9-4fdc-a532-8723a27f2dd5/image.png)

CNN to classify cats and dogs

They are based on convolutions, a mathematical concept that "blends" one function with another.

- If you have the time, feel free to check out this great [resource](https://www.youtube.com/watch?v=KuXjwB4LzSA&themeRefresh=1&utm_source=newsletter.aibutsimple.com&utm_medium=referral&utm_campaign=convolutional-neural-networks-simplified)on convolutions

The role of CNNs is to reduce the images into a form that is easier to process, without losing features that are needed for a good prediction.

- This is what convolutions are used for: to reduce the size of input by “blending” the image pixels

This is important when we are designing an architecture that is not only good at learning features but also scalable to large datasets.

The unique thing about CNN’s are that they have specialized layers that help it adaptively learn features effectively from input images.

- These layers include convolutional layers, pooling layers, and fully connected layers.

The layers are arranged in such a way such that they detect simpler patterns first (like lines, curves, etc.) and more complex patterns (like faces, objects, etc.) further along.

- It’s interesting because CNNs are modeled after the visual cortex of the brain, as they process visual information in a hierarchical manner (similar to the brain)
- In the visual cortex, neurons have receptive fields that respond to specific parts of the visual field. Similarly, in CNNs, filters (or kernels) scan through the image and respond to specific patterns or features.

Let’s talk about the specialized layers in a CNN:

### Convolutional Layer

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/4f91e6ed-7841-487d-a029-6b5f95e954d4/Add_a_heading__600_x_350_px___2_-1.png)

Convolutional layers are usually paired with ReLU

The convolutional layer is the core of the CNN. It carries the main portion of the network’s computational load.

The main role of this layer is to reduce the input size of the image and learn the most important features of the image.

This layer uses a special type of matrix, called a kernel, to help with this task.

- A kernel is a small matrix of learnable parameters (weights). During training, the values in the kernel are adjusted to help the network learn the most useful features for the given task.

During the forward pass, the kernel slides across the width and height of the image with a defined sliding size, producing the blurred version and reducing the image size:

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/2bf1919c-9cfb-46e1-840f-9eb4d9a21a77/convolution-operation-with-stride-length.gif)

Visualization of a kernel sliding on an input to create a smaller image, called a feature map

- The sliding size of the kernel is called a stride, and it is a hyperparameter.
  - When the stride is set to 1, the filter moves one pixel at a time, both horizontally and vertically.
  - When the stride is greater than 1, the filter skips some positions as it moves across the input image. (makes the feature map smaller)
  - In the above gif, the stride is 2.

At each position, the kernel does an elementwise product between its weights and the corresponding values (the small region of the input data covered by the kernel), followed by summing the result to get a single value.

After sliding down the whole image, it produces a two-dimensional representation of the image known as a feature map.

- A feature map shows the importance of each region of the image.

Since convolution is a linear operation and images are far from linear, activation functions are used after the convolutional layer to introduce non-linearity to the feature map.

- In recent years, people have shifted to using ReLU and its variations for a faster convergence

### Pooling Layer

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/e13db15a-fae7-4a54-9e45-f60d858c3ac2/Add_a_heading__600_x_350_px___3_-1.png)

The pooling layer of a CNN performs a “down-sampling” operation, which reduces the dimensions (width and height) of the input feature maps while retaining important information.

This layer is crucial, as it decreases the amount of parameters and computation.

The pooling operation is processed on every slice of the input individually.

- There will be multiple neurons in each convolutional layer with multiple kernels, which means the input to the pooling will be 3-dimensional (width, height, depth)
- A single depth slice is a small region of the input volume that spans all the depth channels but retains to the height and width

In terms of pooling options, there are several pooling functions, such as average pooling, max pooling, and global pooling.

- The most popular process is max pooling, which outputs the maximum value of a region.

Consider a 4×4 feature map and a 2×2 pooling window with a stride of 2, visualized with a single depth slice:

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/d4295a8a-94bf-49e9-b0e4-aa42e8c3b513/image.png)

Max pooling literally chooses the maximum of that region.

In this example, max pooling will choose the maximum value of each region covered by the pooling window, sliding down the image with a stride of 2 (skipping the squares [[1, 2],[6, 7]] and [[2,1],[2,3]]).

### Fully Connected Layer

Neurons in this layer are connected with all neurons in the previous and next layers as seen in a regular fully connected neural network:

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/f19673a9-1118-4688-8732-f3fee54b1322/Copy_of_Add_a_heading__1_.png)

- So its output can be computed as usual by a matrix multiplication and a bias factor

The fully connected layer’s role in a CNN is to map the representation between the input and the output.

![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,width=1200,format=auto,onerror=redirect,quality=80/uploads/asset/file/56c326b1-8cb1-4e81-8605-e3cb7d239bbd/Add_a_heading__600_x_350_px___4_-1.png)

After passing through the fully connected layer, the data will be processed to produce the final output.

It will pass through a flattening layer, a layer that converts the input into a one-dimensional vector.

- If the network is performing a multi-class classification task, the final layer usually applies the softmax activation function. This function converts the raw output scores (logits) into probabilities for each class (needs one-dimensional input)
  - The softmax function ensures that the sum of all probabilities is equal to 1
- For binary classification tasks, a sigmoid activation function is often used in the final layer. This function outputs a probability between 0 and 1, indicating the probability of the input belonging to a particular class

As well as producing an output, the model will calculate losses and use backpropagation and an optimization algorithm (like gradient descent, Rmsprop, etc.) to train the network.

---

If you enjoy our content, consider supporting us so we can keep doing what we do. Please share this with a friend!

If you like, you can also donate to our team to push out better newsletters every week!

[Support us ☕](https://ko-fi.com/aibutsimple?utm_source=newsletter.aibutsimple.com&utm_medium=referral&utm_campaign=convolutional-neural-networks-simplified)

---

That’s it for this week’s issue of AI, but simple. See you next week!

—AI, but simple team
