One of the most confusing topics for beginners in Deep Learning is deciding which output layer, activation function, loss function, and dataset label mode should be used for a particular machine learning problem.
Fortunately, there is a simple way to think about it.
Rule #1: Don’t start by thinking about the model. Start by asking:
“What kind of prediction am I trying to make?”
Once you answer that question, the rest becomes almost automatic.
1. Binary Classification
Definition
Binary classification means there are only two possible classes.
Examples:
- Cat vs Dog
- Horse vs Human
- Happy vs Sad
- Spam vs Not Spam
- Cancer vs Healthy
Only one answer can be correct.
Image
│
▼
Cat OR Dog
Model Architecture
Since there are only two possible outcomes, the network only needs one output neuron.
model = tf.keras.Sequential([
...
tf.keras.layers.Dense(1, activation="sigmoid")
])
Why Sigmoid?
Sigmoid converts the output into a probability between 0 and 1.
Example:
0.02 → Cat
0.95 → Dog
Compile
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
Dataset Labels
0
1
or
False
True
When using image_dataset_from_directory():
label_mode="binary"
Memory Trick
2 Classes
↓
1 Output Neuron
↓
Sigmoid
↓
Binary Crossentropy
2. Multi-Class Classification (One Correct Answer)
Definition
There are more than two classes, but only one class is correct.
Examples:
- MNIST Digits (10 classes)
- Fashion MNIST (10 classes)
- Flowers (5 classes)
- Fruits (24 classes)
Example:
Image
↓
Apple
Banana
Orange
Grape
Only ONE is correct.
Model Architecture
If there are 10 classes:
tf.keras.layers.Dense(10, activation="softmax")
If there are 24 classes:
tf.keras.layers.Dense(24, activation="softmax")
Why Softmax?
Softmax converts outputs into probabilities whose total equals 1.0.
Example:
Dog 0.03
Cat 0.02
Horse 0.91
Cow 0.04
The model predicts Horse because it has the highest probability.
Case A — One-Hot Encoded Labels
Labels look like this:
Cat
[1 0 0]
Dog
[0 1 0]
Horse
[0 0 1]
Compile using:
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"]
)
Dataset:
label_mode="categorical"
Case B — Integer Labels
Sometimes labels are stored simply as numbers.
Example:
0
2
5
9
instead of
[1 0 0 0 0 ...]
[0 0 1 0 0 ...]
...
The output layer is exactly the same:
Dense(10, activation="softmax")
The only thing that changes is the loss function.
loss="sparse_categorical_crossentropy"
Dataset:
label_mode="int"
Memory Trick
One Correct Class
↓
Softmax
↓
One-Hot Labels
↓
Categorical Crossentropy
OR
Integer Labels
↓
Sparse Categorical Crossentropy
3. Multi-Label Classification
Definition
An image can belong to multiple classes at the same time.
Example:
Image
✓ Dog
✓ Car
✓ Tree
✗ Cat
✗ Person
Unlike multi-class classification, multiple answers can all be correct.
Model
If there are five possible labels:
Dense(5, activation="sigmoid")
Notice that we use Sigmoid, not Softmax.
Each neuron independently answers:
Yes or No?
Compile
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
Memory Trick
Many Answers
↓
Sigmoid
↓
Binary Crossentropy
4. Regression
Regression predicts a continuous numerical value, not a class.
Examples:
- House price
- Temperature
- Salary
- Stock price
- Age estimation
Example output:
$350,000
Model
Dense(1)
Notice that there is no activation function.
Compile
model.compile(
optimizer="adam",
loss="mse",
metrics=["mae"]
)
Other common loss functions include:
loss="mae"
or
loss="huber"
Memory Trick
Predict Number
↓
No Activation
↓
MSE / MAE
Understanding the Relationship
The easiest way to memorize TensorFlow is to start with the labels.
Binary Labels
0
1
↓
Dense(1)
Sigmoid
Binary Crossentropy
Integer Labels
0
3
9
15
↓
Dense(N)
Softmax
Sparse Crossentropy
One-Hot Labels
[0 0 1 0]
[1 0 0 0]
[0 1 0 0]
↓
Dense(N)
Softmax
Categorical Crossentropy
Multi-Label
[1 0 1 0 1]
↓
Dense(N)
Sigmoid
Binary Crossentropy
Continuous Numbers
23.4
125000
19.8
↓
Dense(1)
No Activation
MSE / MAE
Complete Cheat Sheet
| Problem | Example | Last Layer | Activation | Loss Function | Label Mode |
|---|---|---|---|---|---|
| Binary Classification | Cat vs Dog | Dense(1) | Sigmoid | Binary Crossentropy | binary |
| Multi-Class (One-Hot) | Fashion MNIST | Dense(N) | Softmax | Categorical Crossentropy | categorical |
| Multi-Class (Integer) | MNIST | Dense(N) | Softmax | Sparse Categorical Crossentropy | int |
| Multi-Label | Dog + Car + Tree | Dense(N) | Sigmoid | Binary Crossentropy | Multi-hot |
| Regression | House Price | Dense(1) | None | MSE / MAE | Numeric |
A Simple Way to Remember Everything
Whenever you’re building a neural network, ask these questions in order:
Question 1
Am I predicting a class or a number?
- Number → Regression
- Class → Classification
Question 2
How many correct answers can exist?
- One → Softmax
- Multiple → Sigmoid
Question 3
How are my labels stored?
- Integers → Sparse Categorical Crossentropy
- One-hot vectors → Categorical Crossentropy
- Binary labels → Binary Crossentropy
Final Mnemonic
2 Classes
→ 1 Neuron
→ Sigmoid
→ Binary Crossentropy
One Correct Class
→ N Neurons
→ Softmax
→ Categorical (or Sparse Categorical) Crossentropy
Many Correct Classes
→ N Neurons
→ Sigmoid
→ Binary Crossentropy
Predict a Number
→ 1 Neuron
→ No Activation
→ MSE / MAE
If you memorize these four patterns, you’ll be able to build the correct output layer and choose the appropriate loss function for almost every TensorFlow/Keras model you encounter.


There are 0 comments