-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwind_hyperopt.py
More file actions
151 lines (112 loc) · 3.99 KB
/
wind_hyperopt.py
File metadata and controls
151 lines (112 loc) · 3.99 KB
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
import datetime
from keras.layers import Dense
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import preprocessing.wind_preprocessing as ds
from keras.models import Sequential
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, rand
WINDOW_SIZE = 30
space = {
'units1': hp.choice('units1', [2, 4, 8, 16]),
'activation1': hp.choice('activation1', ['relu', 'sigmoid']),
'units2': hp.choice('units2', [2, 4, 8, 16]),
'activation2': hp.choice('activation2', ['relu', 'sigmoid']),
'optimizer': hp.choice('optimizer', ['adam', 'adadelta']),
'batch_size': hp.choice('batch_size', [16, 32, 64])
}
time_series = ds.read_time_series('data/wind/wind_patzcuaro_10m_complete.csv', 24236, 29996)
time_series_normalized, norm_min, norm_max = ds.normalize_min_max(time_series)
x, y = ds.slide_window(time_series_normalized, WINDOW_SIZE, 1)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.30, random_state=1337)
# last_params = {}
global_trials = Trials()
"""def param_extractor(params):
global last_params
last_params = params
print(params)
return {'loss': 0.5, 'status': STATUS_OK}"""
def run_model(params) -> float:
print("Training model with: ")
print(params)
model = Sequential()
model.add(Dense(params['units1'], input_shape=(WINDOW_SIZE,), activation=params['activation1']))
model.add(Dense(params['units2'], activation=params['activation2']))
model.add(Dense(1))
model.compile(optimizer=params['optimizer'], loss='mean_squared_error', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=20, batch_size=params['batch_size'], verbose=0,
shuffle=1,
validation_data=(x_test, y_test)).history
y_predicted = model.predict(x_test)
mse = mean_squared_error(y_test, y_predicted)
print("Test MSE = ", mse)
return mse
def get_next_params(current_trials):
trial_parameters = {}
def extract_params(params):
nonlocal trial_parameters
trial_parameters = params
return {'loss': 0.5, 'status': STATUS_OK}
cache_trials = Trials()
cache_trials.insert_trial_docs(current_trials.trials)
# cache_trials.refresh()
fmin(extract_params, space, algo=tpe.suggest, trials=cache_trials, max_evals=1,
return_argmin=False, show_progressbar=False)
trial = cache_trials.trials[-1]
return trial, trial_parameters
for i in range(5):
t, tp = get_next_params(global_trials)
mse = run_model(tp)
t['result']['loss'] = mse
t['refresh_time'] = datetime.datetime.now()
global_trials.insert_trial_doc(t)
global_trials.refresh()
print(global_trials.best_trial)
"""
trials = Trials()
print("First recommendation")
fmin(param_extractor, space, algo=tpe.suggest, trials=trials, max_evals=1, return_argmin=False)
new_trial = trials.trials[-1]
new_trial['result']['loss'] = 0.4
print(new_trial)
global_trials.insert_trial_doc(new_trial)
global_trials.refresh()
print("Trial added to global trials")
test_trials = Trials()
test_trials.insert_trial_docs(global_trials.trials)
print("Inserted all trials from global")"""
#
# # Run model and get evaluation
# mse = run_model(last_params)
#
# # Build dict
#
# data = {
# 'book_time': datetime.datetime.now(),
# 'exp_key': None,
# 'misc': {'cmd': ('domain_attachment', 'FMinIter_Domain'),
# 'idxs': {},
# 'tid': 1,
# 'vals': {},
# 'workdir': None},
# 'owner': None,
# 'refresh_time': datetime.datetime.now(),
# 'result': {'loss': mse, 'status': STATUS_OK},
# 'spec': None,
# 'state': 2,
# 'tid': 1,
# 'version': 0
# }
#
# global_trials.insert_trial_doc(data)
#
# # Add to global trials
#
# print("Second recommendation")
# fmin(param_extractor, space, algo=tpe.suggest, trials=global_trials, max_evals=1)
#
# # best = fmin(function, space, algo=tpe.suggest, trials=trials, max_evals=5)
#
# """print(best)
# print(trials.best_trial)
#
# print("Done")"""