-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_experiment.py
133 lines (112 loc) · 5.06 KB
/
run_experiment.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
"""
Experiment for Anfis Sandbox
"""
from Models import myanfis
import numpy as np
import time
import Datagenerator.datagenerator as gen
import tensorflow as tf
import datetime
import os
from tensorflow.keras.callbacks import TensorBoard
from tensorboard.plugins.hparams import api as hp
##############################################################################
# Initializing HyperParameters
n_input = [2, 3, 5]
HP_n_input = hp.HParam('n_ipnut', hp.Discrete(
n_input)) # no. of Regressors
# no. of fuzzy memberships
HP_n_memb = hp.HParam('n_memb', hp.Discrete([2, 3, 4, 5]))
HP_memb_func = hp.HParam('memb_func', hp.Discrete(
['gaussian', 'gbellmf'])) # 'gaussian' / 'bell'
# sgd / adam / ...
HP_optimizer = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd']))
# mse / mae / huber_loss / hinge / ...
HP_loss = hp.HParam('loss', hp.Discrete(['mse', 'mae', 'huber_loss']))
METRIC = 'mse'
# Model Parameters
batch_size = 16
n_epochs = 50
# Data Parameters
n_obs = 2000
lag = 1
data_id = 1 # 0 = mackey / 1 = sinc /
# 2 = Three-Input Nonlin /
# 3 = markov switching
# 4 = TAR / # 5 = STAR
# General Parameters
core = '/device:CPU:0' # '/device:CPU:0' // '/device:GPU:0'
show_core_usage = False # True / False
##############################################################################
# find out which devices your operations and tensors are assigned to
tf.debugging.set_log_device_placement(show_core_usage)
# create logs for the training process that records the losses
with tf.summary.create_file_writer('logs/exp_anfis').as_default():
hp.hparams_config(hparams=[HP_n_input, HP_n_memb, HP_memb_func, HP_optimizer, HP_loss],
metrics=[hp.Metric(METRIC, display_name='mse')]
)
# A function To Train And Validate
def train_test_model(logdir, hparams):
# create model
fis = myanfis.ANFIS(n_input=hparams[HP_n_input],
n_memb=hparams[HP_n_memb],
batch_size=batch_size,
memb_func=hparams[HP_memb_func],
name='myanfis'
)
# compile model
fis.model.compile(optimizer=hparams[HP_optimizer],
loss=hparams[HP_loss],
metrics=['mse'], # TODO: add ['mae', 'mse']
)
# fit model
fis.fit(X_train[:, :hparams[HP_n_input]], y_train,
epochs=n_epochs,
batch_size=batch_size,
validation_data=(X_test[:, :hparams[HP_n_input]], y_test),
callbacks=[TensorBoard(log_dir=logdir, histogram_freq=1, profile_batch=100000000), # log metrics
hp.KerasCallback(logdir, hparams), # log hparams
]
)
_, evaluation = fis.model.evaluate(X_test[:, :hparams[HP_n_input]], y_test)
return evaluation
# A function to log the training process
def run(logdir, hparams):
with tf.summary.create_file_writer(logdir).as_default():
hp.hparams(hparams) # record the values used in this trial
evaluation = train_test_model(logdir, hparams)
tf.summary.scalar(METRIC, evaluation, step=1)
# Generate Data (same for every session)
X, X_train, X_test, y, y_train, y_test = gen.gen_data(
data_id, n_obs, max(n_input), batch_size, lag)
# Start experiment
start_time = time.time()
session_num = 0
with tf.device(core): # CPU / GPU
for n_input in HP_n_input.domain.values:
for n_memb in HP_n_memb.domain.values:
for memb_func in HP_memb_func.domain.values:
for optimizer in HP_optimizer.domain.values:
for loss in HP_loss.domain.values:
# generate hyperparameters
hparams = {HP_n_input: n_input,
HP_n_memb: n_memb,
HP_memb_func: memb_func,
HP_optimizer: optimizer,
HP_loss: loss
}
# generate log path
session_name = f'-{gen.get_data_name(data_id)}_N{n_input}_M{n_memb}_{memb_func}_{optimizer}_{loss}'
logdir = os.path.join("logs", "exp_anfis",
datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
+ session_name
)
# run session
print(f'--- Starting trial: {session_num}')
print({h.name: hparams[h] for h in hparams})
run(logdir, hparams)
session_num += 1
end_time = time.time()
time = np.round(end_time - start_time, 2)
print(f'Time for experiment: {np.round(end_time - start_time,2)} seconds')
print(f'number of experimental settings was {session_num} ')