
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to compile the model using Python?
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.
- Related Articles
- How can Tensorflow be used to compile the exported model using Python?
- How can Tensorflow be used with Estimator to compile the model using Python?
- How can Tensorflow be used to compile and fit the model using Python?
- How can Tensorflow and pre-trained model be used to compile the model using Python?
- How can Tensorflow be used to train and compile the augmented model?
- How can Tensorflow be used to train and compile a CNN model?
- How can Tensorflow be used to train the model using Python?
- How can Tensorflow used with the pre-trained model to compile the model?
- How can Tensorflow be used to export the model built using Python?
- How can Tensorflow be used to export the built model using Python?
- How can Tensorflow be used with the flower dataset to compile and fit the model?
- How can Tensorflow be used with Estimators to evaluate the model using Python?
- How can Tensorflow be used to evaluate a CNN model using Python?
- How can Tensorflow be used to create a sequential model using Python?
- How can Tensorflow be used to fit the data to the model using Python?

Advertisements