-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_mpl_keras.py
96 lines (66 loc) · 2.85 KB
/
basic_mpl_keras.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from keras.layers import Input, Dense, Activation
from keras.models import Model
from keras.models import Sequential
from keras.datasets import mnist
from keras.utils import np_utils
from keras.metrics import binary_accuracy
from keras import backend as K
import numpy as np
np.set_printoptions(linewidth=10000, precision = 3, edgeitems= 100, suppress=True)
import matplotlib.pyplot as plt
plt.ion()
def noop():
pass
def first_way():
(X_train, y_train), (X_test, y_test) = mnist.load_data()
data = np.reshape(X_train, (-1, 28*28))
test_data = np.reshape(X_test, (-1, 28*28))
labels = np_utils.to_categorical(y_train, 10) #preprocess labels into one-hot encoddings.
test_labels = np_utils.to_categorical(y_test, 10).astype('float32') #preprocess labels into one-hot encoddings.
# This returns a tensor
inputs = Input(shape=(784,))
# a layer instance is callable on a tensor (That's the input)
# and returns a tensor (the output)
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
model.fit(data, labels) # starts training
#predicted_labels = model.predict(test_data)
scores = model.evaluate(test_data, test_labels)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
#print(K.eval(binary_accuracy(test_labels, predicted_labels)))
return
def second_way():
(X_train, y_train), (X_test, y_test) = mnist.load_data()
data = np.reshape(X_train, (-1, 28*28))
test_data = np.reshape(X_test, (-1, 28*28))
labels = np_utils.to_categorical(y_train, 10) #preprocess labels into one-hot encoddings.
test_labels = np_utils.to_categorical(y_test, 10) #preprocess labels into one-hot encoddings.
model = Sequential()
#by default kernel_initializer='glorot_uniform' which is Xavier's initialization
model.add(Dense(64, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(optimizer='sgd', loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
model.fit(data, labels) # starts training
#predicted_labels = model.predict(test_data)
#print(K.eval(binary_accuracy(test_labels, predicted_labels)))
scores = model.evaluate(test_data, test_labels)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
return
if __name__ == "__main__":
#Two ways of building a keras model.
first_way()
#second_way()