0% found this document useful (0 votes)
27 views7 pages

Lab 6 ML

Uploaded by

kailashv1043
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

Lab 6 ML

Uploaded by

kailashv1043
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

RNN -Digit recognition using MNIST Data Sets using Python /Watson

notebook

Working on a Recurrent Neural Network (RNN) for digit recognition


using the MNIST dataset in an IBM Watson Studio Notebook is quite
similar to working on a Jupyter Notebook locally. However, IBM Watson
Studio offers an integrated environment with additional features like
easy dataset access, cloud storage, GPU support, and collaborative
features.
Here is a step-by-step guide on how to work on the Digit Recognition
using the MNIST Dataset in IBM Watson Studio, using a Python
notebook:

Step 1: Setting Up Your IBM Watson Studio Environment


1. Create a New Watson Studio Project:
o After logging in to the IBM Cloud, navigate to Watson
Studio.
o Create a new project by selecting the Create a Project
button.
o Choose Data Science as the project type (which is suitable
for machine learning tasks).
o You can either upload your existing files or use the default
settings for this exercise.
2. Create a New Notebook:
o Once inside your project, create a new notebook by selecting
+ New notebook.
o Select Python 3 as the language for your notebook.
o You can use either the Python 3.7 or Python 3.8
environment.
o You may also select the environment with GPU support for
faster training if available.
3. Set Up Your IBM Watson Studio Environment:
o IBM Watson Studio automatically provides necessary
libraries like TensorFlow, Keras, NumPy, and Matplotlib.
However, if you need to install additional libraries, you can
do so by running the following cell:
!pip install tensorflow matplotlib numpy
4. Access Data:
o IBM Watson Studio allows you to either upload data files
directly or pull datasets from cloud storage (e.g., IBM Cloud
Object Storage).
o For this project, MNIST is a built-in dataset in Keras, so we
don't need to upload it manually.

Step 2: Load the MNIST Dataset


The MNIST dataset is a collection of 60,000 training images and 10,000
testing images of handwritten digits (0-9). In IBM Watson Studio, you
can directly import the dataset using Keras (part of TensorFlow).
Code to Load MNIST:
import tensorflow as tf
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset (train and test data)


(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Check the shape of the datasets


print(f'Training data shape: {x_train.shape}')
print(f'Testing data shape: {x_test.shape}')

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).

Step 3: Preprocess the Data


We need to preprocess the data before feeding it into the RNN. This
includes normalizing the pixel values and reshaping the data to match
the RNN input format.
Code to Preprocess the Data:
# Normalize pixel values to between 0 and 1
x_train, x_test = x_train / 255.0, x_test / 255.0

# Reshape the data to match RNN input format


# MNIST images are 28x28, so we treat each row as a time step in the
sequence
x_train = x_train.reshape((x_train.shape[0], 28, 28)) # 28 time steps
(rows), each with 28 features (pixels)
x_test = x_test.reshape((x_test.shape[0], 28, 28)) # same for test data

print(f'New shape of x_train: {x_train.shape}')


print(f'New shape of x_test: {x_test.shape}')
Explanation:
 The pixel values are normalized to the range [0, 1] by dividing by
255.
 The data is reshaped so that each image has 28 time steps
(corresponding to the rows of the image) and 28 features per time
step (the pixels in each row).

Step 4: Build the RNN Model


Next, we'll build an RNN model for digit recognition. We'll use LSTM
(Long Short-Term Memory) cells for the RNN since they work well
with sequence data like the MNIST dataset.
Code to Build the RNN Model:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout

# Initialize the model


model = Sequential()

# Add LSTM layer


model.add(LSTM(128, input_shape=(28, 28), return_sequences=False))

# Add dropout layer to prevent overfitting


model.add(Dropout(0.2))

# Add output layer with 10 neurons (digits 0-9) and softmax activation
model.add(Dense(10, activation='softmax'))

# Summarize the model architecture


model.summary()
Explanation:
 LSTM Layer: This layer has 128 units, which is the number of
neurons. We treat the input as sequences of 28 time steps (rows)
where each step has 28 features (pixels in the row).
 Dropout Layer: Added to reduce overfitting by randomly setting
20% of the input units to zero during training.

 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.

Step 6: Train the Model


We now train the model using the training data and validate it on the
test data.
Code to Train the Model:
# Train the model
history = model.fit(x_train, y_train,
epochs=5,
batch_size=64,
validation_data=(x_test, y_test))

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.

Step 8: Test the Model on New Data


Finally, you can make predictions on new test images and visualize the
results.
Code to Test the Model and Visualize Predictions:
import matplotlib.pyplot as plt
import numpy as np

# Make a prediction for the first test image


prediction = model.predict(x_test[0:1])

# Get the predicted class (digit)


predicted_class = np.argmax(prediction)

# Plot the first test image


plt.imshow(x_test[0], cmap='gray')
plt.title(f'Predicted: {predicted_class}, True: {y_test[0]}')
plt.show()
Explanation:
 model.predict(x_test[0:1]): Predicts the class for the first test
image.
 np.argmax(prediction): Retrieves the predicted digit (the index
of the highest probability).
 plt.imshow: Displays the first test image, with the predicted and
true labels.

Step 9: Experiment and Improve the Model


You can further improve the model by experimenting with:
 Increasing the number of LSTM units or adding more LSTM
layers.
 Adding more dense layers.
 Training for more epochs.
 Using different types of RNNs (e.g., GRU instead of LSTM).

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.

You might also like