0
0
TensorFlowml~20 mins

Convolution operation concept in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Convolution operation concept
Problem:You want to understand how a convolution operation works in a simple neural network layer using TensorFlow. Currently, you have a model that applies a convolution but you don't see how the output changes with different filters.
Current Metrics:No training metrics yet, just initial output shape and values from a convolution layer with one filter.
Issue:The model uses only one filter and the output is not very informative. You want to experiment with multiple filters and see how the convolution operation extracts different features.
Your Task
Modify the convolution layer to use multiple filters and observe how the output changes. Understand how the convolution operation transforms the input image.
Use TensorFlow and Keras layers only.
Keep the input shape fixed as (28, 28, 1) to simulate a grayscale image.
Do not add training or complex model layers; focus on convolution operation only.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
import numpy as np

# Create a dummy grayscale image of shape (1, 28, 28, 1)
input_image = np.random.rand(1, 28, 28, 1).astype(np.float32)

# Define a Conv2D layer with 3 filters and kernel size 3x3
conv_layer = tf.keras.layers.Conv2D(filters=3, kernel_size=(3,3), padding='same', activation='relu')

# Apply convolution
output = conv_layer(input_image)

# Print input and output shapes
print(f'Input shape: {input_image.shape}')
print(f'Output shape: {output.shape}')

# Print output values for the first filter at the first 5x5 region
print('Output values for filter 0, top-left 5x5 region:')
print(output[0, :5, :5, 0].numpy())
Increased the number of filters from 1 to 3 to see multiple feature maps.
Used kernel size (3,3) with 'same' padding to keep output size equal to input size.
Added ReLU activation to observe non-linear feature extraction.
Printed output shape and sample values to understand convolution effect.
Results Interpretation

Before: Output shape was (1, 28, 28, 1) with only one filter, showing a single feature map.

After: Output shape is (1, 28, 28, 3) with three filters, showing three different feature maps extracted by the convolution operation.

Increasing the number of filters in a convolution layer allows the model to extract multiple features from the input image, each filter detecting different patterns or edges.
Bonus Experiment
Try changing the kernel size to (5,5) and observe how the output changes.
💡 Hint
Larger kernels capture bigger patterns but may reduce spatial resolution; compare output shapes and values.