Final Project and Review
Duration: 10 min
This module serves as the culmination of your TensorFlow and Keras journey, where you will apply your knowledge to a final project. You will review key concepts such as neural networks, CNNs, RNNs, transfer learning, and hyperparameter tuning. This module will also guide you through the deployment of your model, ensuring you have a complete understanding of the end-to-end machine learning workflow.
Final Project Overview
The final project will involve creating a complete machine learning pipeline using TensorFlow and Keras. You will choose a dataset, preprocess it, build and train a neural network model, perform hyperparameter tuning, and finally deploy the model. This project will demonstrate your ability to apply all the concepts learned throughout the course.
import tensorflow as tf
from tensorflow.keras import layers, models
# Load and preprocess dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build CNN model
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10)
])
# Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10,
validation_data=(x_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f'Test accuracy: {test_acc}')Epoch 1/10
1563/1563 [==============================] - 15s 9ms/step - loss: 1.5762 - accuracy: 0.4199 - val_loss: 1.3774 - val_accuracy: 0.5093
...
Epoch 10/10
1563/1563 [==============================] - 14s 9ms/step - loss: 0.7359 - accuracy: 0.7492 - val_loss: 1.1234 - val_accuracy: 0.6543
313/313 - 1s - loss: 1.1234 - accuracy: 0.6543
Test accuracy: 0.6543Deployment of the Model
Once your model is trained and tuned, the final step is to deploy it. Deployment involves saving the model and loading it into a production environment where it can make predictions on new data. TensorFlow provides several ways to save and load models, including the SavedModel format and HDF5 format.
import tensorflow as tf
from tensorflow.keras import models
# Save the model
model.save('my_model')
# Load the model
loaded_model = models.load_model('my_model')
# Make predictions with the loaded model
predictions = loaded_model.predict(x_test[:5])
print(predictions)💡 Tip: Ensure that the environment where you deploy the model has the same TensorFlow version and dependencies as the environment where the model was trained to avoid compatibility issues.
❓ What is the purpose of the final project in this module?
❓ Which format is commonly used to save and deploy TensorFlow models?