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

Deep Learning

The document outlines a process for building and training a convolutional neural network using the Keras library on the MNIST dataset. It includes data preprocessing steps, model architecture definition, compilation, training, evaluation, and prediction visualization. The final output includes test loss and accuracy metrics along with a visual representation of a test image.

Uploaded by

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

Deep Learning

The document outlines a process for building and training a convolutional neural network using the Keras library on the MNIST dataset. It includes data preprocessing steps, model architecture definition, compilation, training, evaluation, and prediction visualization. The final output includes test loss and accuracy metrics along with a visual representation of a test image.

Uploaded by

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

1)

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

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

3)
len(x_train)
len(y_train)
len(x_test)
len(y_test)

4)
num_of_trainigs=x_train.shape[0]
num_of_testings=x_test.shape[0]
img_width=28
img_height=28

5)
x_train=x_train.reshape(x_train.shape[0],img_height,img_width,1)
x_test=x_test.reshape(x_test.shape[0],img_height,img_width)

6)
input_shape=(img_height,img_width,1)

7)
x_train=x_train.astype('float32')
x_test=x_test.astype('float32')
x_train /=255
x_test /=255

8)num_classes=10
y_train=keras.utils. to_categorical(y_train, num_classes)
y_test=keras.utils. to_categorical(y_test, num_classes)

9)
model=Sequential()
model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation='softmax'))
10)
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers
.Adadelta(),metrics=['accuracy'])

11)
model.fit(x_train,y_train,batch_size=128,epochs=12,verbose=1,validation_data=(x_tes
t,y_test))

12)
score=model.evaluate(x_test, y_test, verbose=0)
print('test loss:',score[0])
print('test accuracy:', score[1])

13)
x_test.shape

14)
test_images_predict=model.predict(x_test)

15)
test_images_predict[9]

16)
import matplotlib.pyplot as plt
plt.matshow(x_test[9])

17)
import numpy as np
np.argmax(test_images_predict[9])

You might also like