In this post, you will learn about how to set up Keras and get started with Keras, one of the most popular deep learning frameworks in current times which is built on top of TensorFlow 2.0 and can scale to large clusters of GPUs. You will also learn about getting started with hello world program with Keras code example. Here are some of the topics which will be covered in this post:
In this section, you will learn about how to set up Keras with Anaconda. Here are the steps:
#
# Command for setting up Keras
#
conda install -c conda-forge keras
from keras import datasets
#
# Load MNIST data
#
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
#
# Check the dataset loaded
#
train_images.shape, test_images.shape
Keras can be imported from tensorflow using the following code:
from tensorflow import keras
In this section, you will learn about training a very simplistic deep neural network (Hello World program) model for classifying the grayscale images of handwritten digits (28 × 28 pixels) into their 10 categories (0 through 9). In order to build the model, the MNIST dataset is used. MNIST dataset is a set of 60,000 training images and 10,000 test images, assembled by the National Institute of Standards and Technology (the NIST). The following are some of the key steps needed to be followed for training the deep neural network:
Here is the pipeline for training and testing the neural network model:
Here is how the code relating to setting up neural network architecture will look like.
from keras import models
from keras import layers
#
# Create network comprising of two layers
#
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,), name="layer1"))
network.add(layers.Dense(10, activation='softmax', name="layer2"))
In the hello world program, here is the network preparation code:
#
# Prepare the neural network
#
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
from keras.utils import to_categorical
#
# Prepare the training images and training labels
#
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
#
# Prepare the test images and test labels
#
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
test_labels = to_categorical(test_labels)
#
# Fit the neural network
#
network.fit(train_images, train_labels, epochs=5, batch_size=128)
#
# Evaluate the network performance
#
test_loss, test_acc = network.evaluate(test_images, test_labels)
#
# Print the accuracy
#
print('test_acc:', test_acc)
Here is the entire code for training deep neural network for classifying the grayscale images of handwritten digits (28 × 28 pixels) into their 10 categories (0 through 9).
from keras import datasets
from keras import models
from keras import layers
from keras.utils import to_categorical
#
# Load MNIST data
#
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
#
# Create network comprising of two dense (fully connected) layers
#
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,), name="layer1"))
network.add(layers.Dense(10, activation='softmax', name="layer2"))
#
# Prepare the training images and training labels
#
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
#
# Prepare the test images and test labels
#
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
test_labels = to_categorical(test_labels)
#
# Prepare the network
#
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
#
# Fit the neural network
#
network.fit(train_images, train_labels, epochs=5, batch_size=128)
#
# Evaluate the network performance
#
test_loss, test_acc = network.evaluate(test_images, test_labels)
#
# Print the accuracy
#
print('test_acc:', test_acc)
Here is what you learned in this post related to setting up Keras and getting started with Hello World program:
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…