-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1 quest2.py
96 lines (83 loc) · 3.7 KB
/
hw1 quest2.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
import keras
from keras import layers
from keras.layers import Dropout
from keras import models
from keras.datasets import mnist
from keras.utils import to_categorical
import os
import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.wrappers import scikit_learn
import numpy
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
"""数据加载"""
path = '/Users/wangzhaohan/Desktop/ML-Program/mnist.npz'
file = np.load(path)
train_images, train_labels = file['x_train'], file['y_train']
test_images, test_labels = file['x_test'], file['y_test']
"""数据处理"""
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
"""模型搭建"""
def create_model(number_of_layer, number_of_neurons, dropout_value, l1_l2_regularization):
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
model.add(layers.Dropout(dropout_value))
i = number_of_layer - 2
while i > 0:
if l1_l2_regularization == 1:
model.add(
layers.Dense(number_of_neurons, activation='relu', kernel_regularizer=keras.regularizers.l1(0.01)))
if l1_l2_regularization == 2:
model.add(
layers.Dense(number_of_neurons, activation='relu', kernel_regularizer=keras.regularizers.l2(0.01)))
if l1_l2_regularization == 3:
model.add(
layers.Dense(number_of_neurons, activation='relu', kernel_regularizer=keras.regularizers.l1_l2(0.01)))
if l1_l2_regularization == 0:
model.add(
layers.Dense(number_of_neurons, activation='relu'))
i -= 1
model.add(layers.Dense(10, activation='softmax'))
# model.summary()
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# model.fit(train_images, train_labels, epochs=5, batch_size=64)
return model
number_of_layers_range = [2, 3, 4]
number_of_neurons_range = [10, 25, 50, 100]
dropout_range = [0, 0.1, 0.25, 0.5]
l1_l2_range = [0, 1, 2, 3]
epochs_range = [5, 10, 15, 20]
if __name__ == '__main__':
n = 0
result_array = []
for a in number_of_layers_range:
for b in number_of_neurons_range:
for c in dropout_range:
for d in l1_l2_range:
for e in epochs_range:
network = create_model(a, b, c, d)
network.fit(train_images, train_labels, epochs=e, batch_size=128, verbose=0)
test_loss, test_acc = network.evaluate(test_images, test_labels)
result_array.append([a, b, c, d, e, test_loss, test_acc])
n += 1
print(n, '/768')
for i in result_array:
print(i)
result_array = np.array(result_array)
ranked_result_array = result_array[result_array[:, 6].argsort()]
for i in ranked_result_array:
print(i)
print('Accuracy:', ranked_result_array[len(ranked_result_array) - 1][6],
'\nLoss:', ranked_result_array[len(ranked_result_array) - 1][5],
'\nNumber of layers:', ranked_result_array[len(ranked_result_array) - 1][0],
'\nNumber of neurons:', ranked_result_array[len(ranked_result_array) - 1][1],
'\nDropout parameter:', ranked_result_array[len(ranked_result_array) - 1][2],
'\nL1/L2 regularization type:', ranked_result_array[len(ranked_result_array) - 1][3],
'\nEpochs:', ranked_result_array[len(ranked_result_array) - 1][4])