Your First Neural Network
Keras Basics
2 min read
Published Nov 17 2025
Guide Sections
Guide Comments
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:
- Load data
- Prepare data
- Build model
- Compile model
- Train model
- Evaluate model
- Predict using model
Load the MNIST Dataset
MNIST is built into Keras:
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:
Preprocess the Data
Neural networks work best with scaled data.
Convert pixel values (0–255) to floats (0–1):
Flatten images (28×28 → 784)
Our first model is a simple Dense network, so flattening is required.
Later sections (CNNs) will use unflattened images.
Build the Model
Use a basic Sequential API MLP (multi-layer perceptron):
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
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().
What each parameter means:
batch_size=32- Standard mini-batch gradient descent batch sizeepochs=5- Full passes through the training datavalidation_split=0.1- Reserves 10% of training data for validation monitoring
Training output shows:
- Loss
- Accuracy
- Validation loss
- Validation accuracy
Evaluate the Model
MNIST typically gives:
Making Predictions
Predict class probabilities:
Predict most likely classes:
Compare with true labels:
Visualising a Prediction

Understanding the Training History
The .fit() method returns a history object containing training curves:
Common keys:
lossaccuracyval_lossval_accuracy
Example plotting accuracy:















