A convolutional neural network can be trained and compiled using the ‘train’ method and the ‘fit’ method respectively. The ‘epoch’ value is provided in the ‘fit’ method.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.
A neural network that contains at least one layer is known as a convolutional layer. Convolutional neural networks have been used to produce great results for a specific kind of problems, such as image recognition.
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("Compiling the model") model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) print("Training the model to fit the data") history = model.fit(train_images, train_labels, epochs=10,validation_data=(test_images, test_labels))
Code credit: https://www.tensorflow.org/tutorials/images/cnn
Output
Compiling the model Training the model to fit the data Epoch 1/10 1563/1563 [==============================] - 70s 44ms/step - loss: 1.7408 - accuracy: 0.3557 - val_loss: 1.2260 - val_accuracy: 0.5509 Epoch 2/10 1563/1563 [==============================] - 67s 43ms/step - loss: 1.1928 - accuracy: 0.5751 - val_loss: 1.0800 - val_accuracy: 0.6159 Epoch 3/10 1563/1563 [==============================] - 68s 43ms/step - loss: 1.0330 - accuracy: 0.6396 - val_loss: 0.9791 - val_accuracy: 0.6562 Epoch 4/10 1563/1563 [==============================] - 66s 43ms/step - loss: 0.9197 - accuracy: 0.6782 - val_loss: 0.9488 - val_accuracy: 0.6677 Epoch 5/10 1563/1563 [==============================] - 65s 42ms/step - loss: 0.8388 - accuracy: 0.7043 - val_loss: 0.9090 - val_accuracy: 0.6851 Epoch 6/10 1563/1563 [==============================] - 66s 42ms/step - loss: 0.7755 - accuracy: 0.7279 - val_loss: 0.8694 - val_accuracy: 0.6944 Epoch 7/10 1563/1563 [==============================] - 66s 42ms/step - loss: 0.7107 - accuracy: 0.7494 - val_loss: 0.9152 - val_accuracy: 0.6929 Epoch 8/10 1563/1563 [==============================] - 65s 42ms/step - loss: 0.6674 - accuracy: 0.7649 - val_loss: 0.8613 - val_accuracy: 0.7045 Epoch 9/10 1563/1563 [==============================] - 66s 42ms/step - loss: 0.6288 - accuracy: 0.7771 - val_loss: 0.8788 - val_accuracy: 0.7026 Epoch 10/10 1563/1563 [==============================] - 66s 42ms/step - loss: 0.5913 - accuracy: 0.7953 - val_loss: 0.8884 - val_accuracy: 0.7053
Explanation
- The model is compiled.
- The next step is to train the model to fit the training data.
- The number of steps to train the data is 10.