C.W.K.
Stream
Lesson 03 of 07 · published

Understanding Layer Arguments

~8 min · sequential

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

The knobs on a layer

A Dense layer takes a handful of constructor arguments, but only two of them you'll touch on almost every layer — units and activation. The rest have well-chosen defaults you should leave alone until you have a concrete reason not to.

ArgumentTypeDescription
unitsintOutput dimensionality — how many neurons in this layer
activationstr/fnActivation function: 'relu', 'sigmoid', 'softmax', 'tanh', None
input_shapetupleShape of input (only needed for first layer, prefer keras.Input)
use_biasboolWhether to add a bias term (default: True)
kernel_initializerstrWeight initialization: 'glorot_uniform' (default), 'he_normal', etc.

Activation is a decision about the output, not a style choice

The activation on your last layer is dictated by the task, not by taste — get it wrong and your loss function silently receives values it can't interpret:

  • ReLU — the default for hidden layers. Simple, fast, and it dodges the vanishing-gradient trap that sigmoid/tanh fall into in deep stacks.
  • Sigmoid — output layer for binary classification: squashes to a single probability in [0, 1].
  • Softmax — output layer for multi-class classification: a vector of probabilities that sums to 1 across the classes.
  • Tanh — output range [-1, 1]. Occasionally useful in hidden layers and common in older RNN cells.
  • None / Linear — output layer for regression. No transformation, so the network can emit any real number.

Notice the pattern: hidden layers almost always use ReLU, and the final activation is chosen by what you're predicting. That single rule eliminates a whole class of beginner bugs.

External links

Exercise

Build a Dense layer with activation=None. Apply keras.activations.relu manually after the layer call. Compare to Dense(activation='relu'). Should produce identical results.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.