Keras : Simpler Alternative to Tensorflow | by Jyoti | May, 2024


In this blog, we’ll learn the basics of Keras API – a simple and compact API for creating neural networks.

The most popular deep learning framework in the world is TensorFlow. It is incredibly powerful, efficient, and widely used in industry. However, a downside to TensorFlow is that the code can be a bit complex, especially when setting up a model for training or evaluation.

A simpler alternative to TensorFlow is Keras. The Keras API is easier to use than TensorFlow, allowing us to create, train, and evaluate a deep learning model with considerably less code. Interestingly, Keras is often run on top of TensorFlow, acting as a wrapper API to make the coding simpler.

Keras is excellent for building small deep learning projects, but TensorFlow is still the preferred framework for industry-level projects since it provides more utilities and efficient training mechanisms.

In this section, we will learn to initialize an MLP model in Keras

A. Building the MLP

In Keras, every neural network model is an instance of the Sequential object. This acts as the container of the neural network, allowing us to build the model by stacking multiple layers inside the Sequential object.

The most commonly used Keras neural network layer is the Dense layer. This represents a fully-connected layer in the neural network, and it is the most important building block of an MLP model.

When building a model, we start off by initializing a Sequential object. We can initialize an empty Sequentialobject and add layers onto the model using the add function, or we can directly initialize the Sequential object with a list of layers.

#Adding two Dense layers to a Sequential model.
model = Sequential()
layer1 = Dense(5, input_dim=4) #input data has shape (batch_size, 4)
#(where batch_size is the data's batch size,
#decided at runtime).
model.add(layer1)
layer2 = Dense(3, activation='relu')
model.add(layer2)
#Initializing a Sequential model with two Dense layers.
layer1 = Dense(5, input_dim=4)
layer2 = Dense(3, activation='relu')
model = Sequential([layer1, layer2])

The Dense object takes in a single required argument, which is the number of neurons in the fully-connected layer. The activation keyword argument specifies the activation function for the layer (the default is no activation). In the code snippets above, we used no activation for layer1 and ReLU activation for layer2.

The first layer of the Sequential model represents the input layer. Therefore, in the first layer we need to specify the feature dimension of the input data for the model, which we do with the input_dim keyword argument.

Now, we learn to complete a multilayer perceptron model in Keras by adding the final layers to the MLP for multiclass classification

A. Final layer activation

The TensorFlow cross-entropy loss functions applies the sigmoid/softmax function to the output of the MLP to produce logits.

In Keras, the cross-entropy loss functions only calculate cross-entropy, without applying the sigmoid/softmax function to the MLP output. Therefore, we can have the model directly output class probabilities instead of logits (i.e. we apply sigmoid/softmax activation to the output layer).

#Creating an MLP model for binary classification (sigmoid activation).
model = Sequential()
layer1 = Dense(5, activation='relu', input_dim=4)
model.add(layer1)
layer2 = Dense(1, activation='sigmoid')
model.add(layer2)
#Creating an MLP model for multiclass classification with 3 classes (softmax activation).
model = Sequential()
layer1 = Dense(5, input_dim=4)
model.add(layer1)
layer2 = Dense(3, activation='softmax')
model.add(layer2)

Now, we will learn to configure a Keras model for training.

A. Configuring for training

When it comes to configuring the model for training, Keras shines with its simplicity. A single call to the compile function allows to set up all the training requirements for the model.

The function takes in a single required argument, which is the optimizer to use, e.g. ADAM.

The two main keyword arguments to know are loss and metrics. The loss keyword argument specifies the loss function to use.

For binary classification, we set the value to binary_crossentropy, which is the binary cross-entropy function and For multiclass classification, we set the value to categorical_crossentropy, which is the multiclass cross-entropy function.

The metrics keyword argument takes in a list of strings, representing metrics we want to track during training and evaluation. For classification, we only need to track the model loss (which is tracked by default) and the classification accuracy.

#Creates an MLP model for binary classification and configures it for training.
model = Sequential()
layer1 = Dense(5, activation='relu', input_dim=4)
model.add(layer1)
layer2 = Dense(1, activation='sigmoid')
model.add(layer2)
model.compile('adam',
loss='binary_crossentropy',
metrics=['accuracy']) # classified classification accuracy
# as the metric to track.

In the final section of this blog, we will Learn how to train, evaluate, and make predictions with a Keras model. Basically, we will Understand the facets of model execution for Keras models

A. Training

