-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist.py
58 lines (45 loc) · 1.68 KB
/
mnist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""MNIST.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/12a1YXrUCfIgib4Fke02qj5qE5y6tJg_h
"""
import keras
import numpy as np
from keras import datasets, layers, models
from keras.datasets import mnist
from matplotlib import pyplot
#loading
(train_X, train_y), (test_X, test_y) = mnist.load_data()
#shape of dataset
print('X_train: ' + str(train_X.shape))
print('Y_train: ' + str(train_y.shape))
print('X_test: ' + str(test_X.shape))
print('Y_test: ' + str(test_y.shape))
#plotting
from matplotlib import pyplot
for i in range(9):
pyplot.subplot(330 + 1 + i)
pyplot.imshow(train_X[i], cmap=pyplot.get_cmap('gray'))
pyplot.show()
train_X = train_X / 255
test_X = test_X / 255
train_X = train_X.reshape(-1,28,28,1)
test_X = test_X.reshape(-1,28,28,1)
convolutional_neural_network = models.Sequential([
layers.Conv2D(filters=25, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
convolutional_neural_network.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
convolutional_neural_network.fit(train_X, train_y, epochs=10)
convolutional_neural_network.evaluate(test_X, test_y)
y_predicted_by_model = convolutional_neural_network.predict(test_X)
y_predicted_by_model[0]
np.argmax(y_predicted_by_model[0])