-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_tuner_FMNIST_without_reg_test.py
184 lines (116 loc) · 5.18 KB
/
keras_tuner_FMNIST_without_reg_test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import keras_tuner
import tensorflow as tf
from tensorflow import keras
from keras.callbacks import TensorBoard
import numpy as np
import random
import os
import csv
# cd Documents/
# cd populationDescent/
# python3 -m venv ~/venv-metal
# source ~/venv-metal/bin/activate
# python3 -m keras_tuner_FMNIST_without_reg_test
# grad_steps = 25 trials * 2 executions each trial * 782 batches per execution + (5 * 782) for final training = 43000 steps
# Fashion-MNIST dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
sample_shape = train_images[0].shape
img_width, img_height = sample_shape[0], sample_shape[1]
input_shape = (img_width, img_height, 1)
# Reshape data
train_images = train_images.reshape(len(train_images), input_shape[0], input_shape[1], input_shape[2])
test_images = test_images.reshape(len(test_images), input_shape[0], input_shape[1], input_shape[2])
# normalizing data
train_images, test_images = train_images / 255.0, test_images / 255.0
# splitting data into validation/test set
validation_images, validation_labels = test_images[0:5000], test_labels[0:5000]
test_images, test_labels = test_images[5000:], test_labels[5000:]
def build_model(hp):
model = keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, kernel_size = 3, strides=(2,2), dilation_rate=(1,1), activation='relu', input_shape = (28, 28, 1)))
model.add(tf.keras.layers.Conv2D(128, kernel_size = 3, strides=(2,2), dilation_rate=(1,1), activation='relu'))
model.add(tf.keras.layers.Conv2D(256, kernel_size = 3, dilation_rate=(1,1), activation='relu'))
model.add(tf.keras.layers.Flatten())
# no regularization
# hp_reg = hp.Float("reg_term", min_value=1e-5, max_value=1e-1)
model.add(tf.keras.layers.Dense(1024, activation = "relu"))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation = "softmax"))
hp_learning_rate = hp.Float("lr", min_value=1e-4, max_value=1e-2, sampling="log")
model.compile(
optimizer=keras.optimizers.legacy.Adam(learning_rate=hp_learning_rate),
loss=keras.losses.SparseCategoricalCrossentropy(),
metrics=["accuracy"],
)
return model
# seed:
def set_seeds(seed):
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
tf.random.set_seed(seed)
np.random.seed(seed)
def set_global_determinism(seed):
set_seeds(seed=seed)
os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
tf.config.threading.set_inter_op_parallelism_threads(1)
tf.config.threading.set_intra_op_parallelism_threads(1)
# SEED = [5, 15, 24, 34, 49, 60, 74, 89, 97, 100]
SEED = [101, 150, 200]
for seed in SEED:
set_global_determinism(seed=seed)
print(seed), print("")
import time
start_time = time.time()
max_trials = 25
model_num = "4 without reg"
# define tuner
print("random search")
tuner = keras_tuner.RandomSearch(
hypermodel=build_model,
objective="val_accuracy",
max_trials=max_trials,
executions_per_trial=2,
overwrite=True,
project_name="FMNIST: %s" % SEED
)
with tf.device('/device:GPU:0'):
# search
tuner.search(train_images, train_labels, validation_data=(validation_images, validation_labels), batch_size=64)
# retrieve and train best model
best_hps = tuner.get_best_hyperparameters(5)
model = build_model(best_hps[0])
# Use early stopping
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2)
# TRAIN Model
print("")
print("TRAINING")
train_epochs = 20
hist = model.fit(train_images, train_labels, batch_size= 64, validation_data=(validation_images, validation_labels), epochs=train_epochs, callbacks=[callback])
# getting history
print("history"), print(hist.history["val_loss"])
grad_steps = [i * 936 for i in hist.history['val_loss']]
print(""), print("grad_steps"), print(grad_steps)
time_lapsed = time.time() - start_time
# evaluating model on test and train data
batch_size = 64
np.random.seed(0)
eIndices = np.random.choice(4999, size = (batch_size*25, ), replace=False)
random_batch_train_images, random_batch_train_labels, random_batch_test_images, random_batch_test_labels = train_images[eIndices], train_labels[eIndices], test_images[eIndices], test_labels[eIndices]
print(""), print(""), print("Evaluating models on test data after randomization")
# evaluating on train, test images
lossfn = tf.keras.losses.SparseCategoricalCrossentropy()
# train_loss = lossfn(random_batch_train_labels, model(random_batch_train_images))
# test_loss = lossfn(random_batch_test_labels, model(random_batch_test_images))
train_loss = model.evaluate(random_batch_train_images, random_batch_train_labels)[0]
test_loss = model.evaluate(random_batch_test_images, random_batch_test_labels)[0]
print("unnormalized train loss: %s" % train_loss)
print("unnormalized test loss: %s" % test_loss)
# print("normalized (1/1+loss) test loss: %s" % ntest_loss)
model_num = "4_with_reg"
# writing data to excel file
data = [[test_loss, train_loss, model_num, max_trials, time_lapsed, seed]]
with open('/Users/abhi/Documents/research_data/keras_tuner_random_search_FMNIST.csv', 'a', newline = '') as file:
writer = csv.writer(file)
writer.writerows(data)