How can Tensorflow be used with the flower dataset to compile and fit the model?

The flower dataset can be compiled and fit to the model using the ‘compile’ and ‘fit’ methods respectively. To the ‘fit’ method, the training dataset as well as the validation dataset are passed as parameters. The number of epochs is also defined in the ‘fit’ method.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will be using the flowers dataset, which contains images of several thousands of flowers. It contains 5 sub-directories, and there is one sub-directory for every class.  

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.

print("The model is being compiled")
print("The model is being fit to the data")

Code credit:


The model is being compiled
The model is being fit to the data
Epoch 1/3
92/92 [==============================] - 107s 1s/step - loss: 1.3570 - accuracy: 0.4183 - val_loss: 1.0730 - val_accuracy: 0.5913
Epoch 2/3
92/92 [==============================] - 101s 1s/step - loss: 1.0185 - accuracy: 0.5927 - val_loss: 1.0041 - val_accuracy: 0.6199
Epoch 3/3
92/92 [==============================] - 95s 1s/step - loss: 0.8691 - accuracy: 0.6529 - val_loss: 0.9985 - val_accuracy: 0.6281
<tensorflow.python.keras.callbacks.History at 0x7f2cdcbbba90>


  • Once the layers are created and the data is trained, the next step is to compile the model that has been built.
  • Once the compiling is done, the model is fit to the input dataset.
  • The validation accuracy is a low value in comparison to the training accuracy value.
  • This means that our model overfits.