A Convolutional Neural Network (CNN) is a powerful deep learning architecture designed for processing grid-like data (e.g., images). This tutorial will walk you through creating a basic CNN using TensorFlow and Keras.
Step 1: Import Libraries 📚
import tensorflow as tf
from tensorflow.keras import layers, models
Step 2: Build the Model 🏗️
model = models.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
layers.MaxPooling2D((2,2)),
layers.Flatten(),
layers.Dense(10, activation='softmax')
])
Step 3: Compile and Train 🔄
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train on MNIST dataset
model.fit(train_images, train_labels, epochs=5)
Step 4: Evaluate and Predict 🧪
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")
# Make predictions
predictions = model.predict(test_images)
For a deeper dive into CNN applications, check out our Advanced CNN Techniques tutorial. Happy coding! 🚀