Keras for Classification (Binary & Multi-Class)
Keras Basics
2 min read
Published Nov 17 2025, updated Aug 17 2026
Guide Sections
Guide Comments
Classification forms the backbone of many deep learning applications:
- Sentiment analysis
- Image recognition
- Spam filtering
- Disease classification
- Quality control
- Product categorisation
We’ll use two standard real-world datasets:
- IMDB Reviews → Binary classification
- Fashion-MNIST → Multi-class classification
Binary Classification - (IMDB Sentiment)
IMDB Dataset Overview
Dataset contains:
- 25,000 movie reviews (train)
- 25,000 movie reviews (test)
- Labels: 0 = negative, 1 = positive
- Reviews are pre-tokenised as integer sequences
Load data:
from tensorflow.keras import datasets(x_train, y_train), (x_test, y_test) = datasets.imdb.load_data(num_words=10000)We limit vocabulary to 10,000 most common words.
Preprocess the IMDB Data
Reviews are variable-length sequences.
We pad them so all sequences have equal length.
from tensorflow.keras.preprocessing.sequence import pad_sequences# reasonable fixed sequence lengthmax_len = 200 x_train = pad_sequences(x_train, maxlen=max_len)x_test = pad_sequences(x_test, maxlen=max_len)Build a Binary Classification Model
We’ll use:
- An Embedding layer to convert integers → dense word vectors
- A GlobalAveragePooling1D to reduce the sequence
- A Dense output with sigmoid activation
from tensorflow import kerasfrom tensorflow.keras import layersmodel = keras.Sequential([ layers.Embedding(input_dim=10000, output_dim=32, input_length=max_len), layers.GlobalAveragePooling1D(), layers.Dense(32, activation='relu'), layers.Dense(1, activation='sigmoid')])Why this architecture?
- Embedding: learns word meanings
- Pooling: simple sequence reduction
- Sigmoid output: perfect for binary classification
Compile the Binary Model
Binary classification uses:
- Loss: binary_crossentropy
- Activation: sigmoid
- Metric: accuracy
model.compile( optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])Train the Model
history = model.fit( x_train, y_train, epochs=5, batch_size=32, validation_split=0.2)Training is fast because the model is lightweight.
Evaluate the Model
model.evaluate(x_test, y_test)Typical accuracy: ~85–88%
More advanced models (LSTM/Conv1D) reach 90–92%, which you’ll learn in a later section.
Predictions
import numpy as nppred_prob = model.predict(x_test[:1])[0][0]print("Predicted probability:", pred_prob)print("Predicted class:", int(pred_prob > 0.5))Multi-Class Classification - (Fashion-MNIST)
Fashion-MNIST Overview
This dataset is like MNIST but with clothing items:
- T-shirts
- Trousers
- Pullovers
- Dresses
- Coats
- Sandals
- Shirts
- Sneakers
- Bags
- Ankle boots
Load dataset:
from tensorflow.keras import datasets(x_train, y_train), (x_test, y_test) = datasets.fashion_mnist.load_data()Preprocess the Data
Normalise pixel values:
x_train = x_train.astype("float32") / 255.0x_test = x_test.astype("float32") / 255.0Flatten for dense layers:
x_train = x_train.reshape(-1, 28*28)x_test = x_test.reshape(-1, 28*28)Build a Multi-Class Classifier
Multi-class classifiers end with softmax output and integer labels.
from tensorflow import kerasfrom tensorflow.keras import layersmodel = keras.Sequential([ layers.Dense(256, activation='relu', input_shape=(784,)), layers.Dense(128, activation='relu'), layers.Dense(10, activation='softmax')])Compile the Multi-Class Model
model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])Key points:
- softmax output
- sparse_categorical_crossentropy for integer labels (0–9)
Train the Model
history = model.fit( x_train, y_train, epochs=10, batch_size=32, validation_split=0.1)Expect around 88–92% accuracy without CNNs. (CNNs will significantly improve in a later section.)
Evaluate
model.evaluate(x_test, y_test)Making Predictions
import numpy as nppred = model.predict(x_test[:1])pred_class = np.argmax(pred)print("Prediction:", pred_class)print("True label:", y_test[0])Confusion Matrix
We can use scikit-learn’s confusion matrix:
from sklearn.metrics import confusion_matriximport numpy as npy_pred = np.argmax(model.predict(x_test), axis=1)cm = confusion_matrix(y_test, y_pred)print(cm)Key Differences Between Binary and Multi-Class Models
Binary Classification | Multi-Class Classification |
Output: 1 unit | Output: N units (classes) |
Activation: sigmoid | Activation: softmax |
Loss: binary_crossentropy | Loss: sparse_categorical_crossentropy |
Prediction: >0.5 threshold | Prediction: argmax(probabilities) |