Keras is a Python library that helps developers in making neural networks. This library provides functions that are very feasible in defining the model's architecture. Due to the Keras functions' feasibility, people from various domains use it to train the model on their dataset. In this Answer, we will discuss the steps to train a neural network with the help of this library.
As you know, training neural networks includes pre-processing the data, defining an algorithm, and evaluating the model based on the training.
The model architecture we are going to build is sequential. In this architecture, the neural network is designed with stacked layers. The convolutional neural network and the dense layer network are types of sequential architecture.
model = tf.keras.Sequential()
We are going to use an
mnist = tf.keras.datasets.mnist(train_I, train_T), (test_I, test_T) = mnist.load_data()# Normalize the datatrain_I = train_I / 255.0test_I = test_I / 255.0
We defined a neural network model with three layers: input (64 units, ReLU), hidden (128 units, ReLU), and output (10 units, Softmax). It's designed for multi-class classification, particularly recognizing handwritten digits from the MNIST dataset.
model.add(layers.Dense(units=64, activation='relu', input_shape=(input_shape,)))model.add(layers.Dense(units=128, activation='relu'))model.add(layers.Dense(units=10, activation='softmax'))
The code below compiles the model using the Adam optimizer, categorical cross-entropy loss function, and accuracy metric for evaluation.
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
The model is trained using the training data train_data
and corresponding labels train_labels
. The training is performed in batches of size 32 for 10 epochs. A validation split of 20% is used for validation during training.
model.fit(train_data, train_labels, batch_size=32, epochs=10, validation_split=0.2)
The model is evaluated on the test_data
and corresponding labels test_labels
. The evaluation results, including the test loss and test accuracy, are stored in test_loss
and test_accuracy
variables, respectively. Finally, the test accuracy is printed using print("Test Accuracy:", test_accuracy)
.
test_loss, test_accuracy = model.evaluate(test_data, test_labels)print("Model Accuracy:", test_accuracy)
By summing all the above functions, we have our model built. Run the code below for further understanding.
import tensorflow as tf from tensorflow.keras import layers # Load the MNIST dataset mnist = tf.keras.datasets.mnist (train_data, train_target), (test_data, test_target) = mnist.load_data() # Prepare your data # Normalize the pixel values to range between 0 and 1 train_data = train_data / 255.0 test_data = test_data / 255.0 # Define the architecture of the model model = tf.keras.Sequential() # Add layers to the model model.add(layers.Flatten(input_shape=(28, 28))) # Flatten the 28x28 image into a 1D array model.add(layers.Dense(units=128, activation='relu')) model.add(layers.Dense(units=10, activation='softmax')) # Compile the model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train the model model.fit(train_data, train_target, batch_size=32, epochs=5, validation_split=0.2) # Evaluate the model test_loss, test_accuracy = model.evaluate(test_data, test_target) print("Test Accuracy:", test_accuracy)
Keras is a popular deep-learning library that simplifies the process of building neural networks. We use Keras to build a neural network for recognizing handwritten digits from the MNIST dataset. We compile the model with the help of the Adam optimizer, categorical cross-entropy loss function, and the accuracy metric. After that, we can train on the training data and evaluate the accuracy of the testing data.
What does the compile() function in Keras do?
Preprocess the input data
Train the model
Evaluate the model on test data
Specify the optimizer, loss function, and metrics for training
Free Resources