forked from lukas/ml-class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeras-cnn-1.py
More file actions
30 lines (23 loc) · 850 Bytes
/
keras-cnn-1.py
File metadata and controls
30 lines (23 loc) · 850 Bytes
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
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from keras.utils import np_utils
(X_train, y_train), (X_test, y_test) = mnist.load_data()
img_width=28
img_height=28
X_train /= 255
X_test /= 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
# build model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(img_width, img_height,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train)