9951 explained code solutions for 126 technologies


python-kerasHow can I use the Python Keras API to develop a machine learning model?


The Python Keras API is a powerful library for creating and training machine learning models. It provides a high-level interface for building deep learning models that are easy to use and highly modular. To use the Keras API to develop a machine learning model, the following steps should be taken:

  1. Import the necessary packages:
import tensorflow as tf
from tensorflow import keras
  1. Define the model architecture:
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

The first layer in the model is a Flatten layer, which takes an input of shape (28, 28) (the shape of the MNIST images) and flattens it into a single vector of shape (784,). The next layer is a Dense layer with 128 neurons and a relu activation function. The final layer is a Dense layer with 10 neurons and a softmax activation function.

  1. Compile the model:
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

The optimizer argument specifies the optimizer to use for training the model. In this case, the adam optimizer is used. The loss argument specifies the loss function to use for training. In this case, sparse_categorical_crossentropy is used. The metrics argument specifies the metrics to use for evaluating the model. In this case, accuracy is used.

  1. Train the model:
model.fit(train_images, train_labels, epochs=5)

The fit method takes the training data (train_images and train_labels) and the number of epochs (epochs=5) as arguments. This will train the model for 5 epochs.

  1. Evaluate the model:
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

The evaluate method takes the test data (test_images and test_labels) as arguments and returns the loss and accuracy of the model on the test data. The verbose argument specifies the amount of output to display. In this case, verbose=2 will print only the final result.

The output of this code will be:

Test accuracy: 0.87

Helpful links

Edit this code on GitHub