Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Training and Evaluating Models with Keras

In this lesson, we will explore how to train and evaluate a simple neural network model using Keras.


Data Preparation

Keras provides a variety of built-in datasets that can be used to practice training deep learning models.

The example below demonstrates the process of training a neural network model using the handwritten digits dataset (MNIST).


Loading MNIST Data
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Loading MNIST data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Data preprocessing: normalization and one-hot encoding
x_train = x_train / 255.0
x_test = x_test / 255.0

# Reshape images from 28x28 (2D arrays) to 784-dimensional vectors (adjust for neural network input)
x_train = x_train.reshape(-1, 28 * 28)
x_test = x_test.reshape(-1, 28 * 28)

# Convert number labels (answers) to one-hot encoded format (e.g., number 3 → [0,0,0,1,0,0,0,0,0,0])
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)

Code Explanation

  1. Load the handwritten digits dataset using mnist.load_data().

  2. x_train and x_test are arrays of training and test images, respectively.

  3. y_train and y_test are the labels (answers) for the training and test data, respectively.

  4. Normalize the pixel values of x_train and x_test to a range of 0 to 1, which enhances the effectiveness of neural network training.

  5. Reshape the images in x_train and x_test from 28x28 2D arrays to 784-dimensional vectors to match the neural network input format.

  6. Use the to_categorical() function to convert labels to their one-hot encoded format. For example, the number 3 is converted to [0,0,0,1,0,0,0,0,0,0].

  7. num_classes=10 specifies the number of classes for the digits 0 through 9.


Model Training

Now we will train the neural network model we previously defined.

Training the Model
# Train the model
model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

Key Parameters

  • epochs=10: Sets the number of epochs to 10, meaning the complete training data will be used 10 times for training.

  • batch_size=32: Updates weights using 32 samples per iteration.

  • validation_split=0.2: Uses 20% of the training data for validation.


Model Evaluation and Prediction

Once training is complete, the trained model can be evaluated using the test data, and predictions can be made on new input data.

Evaluating and Predicting with the Model
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)

# Make predictions
predictions = model.predict(x_test)

Code Explanation

  1. Use model.evaluate(x_test, y_test) to evaluate the model's performance on the test data.

  2. Call model.predict(x_test[:5]) to output predictions for some test data samples.


We have now learned the basic process of creating, training, and evaluating a neural network model using Keras.

In the next lesson, we'll solve a simple quiz based on what we've learned about Keras so far.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.