๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #175

Fully Connected Layer

The fully connected layer at the end of a CNN is exactly the same dense layer covered throughout the Neural Network Fundamentals category โ€” its role here is specifically to turn the extracted spatial features into a final classification decision, after flattening (or GAP) has produced a plain feature vector.

What It Does, in a CNN Context

\[ \mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b} \]

\(\mathbf{x}\) here is the flattened (or globally-pooled) feature vector from the convolutional part of the network โ€” every value in this vector represents some learned, increasingly abstract property of the original image. The fully connected layer combines these features linearly (optionally through one or more hidden fully-connected layers, each with its own activation) to produce the final output โ€” typically class logits, fed into softmax for classification.

Where Parameter Count Often Explodes

As shown numerically in Global Average Pooling, a fully-connected layer following a flattened (not globally-pooled) large feature map can dominate a CNN's total parameter count โ€” sometimes accounting for the majority of an entire architecture's weights, despite being just one or two layers at the very end. This is exactly why GAP has become a popular alternative specifically to shrink this layer's input size before it's ever reached.

Code โ€” The Classification Head

import torch.nn as nn

# Convolutional feature extractor
features = nn.Sequential(
    nn.Conv2d(3, 32, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
    nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
)

# Classification head -- fully connected layer(s) turning features into class scores
classifier = nn.Sequential(
    nn.Flatten(),
    nn.Linear(64 * 8 * 8, 128), nn.ReLU(), nn.Dropout(0.5),
    nn.Linear(128, 10)   # final output: 10 class logits
)

model = nn.Sequential(features, classifier)

Regularizing the Fully Connected Layers Specifically

Because fully-connected layers in a CNN often carry a disproportionate share of the model's total parameters, they're frequently a specific target for regularization โ€” Dropout (see Dropout) is very commonly applied exactly here, between fully-connected layers, even in architectures that use little or no dropout within their convolutional layers.

Common Mistakes

  • Adding many wide fully-connected layers at the end of a CNN without considering the resulting parameter and overfitting cost โ€” this is precisely the situation Global Average Pooling was developed to avoid.
  • Forgetting that the fully-connected layer's role is purely to combine already-extracted features, not to do any further spatial feature extraction โ€” that job belongs entirely to the convolutional layers before it.

Interview Relevance

Q: "Why might a CNN's fully-connected layers account for more total parameters than all its convolutional layers combined?" Convolutional layers benefit from parameter sharing (the same small kernel is reused across every spatial position), keeping their parameter count small and largely independent of input size. A fully-connected layer following a large flattened feature map has a weight matrix sized by the full flattened vector length times the number of output units โ€” with no such sharing, this can easily dominate the architecture's total parameter count, especially without Global Average Pooling to shrink that input first.

Practice Question

Why is Dropout so commonly applied specifically between fully-connected layers in a CNN, even when it's used sparingly (or not at all) in the convolutional layers?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Fully Connected Layer โ€“ FAQs

Quick answers about learning Fully Connected Layer in Deep Learning.

This free note from CodingNow 2.0 explains Fully Connected Layer in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Fully Connected Layer, is 100% free with no signup required.
With focused practice, most students grasp Fully Connected Layer in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now