Keras for Classification (Binary & Multi-Class)

Keras Basics

2 min read

Published Nov 17 2025


11
0
0
0

KerasNeural NetworksPythonTensorFlow

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 length
max_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 keras
from tensorflow.keras import layers

model = 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 np
pred_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.0
x_test = x_test.astype("float32") / 255.0

Flatten 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 keras
from tensorflow.keras import layers

model = 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 np

pred = 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_matrix
import numpy as np

y_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)


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact