This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlpcnn.py
155 lines (140 loc) · 7.91 KB
/
mlpcnn.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import tensorflow_datasets as tfds
import tensorflow as tf
from tensorflow import keras
from keras.optimizers import SGD
import matplotlib.pyplot as plt
import numpy as np
import random
from skopt import gp_minimize
from skopt.plots import plot_convergence
from functools import partial
save_path = "/content/drive/MyDrive/LIACS/AML/mlp/"
(fmnist_X_train_full, fmnist_Y_train_full), (fmnist_X_test, fmnist_Y_test) = keras.datasets.fashion_mnist.load_data()
fmnist_X_valid, fmnist_X_train = fmnist_X_train_full[:5000] / 255.0, fmnist_X_train_full[5000:] / 255.0
fmnist_Y_valid, fmnist_Y_train = fmnist_Y_train_full[:5000], fmnist_Y_train_full[5000:]
def train_mlp(x):
learning_rate, momentum, decay_rate, batchsize = x[0],x[1],x[2],x[3]
print(learning_rate, momentum, decay_rate, batchsize)
mlp = keras.models.Sequential()
mlp.add(keras.layers.Flatten(input_shape=[28,28]))
mlp.add(keras.layers.Dense(512, activation="relu"))
mlp.add(keras.layers.Dense(256, activation="relu"))
mlp.add(keras.layers.Dense(10,activation="softmax"))
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate)
mlp.compile(loss="sparse_categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
history = mlp.fit(fmnist_X_train, fmnist_Y_train, verbose=0, batch_size=batchsize, epochs = 30, validation_data=(fmnist_X_valid,fmnist_Y_valid))
return history.history['loss'][-1]
def train_mlp_randomsearch(learning_rate, momentum, decay_rate, batchsize):
mlp = keras.models.Sequential()
mlp.add(keras.layers.Flatten(input_shape=[28,28]))
mlp.add(keras.layers.Dense(512, activation="relu"))
mlp.add(keras.layers.Dense(256, activation="relu"))
mlp.add(keras.layers.Dense(10,activation="softmax"))
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate)
mlp.compile(loss="sparse_categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
history = mlp.fit(fmnist_X_train, fmnist_Y_train, verbose=0, batch_size=batchsize, epochs = 30, validation_data=(fmnist_X_valid,fmnist_Y_valid))
return history.history['loss'][-1]
def train_cnn(x):
learning_rate, momentum, decay_rate, batchsize = x[0],x[1],x[2],x[3]
DefaultConv2D = partial(keras.layers.Conv2D,kernel_size=3, activation='relu', padding="SAME")
cnn = keras.models.Sequential([
DefaultConv2D(filters=64, kernel_size=7, input_shape=[28,28,1]),
keras.layers.MaxPooling2D(pool_size=2),
DefaultConv2D(filters=128),
DefaultConv2D(filters=128),
keras.layers.MaxPooling2D(pool_size=2),
DefaultConv2D(filters=256),
DefaultConv2D(filters=256),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Flatten(),
keras.layers.Dense(units=128, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=64, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=10, activation='softmax'),
])
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate)
cnn.compile(loss="sparse_categorical_crossentropy", optimizer = sgd, metrics=["accuracy"])
history = cnn.fit(fmnist_X_train, fmnist_Y_train, verbose=0, batch_size=batchsize, epochs = 30, validation_data=(fmnist_X_valid,fmnist_Y_valid))
return history.history['loss'][-1]
def train_cnn_randomsearch(learning_rate, momentum, decay_rate, batchsize):
DefaultConv2D = partial(keras.layers.Conv2D,kernel_size=3, activation='relu', padding="SAME")
cnn = keras.models.Sequential([
DefaultConv2D(filters=64, kernel_size=7, input_shape=[28,28,1]),
keras.layers.MaxPooling2D(pool_size=2),
DefaultConv2D(filters=128),
DefaultConv2D(filters=128),
keras.layers.MaxPooling2D(pool_size=2),
DefaultConv2D(filters=256),
DefaultConv2D(filters=256),
keras.layers.MaxPooling2D(pool_size=2),
keras.layers.Flatten(),
keras.layers.Dense(units=128, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=64, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=10, activation='softmax'),
])
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate)
cnn.compile(loss="sparse_categorical_crossentropy", optimizer = sgd, metrics=["accuracy"])
history = cnn.fit(fmnist_X_train, fmnist_Y_train, verbose=1, batch_size=batchsize, epochs = 30, validation_data=(fmnist_X_valid,fmnist_Y_valid))
return history.history['loss'][-1]
#Train MLP with BO
for run in range(10):
res = gp_minimize(train_mlp, # the function to minimize
[(1e-3,1e-2), # learningrate
(0.0,1.0), # momentum
(1e-6,1e-5), # decay rate
(16,200)], # batchsize
acq_func="EI", # the acquisition function
n_calls=50, # the number of evaluations of f
n_initial_points=5, # the number of random initialization points
)
fn = "MLP-Result-RUN-" + str(run + 1)
np.save(save_path + fn, np.array(res.func_vals))
#Train MLP with Random Search
for i in range(10):
randomsearch_result = []
minFx = 100
for j in range (50):
learning_rate = np.random.uniform(1e-3,1e-2)
momentum = np.random.uniform(0.0,1.0)
decay_rate = np.random.uniform(1e-6,1e-5)
batchsize = np.random.randint(16,200)
loss = train_mlp_randomsearch(learning_rate, momentum, decay_rate, batchsize)
if loss < minFx:
minFx = loss
randomsearch_result.append(minFx)
fn = "RandomMLP-Result-RUN-" + str(i+1)
np.save(save_path + fn, np.array(randomsearch_result))
#reshape data for CNN
fmnist_X_train = fmnist_X_train.reshape(fmnist_X_train.shape[0], 28, 28, 1)
fmnist_X_valid = fmnist_X_valid.reshape(fmnist_X_valid.shape[0], 28, 28, 1)
#Train CNN with BO
for run in range(10):
res = gp_minimize(train_cnn, # the function to minimize
[(1e-3,1e-2), # learningrate
(0.0,1.0), # momentum
(1e-6,1e-5), # decay rate
(16,200)], # batchsize
acq_func="EI", # the acquisition function
n_calls=50, # the number of evaluations of f
n_initial_points=5, # the number of random initialization points
)
fn = "CNN-Result-RUN-" + str(run + 1)
np.save(save_path + fn, np.array(res.func_vals))
#Train CNN with Random Search
for i in range(10):
randomsearch_result = []
minFx = 100
for j in range (50):
learning_rate = np.random.uniform(1e-3,1e-2)
momentum = np.random.uniform(0.0,1.0)
decay_rate = np.random.uniform(1e-6,1e-5)
batchsize = np.random.randint(16,200)
loss = train_cnn_randomsearch(learning_rate, momentum, decay_rate, batchsize)
if loss < minFx:
minFx = loss
randomsearch_result.append(minFx)
fn = "Random_CNN-Result-RUN-" + str(i+1)
np.save(save_path + fn, np.array(randomsearch_result))