Fully Connected Layers

After a series of convolution and pooling layers extract features from an image, a CNN needs a way to combine all of that information to make a final decision - that's the role of fully connected layers.

How They Work

The multi-dimensional feature maps produced by the convolutional layers are first flattened into a single long vector, which is then passed through one or more standard dense (fully connected) layers - identical in structure to the Multilayer Perceptron covered earlier in this course.

Feature Maps -> Flatten -> Dense Layer(s) -> Output Layer (Softmax/Sigmoid)

Why Flattening is Needed

Convolutional layers preserve spatial structure (rows and columns), but fully connected layers expect a simple 1D vector of inputs - flattening bridges that gap by unrolling the feature map into a single sequence of numbers.

Implementing in Python

from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Conv2D(32, (3,3), activation="relu", input_shape=(64,64,3)),
    layers.MaxPooling2D((2,2)),
    layers.Flatten(),
    layers.Dense(128, activation="relu"),
    layers.Dense(10, activation="softmax")
])

Overfitting Risk

Fully connected layers contain the vast majority of a CNN's total parameters, making them especially prone to overfitting - techniques like Dropout are commonly applied here to randomly deactivate neurons during training and improve generalization.

The final fully connected layer's size always matches the number of output classes, with a Softmax activation used for multi-class problems to output a clean probability distribution.

Coming Up Next

With the full CNN architecture covered, let's study a real, landmark example that put this architecture on the map - the AlexNet case study.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course