Lab 6 ML
Lab 6 ML
notebook
Explanation:
x_train and x_test are the images in the training and testing
datasets. These are numpy arrays with shape (60000, 28, 28) for
training, and (10000, 28, 28) for testing.
y_train and y_test are the corresponding labels for the images
(digits 0-9).
# Add output layer with 10 neurons (digits 0-9) and softmax activation
model.add(Dense(10, activation='softmax'))
Dense Output Layer: This layer has 10 neurons (one for each
digit), and uses softmax activation to predict the probabilities of
each digit.
Step 5: Compile the Model
After defining the model architecture, we need to compile the model.
We'll use the Adam optimizer and sparse categorical crossentropy
loss function, as we are performing multi-class classification.
Code to Compile the Model:
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Explanation:
Adam Optimizer: A popular optimization algorithm for training
deep learning models.
Loss Function: We use sparse_categorical_crossentropy since
our labels are integers (not one-hot encoded).
Metrics: We use accuracy to track the model's performance.
Explanation:
epochs=5: The model will be trained for 5 epochs (iterations over
the entire training data).
batch_size=64: The model will update its weights every 64
samples.
validation_data=(x_test, y_test): This allows the model to track
its performance on the test set during training.
Step 7: Evaluate the Model
After training, we evaluate the model on the test set to determine its
accuracy on unseen data.
Code to Evaluate the Model:
# Evaluate the model on test data
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc * 100:.2f}%')
Explanation:
The evaluate function will compute the loss and accuracy of the
model on the test data.
Conclusion
This guide walks you through using IBM Watson Studio to create a
simple RNN-based digit recognition model using the MNIST
dataset. You learned how to:
1. Set up a Python notebook on IBM Watson Studio.
2. Load and preprocess the MNIST data.
3. Build, compile, and train a simple LSTM-based RNN model.
4. Evaluate and visualize model performance.