# How can TensorFlow be used to define a loss function, an optimizer, train the model and evaluate it on the IMDB dataset in Python?

PythonServer Side ProgrammingProgramming

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

The ‘IMDB’ dataset contains reviews of over 50 thousand movies. This dataset is generally used with operations associated with Natural Language Processing.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Following is the code snippet to define a loss function, an optimizer, train the model and evaluate it on the IMDB dataset −

model.compile(loss=losses.BinaryCrossentropy(from_logits=True),
metrics=tf.metrics.BinaryAccuracy(threshold=0.0))
epochs = 10
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs)
loss, accuracy = model.evaluate(test_ds)

print("Loss is : ", loss)
print("Accuracy is : ", accuracy)

## Output

Epoch 1/10
625/625 [==============================] - 12s 19ms/step - loss: 0.6818 - binary_accuracy: 0.6130 - val_loss: 0.6135 - val_binary_accuracy: 0.7750
Epoch 2/10
625/625 [==============================] - 4s 7ms/step - loss: 0.5785 - binary_accuracy: 0.7853 - val_loss: 0.4971 - val_binary_accuracy: 0.8230
Epoch 3/10
625/625 [==============================] - 4s 7ms/step - loss: 0.4651 - binary_accuracy: 0.8372 - val_loss: 0.4193 - val_binary_accuracy: 0.8470
Epoch 4/10
625/625 [==============================] - 4s 7ms/step - loss: 0.3901 - binary_accuracy: 0.8635 - val_loss: 0.3732 - val_binary_accuracy: 0.8612
Epoch 5/10
625/625 [==============================] - 4s 7ms/step - loss: 0.3435 - binary_accuracy: 0.8771 - val_loss: 0.3444 - val_binary_accuracy: 0.8688
Epoch 6/10
625/625 [==============================] - 4s 7ms/step - loss: 0.3106 - binary_accuracy: 0.8877 - val_loss: 0.3255 - val_binary_accuracy: 0.8730
Epoch 7/10
625/625 [==============================] - 5s 7ms/step - loss: 0.2855 - binary_accuracy: 0.8970 - val_loss: 0.3119 - val_binary_accuracy: 0.8732
Epoch 8/10
625/625 [==============================] - 5s 7ms/step - loss: 0.2652 - binary_accuracy: 0.9048 - val_loss: 0.3027 - val_binary_accuracy: 0.8772
Epoch 9/10
625/625 [==============================] - 5s 7ms/step - loss: 0.2481 - binary_accuracy: 0.9125 - val_loss: 0.2959 - val_binary_accuracy: 0.8782
Epoch 10/10
625/625 [==============================] - 5s 7ms/step - loss: 0.2328 - binary_accuracy: 0.9161 - val_loss: 0.2913 - val_binary_accuracy: 0.8792
782/782 [==============================] - 10s 12ms/step - loss: 0.3099 - binary_accuracy: 0.8741
Loss is : 0.3099007308483124
Accuracy is : 0.8741199970245361

## Explanation

• Once the model has been built, it is compiled using the ‘compile’ function.

• The number of steps defined to train the model is 10 here.

• The ‘fit’ function is used to fit the data to the model that was built.

• The ‘evaluate’ function is used to compute the loss and the accuracy of the model on the test dataset.

• The values of loss and accuracy are displayed on the console.

Published on 19-Jan-2021 13:44:45