Welcome to the fourth module of our AI Basics course! In this module, we will dive into practical exercises that will help you apply the concepts you've learned so far. Let's get started!

Exercises Overview

Here's a list of exercises you will be working on in this module:

  • Exercise 1: Implement a simple neural network using Python and TensorFlow.
  • Exercise 2: Train a machine learning model to classify images using the MNIST dataset.
  • Exercise 3: Optimize a machine learning model using hyperparameter tuning.

Exercise 1: Implement a Simple Neural Network

In this exercise, you will learn how to implement a simple neural network using Python and TensorFlow. This will help you understand the basic structure and functionality of neural networks.

Step 1: Install TensorFlow:

pip install tensorflow

Step 2: Write the neural network code:

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# ... (add your training data and model training code here)

For more detailed instructions and examples, check out our TensorFlow tutorials.

Exercise 2: Classify Images with MNIST Dataset

In this exercise, you will train a machine learning model to classify images using the MNIST dataset. This is a classic dataset that contains 60,000 training images and 10,000 testing images of handwritten digits.

Step 1: Load the MNIST dataset:

mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

Step 2: Preprocess the data:

train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

Step 3: Train the model:

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5)

For more information on the MNIST dataset and machine learning models, visit our MNIST tutorial.

Exercise 3: Hyperparameter Tuning

In this exercise, you will learn how to optimize a machine learning model using hyperparameter tuning. This is an essential step to improve the performance of your models.

Step 1: Choose a model and dataset: For this exercise, we will use the same neural network model from Exercise 1 and the MNIST dataset.

Step 2: Define the hyperparameters:

learning_rate = 0.001
epochs = 10
batch_size = 32

Step 3: Train the model with the defined hyperparameters:

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=epochs, batch_size=batch_size)

For more advanced techniques on hyperparameter tuning, read our hyperparameter tuning guide.

Remember to experiment with different hyperparameters to find the best combination for your model.

Additional Resources

If you need more help or want to dive deeper into AI, here are some additional resources:

Happy learning! 🎉