Convolutional Layers

Deep LearningKerasFree Lesson

Advertisement

Introduction

Convolutional layers detect spatial patterns in images through local receptive fields and weight sharing.

Conv2D Layer

from tensorflow.keras import layers
import tensorflow as tf

# Basic Conv2D
model = keras.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(10, activation='softmax')
])

Padding

# Valid (no padding) - output smaller
layers.Conv2D(32, (3, 3), padding='valid')

# Same - output same size as input
layers.Conv2D(32, (3, 3), padding='same')

# Causal - for 1D
layers.Conv1D(32, 3, padding='causal', activation='relu')

Stride

# Stride of 2 reduces dimensions
layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same')

# Dilated convolutions
layers.Conv2D(32, (3, 3), dilation_rate=(2, 2))

Separable Convolution

# More efficient than Conv2D
layers.SeparableConv2D(32, (3, 3), activation='relu')

# Depthwise + pointwise
layers.DepthwiseConv2D((3, 3), depth_multiplier=1)
layers.Conv2D(32, (1, 1))  # Pointwise

Practice Problems

  1. Build CNN for image classification
  2. Use different padding modes
  3. Apply strided convolutions
  4. Implement separable convolutions
  5. Add multiple conv layers

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement