The created model in Tensorflow can be compiled using the ‘compile’ method. The loss is calculated using the ‘SparseCategoricalCrossentropy’ method.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
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") model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) print("The architecture of the model") model.summary()
Code credit: https://www.tensorflow.org/tutorials/images/classification
Output
The model is being compiled The architecture of the model Model: "sequential_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= rescaling_1 (Rescaling) (None, 180, 180, 3) 0 _________________________________________________________________ conv2d_6 (Conv2D) (None, 180, 180, 16) 448 _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 90, 90, 16) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 90, 90, 32) 4640 _________________________________________________________________ max_pooling2d_5 (MaxPooling2 (None, 45, 45, 32) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 45, 45, 64) 18496 _________________________________________________________________ max_pooling2d_6 (MaxPooling2 (None, 22, 22, 64) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 30976) 0 _________________________________________________________________ dense_2 (Dense) (None, 128) 3965056 _________________________________________________________________ dense_3 (Dense) (None, 5) 645 ================================================================= Total params: 3,989,285 Trainable params: 3,989,285 Non-trainable params: 0 _________________________________________________________________
Explanation
- The optimizers.Adam optimizer and losses.SparseCategoricalCrossentropy loss function is used.
- Thetraining and validation accuracy for every training epoch can be viewed by passing the metrics argument.
- Once the model is compiled, the summary of the architecture is displayed using the 'summary' method.