-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1_b.py
52 lines (34 loc) · 1.53 KB
/
exercise1_b.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
import os, os.path
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
import keras.layers as l
import keras.optimizers as o
from keras.layers import Dense, Flatten
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
def exercise1_b(activation_functions, layers):
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = (X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32'))/255
X_test = (X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32'))/255
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
X_train_train = X_train[0:55000]
X_validate = X_train[55000:60000]
Y_train_train = Y_train[0:55000]
Y_validate = Y_train[55000:60000]
results = []
for af in activation_functions:
for layer in layers:
model = Sequential()
model.add(Flatten())
for n in range(0, layer):
model.add(Dense(32, activation=af))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=o.SGD(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train_train, Y_train_train, epochs=3, validation_data = (X_validate, Y_validate))
score, acc = model.evaluate(X_test, Y_test, verbose=0)
results.append([af, layer, str(acc*100) + "%"])
print(results)
exercise1_b(['relu', 'tanh', 'sigmoid'], [5, 20, 40])