Modules / Deep Learning / Classification

How Do Fully Connected Layers Work in CNN?

Visualize how a flattened 1D array of features connects to every node in the output layer using weights, biases, and Softmax to make final classifications.

Dense Network Graph

Dot Product Formula

Zj = Σ (Xi · Wij) + Bj
Click "Forward Pass" to view calculations.

Understanding Fully Connected (Dense) Layers

The decision-making powerhouse at the end of a Convolutional Neural Network.

The Transition to 1D

Throughout the early stages of a CNN, images are processed as 2D grids (or 3D volumes with color channels). Convolutional and pooling layers extract spatial features like edges, textures, and shapes. However, to make a final classification (e.g., "Is this a cat or a dog?"), the network must flatten this 2D/3D data into a single 1D list of numbers. This is the input vector you see on the left.

Why "Fully Connected"?

It is called a Fully Connected (or Dense) layer because every single node in the input vector is connected to every single node in the output vector. If you have 4 inputs and 3 outputs, there are $4 \times 3 = 12$ distinct connections (weights), plus 3 biases.

Each weight represents the "importance" of a specific feature for a specific class. For example, if Input Feature 2 strongly represents "pointy ears", the weight connecting Input 2 to the "Cat" output node will likely be a high positive number, while its connection to "Bird" might be negative.

Guided Experiments with This Interactive

  1. Observe the Forward Pass:

    Click the Forward Pass button. Watch the center canvas. The network computes the output one node at a time. It highlights the connections, multiplies each input by its corresponding weight, sums them up, and adds a bias. This raw sum is called the Logit ($Z$).

  2. Tweak the Weights:

    Look at the Weights table on the right. Change the weights connecting to the "Cat" column to high positive numbers (e.g., 2.0). Click Forward Pass again. Notice how the raw score (Logit) for the Cat class shoots up!

  3. The Role of Bias:

    At the bottom of the weights table are the Biases. A bias is an extra value added to the final sum. It shifts the activation function left or right. Try setting the Bird bias to a high value like 5.0. It will artificially inflate the Bird's probability regardless of the inputs.

  4. Softmax Activation:

    Raw Logits can be any number (e.g., -5.2, 0.4, 12.8). To interpret them as probabilities, we apply the Softmax function. Softmax squashes the logits into values between 0 and 1, ensuring they all sum up to exactly 1.0 (or 100%). Notice how the progress bars in the Output section always balance each other out.

The Dense Formula

For an output node $j$, the raw Logit ($Z_j$) is the dot product of the inputs ($X$) and weights ($W$), plus bias ($b$):

Zj = (X1·W1j + X2·W2j + ...) + bj

Softmax is then applied to turn $Z$ into probabilities ($P$):

Pj = eZj / Σ(eZ)

Key Takeaways

  • Dense layers sit at the very end of a CNN to perform classification.
  • They require 1D flattened input vectors.
  • Every input is connected to every output, creating a massive number of weight parameters.
  • Softmax transforms raw dot-product scores (Logits) into readable probabilities.
Cheat sheet

Fully Connected Layer in CNN

Visualize how a flattened 1D array of features connects to every node in the output layer using weights, biases, and Softmax to make final classifications.

COMPUTER VISION · vizlearn.in/computer_vision/how_dense_layer_works_in_cnn.html