After configuring a Keras model for training, it only takes a single line of code to actually perform the training. We use the Sequential model’s fit function to train the model on input data and labels.

The first two arguments of the fit function are the input data and labels, respectively. Unlike TensorFlow (where we need to use tensor objects for any sort of data), we can simply use NumPy arrays as the input arguments for the fit function.

The training batch size can be specified using the batch_size keyword argument (the default is a batch size of 32). We can also specify the number of epochs, i.e. number of full run-throughs of the dataset during training, using the epochs keyword argument (the default is 1 epoch).

model = Sequential()
layer1 = Dense(200, activation='relu', input_dim=4)
model.add(layer1)
layer2 = Dense(200, activation='relu')
model.add(layer2)
layer3 = Dense(3, activation='softmax')
model.add(layer3)
model.compile('adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

# predefined multiclass dataset
train_output = model.fit(data, labels,
batch_size=20, epochs=5)

Output
Train on 150 samples
Epoch 1/5
20/150 [===>……………………..]
- ETA: 2s - loss: 1.2927 - accuracy: 0.0500
150/150
[==========================] - 0s 2ms/sample - loss: 1.0654 - accuracy: 0.3333
Epoch 2/5 20/150 [===>……………………..]
- ETA: 0s - loss: 0.8686 - accuracy: 0.5500
150/150
[==========================] - 0s 75us/sample - loss: 0.7530 - accuracy: 0.7200
Epoch 3/5 20/150 [===>……………………..]
- ETA: 0s - loss: 0.6444 - accuracy: 0.7000
150/150
[=========================] - 0s 64us/sample - loss: 0.6105 - accuracy: 0.6800
Epoch 4/5 20/150 [===>……………………..]
- ETA: 0s - loss: 0.5068 - accuracy: 0.9500
[==============================] - 0s 63us/scy: 0.8067 Epoch 5/5 20/150 [===>… vnfdkkjnfd…………………..] - ETA: 0s - loss: 0.4957 - accuracy: 0.6500

The console output of the training is logged for each epoch. Notice that both the loss and classification accuracy per epoch is measured (since we configured the model to track classification accuracy).

The output of the fit function is a History object, which records the training metrics. The object’s historyattribute is a dictionary that contains the metric values at each epoch of training.

print(train_output.history)
Output:
{'loss': [0.9612995227177937, 0.6760292808214824, 0.5184778571128845,
0.43344079454739887, 0.3726089795430501],
'accuracy': [0.45333335, 0.75333333, 0.6933333, 0.87333333, 0.91333336]}

The history for the train_output object from the previous code snippet.

B. Evaluation

Evaluating a trained Keras model is just as simple as training it. We use the Sequential model’s evaluatefunction, which also takes in data and labels (NumPy arrays) as its first two arguments.

Calling the evaluate function will evaluate the model over the entire input dataset and labels. The function returns a list containing the evaluation loss as well as the values for each metric specified during model configuration.

# predefined eval dataset
print(model.evaluate(eval_data, eval_labels))
Output:

1/4 [======>.......................] - ETA: 0s - loss: 0.0468 - accuracy: 1.0000
4/4 [==============================] - 0s 1ms/step - loss: 0.3284 - accuracy: 0.9200
[0.3284008800983429, 0.9200000166893005]

C. Predictions

Finally, we can make predictions with a Keras model using the predict function. The function takes in a NumPy array dataset as its required argument, which represents the data observations that the model will make predictions for.

The output of the predict function is the output of the model. That means for classification, the predictfunction is the model’s class probabilities for each data observation.

# 3 new data observations
# Prints Class probabilities for multiclass new data observations.
print('{}'.format(repr(model.predict(new_data))))
Output

array([[0.95961547, 0.03414856, 0.00623599],
[0.00179799, 0.15039763, 0.84780437],
[0.04558854, 0.6302088 , 0.32420266]], dtype=float32)

In the code above, the model returned class probabilities for the 3 new data observations. Based on the probabilities, the first observation would be classified as class 0, the second observation would be classified as class 2, and the third observation would be classified as class 1.

Q. What is the main benefit of Keras over TensorFlow?

  • The API is simpler to use, especially for model training and evaluation

Q. What does the Dense object represent in Keras?

  • A fully-connected neural network layer

Q. For multiclass classification, what is the output of the predict function of a Keras Sequentialmodel?

  • A 2-D NumPy array of class probabilities

Anndd with this, let’s give a pat on our back as we have reached end of this blog !!



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*