9951 explained code solutions for 126 technologies


python-kerasHow do I use the Python Keras module to develop a machine learning model?


Keras is a high-level API for building and training deep learning models in Python. To use the Keras module to develop a machine learning model, the following steps should be taken:

  1. Import the necessary libraries: Begin by importing the necessary libraries, such as keras, tensorflow, numpy, and matplotlib.
import keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
  1. Load the dataset: Load the dataset that you want to use for training your model.
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
  1. Preprocess the data: Preprocess the data by reshaping, normalizing, and one-hot encoding.
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /= 255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
  1. Define the model: Define the model architecture using the Sequential API.
model = keras.Sequential()
model.add(keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
  1. Compile the model: Compile the model by specifying the optimizer, loss function, and evaluation metric.
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  1. Train the model: Train the model by specifying the batch size and number of epochs.
model.fit(x_train, y_train, batch_size=128, epochs=10)
  1. Evaluate the model: Evaluate the model on the test set and print the accuracy.
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)

Output example

Test accuracy: 0.9915

Helpful links

Edit this code on GitHub