Imagine you have a photo and you want to find edges or patterns in it. What does the convolution operation do in this context?
Think about how a filter moves over an image to detect patterns.
The convolution operation applies a small filter (kernel) over the image to multiply and sum pixel values, highlighting features like edges or textures.
Given a 28x28 grayscale image input and a convolutional layer with 16 filters, kernel size 3x3, stride 1, and 'valid' padding, what is the output shape?
import tensorflow as tf input_tensor = tf.random.uniform([1, 28, 28, 1]) conv_layer = tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='valid') output = conv_layer(input_tensor) print(output.shape)
Use the formula: output_size = input_size - kernel_size + 1 when padding is 'valid'.
With 'valid' padding, output height and width = 28 - 3 + 1 = 26. Number of filters = 16, batch size = 1.
If you increase the stride from 1 to 2 in a convolutional layer, what happens to the output feature map size?
Think about how skipping pixels affects the number of positions the filter can be applied.
Increasing stride means the filter jumps more pixels, reducing the number of positions and thus output size.
How many trainable parameters are there in a Conv2D layer with 32 filters, kernel size 3x3, and input channels 3?
Use the formula: (kernel_height * kernel_width * input_channels + 1) * number_of_filters.
Each filter has 3*3*3=27 weights plus 1 bias. Total parameters = (27+1)*32 = 896.
Consider this TensorFlow code snippet:
import tensorflow as tf input_tensor = tf.random.uniform([1, 28, 28, 1]) conv_layer = tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same') output = conv_layer(input_tensor[0]) print(output.shape)
Why does this code raise an error?
Check the shape of input_tensor[0] compared to what Conv2D expects.
Conv2D expects a 4D tensor with batch size. input_tensor[0] removes batch dimension, making it 3D, causing an error.