Your First Neural Network

Keras Basics

2 min read

Published Nov 17 2025


11
0
0
0

KerasNeural NetworksPythonTensorFlow

In this section, you’ll build your first complete Keras model using a real dataset: MNIST handwritten digits.


This section demonstrates all core steps you'll use in every deep learning project:

  1. Load data
  2. Prepare data
  3. Build model
  4. Compile model
  5. Train model
  6. Evaluate model
  7. Predict using model





Load the MNIST Dataset

MNIST is built into Keras:

from tensorflow.keras import datasets

(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()

MNIST consists of:

  • 60,000 training images
  • 10,000 test images
  • Each image is 28×28 grayscale
  • Each label is an integer from 0 to 9

Inspect shapes:

print(x_train.shape)
# (60000, 28, 28)
print(y_train.shape)
# (60000,)





Preprocess the Data

Neural networks work best with scaled data.


Convert pixel values (0–255) to floats (0–1):

x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

Flatten images (28×28 → 784)

Our first model is a simple Dense network, so flattening is required.

x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

Later sections (CNNs) will use unflattened images.






Build the Model

Use a basic Sequential API MLP (multi-layer perceptron):

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(128, activation='relu', input_shape=(784,)),
    layers.Dense(10, activation='softmax')
])

Explanation:

  • 128 hidden units → moderate-sized hidden layer
  • ReLU activation → standard for hidden layers
  • Softmax output → required for multi-class classification





Compile the Model

Compile defines:

  • Loss function
  • Optimiser
  • Metrics
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

Why these choices?

  • Adam: good general-purpose optimizer
  • Sparse categorical crossentropy: correct loss for multi-class integer labels
  • Accuracy: practical metric for classification





Train the Model

Training is done via .fit().

history = model.fit(
    x_train,
    y_train,
    batch_size=32,
    epochs=5,
    validation_split=0.1
)

What each parameter means:

  • batch_size=32 - Standard mini-batch gradient descent batch size
  • epochs=5 - Full passes through the training data
  • validation_split=0.1 - Reserves 10% of training data for validation monitoring

Training output shows:

  • Loss
  • Accuracy
  • Validation loss
  • Validation accuracy





Evaluate the Model

test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)

MNIST typically gives:

Test accuracy: ~0.97





Making Predictions

Predict class probabilities:

pred_probs = model.predict(x_test[:5])
print(pred_probs[0])

Predict most likely classes:

import numpy as np

pred_classes = np.argmax(pred_probs, axis=1)
print(pred_classes)

Compare with true labels:

print(y_test[:5])





Visualising a Prediction

import matplotlib.pyplot as plt

plt.imshow(x_test[0].reshape(28,28), cmap='gray')
plt.title(f"Predicted: {pred_classes[0]}, True: {y_test[0]}")
plt.show()

keras mnist prediction image





Understanding the Training History

The .fit() method returns a history object containing training curves:

history.history.keys()

Common keys:

  • loss
  • accuracy
  • val_loss
  • val_accuracy

Example plotting accuracy:

plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.legend(["Train", "Validation"])
plt.show()

keras mnist prediction validation





The Full Script (Complete Working Example)

from tensorflow.keras import datasets
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np

# Load data
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()

# Preprocess
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

# Build
model = keras.Sequential([
    layers.Dense(128, activation='relu', input_shape=(784,)),
    layers.Dense(10, activation='softmax')
])

# Compile
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

# Train
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.1)

# Evaluate
model.evaluate(x_test, y_test)

# Predict
pred_probs = model.predict(x_test[:1])
pred_class = np.argmax(pred_probs)
print("Prediction:", pred_class, "Label:", y_test[0])

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