Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to train and compile a CNN model?
A convolutional neural network can be trained and compiled using TensorFlow's compile() and fit() methods. The model is first compiled with optimizer, loss function, and metrics, then trained using the fit() method with specified epochs.
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 convolutional layer is known as a convolutional neural network (CNN). CNNs have been used to produce great results for specific problems, such as image recognition and computer vision tasks.
We are using Google Colaboratory to run the code below. Google Colab helps run Python code over the browser and requires zero configuration with free access to GPUs.
Complete CNN Model Example
Let's create a complete CNN model for image classification using the CIFAR-10 dataset ?
import tensorflow as tf
from tensorflow import keras
# Load and preprocess CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
# Build CNN model
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10)
])
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=3, # Reduced for demo
validation_data=(test_images, test_labels))
The output shows the training progress across epochs ?
Compiling the model Training the model to fit the data Epoch 1/3 1563/1563 [==============================] - 45s 29ms/step - loss: 1.7408 - accuracy: 0.3557 - val_loss: 1.2260 - val_accuracy: 0.5509 Epoch 2/3 1563/1563 [==============================] - 44s 28ms/step - loss: 1.1928 - accuracy: 0.5751 - val_loss: 1.0800 - val_accuracy: 0.6159 Epoch 3/3 1563/1563 [==============================] - 43s 28ms/step - loss: 1.0330 - accuracy: 0.6396 - val_loss: 0.9791 - val_accuracy: 0.6562
Key Components
Model Compilation
The compile() method configures the model for training ?
model.compile(
optimizer='adam', # Optimization algorithm
loss='sparse_categorical_crossentropy', # Loss function
metrics=['accuracy'] # Metrics to track
)
Model Training
The fit() method trains the model on the dataset ?
history = model.fit(
train_images, train_labels, # Training data
epochs=10, # Number of training iterations
validation_data=(test_images, test_labels) # Validation data
)
Training Parameters
| Parameter | Description | Example Value |
|---|---|---|
| epochs | Number of complete passes through training data | 10 |
| batch_size | Number of samples per gradient update | 32 (default) |
| validation_data | Data for evaluating model during training | (test_images, test_labels) |
Understanding the Output
- loss: Training loss decreases as model learns
- accuracy: Training accuracy improves over epochs
- val_loss: Validation loss indicates generalization
- val_accuracy: Validation accuracy shows real performance
Conclusion
CNN models are compiled with compile() method specifying optimizer, loss, and metrics, then trained using fit() with epochs parameter. The training progress shows decreasing loss and increasing accuracy over iterations.
