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

Input, Hidden & Output Layers

This note zooms into each of the three layer types individually โ€” what data actually flows through each one, and the specific design decisions you'll make for each when building a real network.

Input Layer

The input layer's "neurons" don't compute anything โ€” they simply hold the raw feature values. Its size is fixed by your data: an image of 28ร—28 pixels flattened gives an input layer of 784 values; a dataset with 10 numeric features gives an input layer of 10. There are no learnable weights associated with the input layer itself โ€” weights appear on the connections leaving it, toward the first hidden layer.

Hidden Layers

Hidden layers do the actual representation learning โ€” each one transforms its input into a new representation, ideally one that makes the task easier for the next layer. Design choices here include:

ChoiceConsideration
Number of hidden layersMore layers โ†’ more hierarchical abstraction, but harder to train without mitigations (see Challenges in Deep Learning)
Neurons per hidden layerMore neurons โ†’ more capacity per layer, more parameters, higher overfitting risk with limited data
Activation functionAlmost always non-linear (ReLU is the most common modern default) โ€” covered fully next category

There's no universal formula for "the right" hidden layer sizes โ€” this is one of the hyperparameters tuned empirically (see the Hyperparameter Tuning category), though common practice starts wide-ish and narrows toward the output, or matches known-good architectures for the task type.

Output Layer

The output layer's size and activation are dictated directly by the task โ€” this is one of the few architecture choices that isn't really a free hyperparameter:

TaskOutput Layer SizeActivation
Binary classification1 neuronSigmoid
Multi-class classification (\(k\) classes)\(k\) neuronsSoftmax
Regression (single value)1 neuronNone (identity) โ€” or occasionally ReLU if the target is always non-negative
Multi-output regression\(k\) neurons (one per target)None (identity), typically

Code โ€” Sizing Each Layer Correctly

import torch.nn as nn

# Example: classifying 28x28 grayscale images into 10 digit classes
model = nn.Sequential(
    nn.Flatten(),                 # input layer: 28*28 = 784 values
    nn.Linear(784, 128), nn.ReLU(),  # hidden layer 1
    nn.Linear(128, 64), nn.ReLU(),    # hidden layer 2
    nn.Linear(64, 10)                 # output layer: 10 classes -- no activation here,
                                        # because nn.CrossEntropyLoss applies softmax internally
)

Common Mistakes

  • Applying softmax explicitly in the model's forward() and then also using nn.CrossEntropyLoss (which applies it internally) โ€” this double-applies softmax and silently corrupts training, as flagged already in Cross-Entropy.
  • Mismatching the output layer size to the task โ€” e.g. using 1 output neuron with softmax (softmax needs at least 2 outputs to be meaningful) or forgetting that binary classification's single sigmoid output represents \(P(\text{class}=1)\), not two separate class probabilities.

Interview Relevance

Q: "How would you size the output layer for a model predicting both a house's price and its number of bedrooms from the same input features?" A multi-output regression head: 2 output neurons (one per target), typically with no activation (identity), since both price and bedroom count are unbounded continuous values (or bounded only by non-negativity, in which case a ReLU output could be considered).

Practice Question

You're building a model to classify an email as spam, promotional, or important (3 mutually exclusive categories). What size and activation should the output layer use?

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

Input, Hidden & Output Layers โ€“ FAQs

Quick answers about learning Input, Hidden & Output Layers in Deep Learning.

This free note from CodingNow 2.0 explains Input, Hidden & Output Layers 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 Input, Hidden & Output Layers, is 100% free with no signup required.
With focused practice, most students grasp Input, Hidden & Output Layers 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