0% found this document useful (0 votes)
21 views2 pages

Assignment2MLlab (Nausheen)

The document implements a CNN for MNIST image classification. It loads the MNIST dataset, reshapes and normalizes the images, builds a sequential CNN model with Conv2D, MaxPooling2D, Dropout and Dense layers, compiles the model using Adadelta optimizer and accuracy metric, and fits the model to the training data over 20 epochs with a batch size of 128. The output shows the model was trained on the MNIST dataset to classify handwritten digits.

Uploaded by

Nausheen Fatima
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)
21 views2 pages

Assignment2MLlab (Nausheen)

The document implements a CNN for MNIST image classification. It loads the MNIST dataset, reshapes and normalizes the images, builds a sequential CNN model with Conv2D, MaxPooling2D, Dropout and Dense layers, compiles the model using Adadelta optimizer and accuracy metric, and fits the model to the training data over 20 epochs with a batch size of 128. The output shows the model was trained on the MNIST dataset to classify handwritten digits.

Uploaded by

Nausheen Fatima
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/ 2

Ques1-Implement CNN for MNIST image classification.

Upload screenshot of your


implementation: -code -output screen

Code-:

import keras
mnist = keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
import matplotlib.pyplot as plt

plt.imshow(x_train[0]) #training image
plt.show()
print(y_train[0]) #what the number is supposed to be

#Reshape the data
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)

#Normalize the pixel values from a scale out of 255 to a scale out of 1
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu'
,
                              input_shape=input_shape))

model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(10, activation='softmax'))

model.compile(“adam”,
              optimizer=keras.optimizers.Adadelta(),

NAUSHEEN FATIMA 1
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=128,
          epochs=20,
          validation_data=(x_test, y_test))
Output:-

NAUSHEEN FATIMA 2

You might also like