dcekit 2.13.5__tar.gz → 2.14.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dcekit-2.13.5/dcekit.egg-info → dcekit-2.14.1}/PKG-INFO +1 -1
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/optimization/__init__.py +2 -0
- dcekit-2.14.1/dcekit/optimization/opt_dnn_hyperparams_class.py +374 -0
- {dcekit-2.13.5 → dcekit-2.14.1/dcekit.egg-info}/PKG-INFO +1 -1
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit.egg-info/SOURCES.txt +1 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/setup.py +1 -1
- {dcekit-2.13.5 → dcekit-2.14.1}/LICENSE +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/LICENSE.txt +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/MANIFEST.in +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/README.md +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/design_of_exmeriments/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/design_of_exmeriments/bayesian_optimization.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/generative_model/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/generative_model/gmr.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/generative_model/gtm.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/generative_model/vbgmr.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/just_in_time/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/just_in_time/jitpls.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/just_in_time/lwpls.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/learning/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/learning/ensemble_learning.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/learning/semi_supervised_learning.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/learning/transfer_learning.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/optimization/fast_opt_svr_hyperparams.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/optimization/iot.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/optimization/opt_dnn_hyperparams.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/sampling/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/sampling/kennard_stone.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/validation/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/validation/applicability_domain.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/validation/metrics.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/validation/validation.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/variable_selection/__init__.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/variable_selection/basic_variable_selection.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/variable_selection/local_interpretation.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit/variable_selection/variable_importance.py +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit.egg-info/dependency_links.txt +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit.egg-info/requires.txt +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/dcekit.egg-info/top_level.txt +0 -0
- {dcekit-2.13.5 → dcekit-2.14.1}/setup.cfg +0 -0
|
@@ -10,3 +10,5 @@ from .iot import iot_obj_func
|
|
|
10
10
|
from .iot import iot
|
|
11
11
|
from .opt_dnn_hyperparams import bo_dnn_hyperparams
|
|
12
12
|
from .opt_dnn_hyperparams import gs_dnn_hyperparams
|
|
13
|
+
from .opt_dnn_hyperparams_class import bo_dnn_hyperparams_class
|
|
14
|
+
from .opt_dnn_hyperparams_class import gs_dnn_hyperparams_class
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*- %reset -f
|
|
2
|
+
"""
|
|
3
|
+
@author: Hiromasa Kaneko
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from sklearn.neural_network import MLPClassifier
|
|
11
|
+
from sklearn.model_selection import StratifiedKFold, cross_val_predict
|
|
12
|
+
from sklearn.gaussian_process import GaussianProcessRegressor
|
|
13
|
+
from sklearn.gaussian_process.kernels import WhiteKernel, RBF, ConstantKernel, Matern, DotProduct
|
|
14
|
+
from sklearn.metrics import accuracy_score
|
|
15
|
+
from scipy.stats import norm
|
|
16
|
+
import warnings
|
|
17
|
+
warnings.simplefilter('ignore')
|
|
18
|
+
|
|
19
|
+
def bo_dnn_hyperparams_class(x, y,
|
|
20
|
+
hidden_layer_sizes_candidates=[(50,), (100,), (50, 10), (100, 10), (50, 50, 10), (100, 100, 10), (50, 50, 50, 10), (100, 100, 100, 10)],
|
|
21
|
+
activation_candidates=['identity', 'logistic', 'tanh', 'relu'],
|
|
22
|
+
alpha_candidates=10 ** np.arange(-6, -1, dtype=float),
|
|
23
|
+
learning_rate_init_candidates=10 ** np.arange(-5, 0, dtype=float),
|
|
24
|
+
parameter=5, bo_iteration_number=15, display_flag=False):
|
|
25
|
+
"""
|
|
26
|
+
Bayesian optimization of scikit-learn[MLPClassifier]-based deep neural network (DNN) hyperparameters
|
|
27
|
+
|
|
28
|
+
Optimize DNN hyperparameters based on Bayesian optimization and cross-validation
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
x : numpy.array or pandas.DataFrame
|
|
33
|
+
(autoscaled) m x n matrix of X-variables of training data,
|
|
34
|
+
m is the number of training sammples and
|
|
35
|
+
n is the number of X-variables
|
|
36
|
+
y : numpy.array or pandas.DataFrame
|
|
37
|
+
(autoscaled) m x 1 vector of a Y-variable of training data
|
|
38
|
+
hidden_layer_sizes_candidates : list
|
|
39
|
+
vector of candidates of hidden_layer_sizes in MLPClassifier
|
|
40
|
+
activation_candidates : list
|
|
41
|
+
vector of candidates of activation in MLPClassifier
|
|
42
|
+
alpha_candidates : numpy.array or list
|
|
43
|
+
vector of candidates of alpha in MLPClassifier
|
|
44
|
+
learning_rate_init_candidates : numpy.array or list
|
|
45
|
+
vector of candidates of learning_rate_init in MLPClassifier
|
|
46
|
+
parameter : int
|
|
47
|
+
"fold_number"-fold cross-validation in cross-validation, and k in midknn
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
optimal_hidden_layer_sizes : tuple
|
|
52
|
+
optimized hidden_layer_sizes in MLPClassifier
|
|
53
|
+
optimal_activation : str
|
|
54
|
+
optimized activation in MLPClassifier
|
|
55
|
+
optimal_alpha : float
|
|
56
|
+
optimized alpha in MLPClassifier
|
|
57
|
+
optimal_learning_rate_init : float
|
|
58
|
+
optimized learning_rate_init in MLPClassifier
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
# 実験計画法の条件
|
|
62
|
+
doe_number_of_selecting_samples = 15 # 選択するサンプル数
|
|
63
|
+
doe_number_of_random_searches = 100 # ランダムにサンプルを選択して D 最適基準を計算する繰り返し回数
|
|
64
|
+
# BOの設定
|
|
65
|
+
bo_iterations = np.arange(0, bo_iteration_number + 1)
|
|
66
|
+
bo_gp_fold_number = 5 # BOのGPを構築するためのcvfold数
|
|
67
|
+
bo_number_of_selecting_samples = 1 # 選択するサンプル数
|
|
68
|
+
#bo_regression_method = 'gpr_kernels' # gpr_one_kernel', 'gpr_kernels'
|
|
69
|
+
bo_regression_method = 'gpr_one_kernel' # gpr_one_kernel', 'gpr_kernels'
|
|
70
|
+
bo_kernel_number = 2 # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
|
|
71
|
+
acquisition_function = 'PTR' # 'PTR', 'PI', 'EI', 'MI'
|
|
72
|
+
target_range = [1, 100] # PTR
|
|
73
|
+
relaxation = 0.01 # EI, PI
|
|
74
|
+
delta = 10 ** -6 # MI
|
|
75
|
+
|
|
76
|
+
x = np.array(x)
|
|
77
|
+
y = np.array(y)
|
|
78
|
+
alpha_candidates = np.array(alpha_candidates)
|
|
79
|
+
learning_rate_init_candidates = np.array(learning_rate_init_candidates)
|
|
80
|
+
|
|
81
|
+
hidden_layer_sizes_indexes = [i for i in range(len(hidden_layer_sizes_candidates))]
|
|
82
|
+
activation_candidates_indexes = [i for i in range(len(activation_candidates))]
|
|
83
|
+
all_candidate_combinations = []
|
|
84
|
+
for hidden_layer_sizes_index in hidden_layer_sizes_indexes:
|
|
85
|
+
for activation_candidates_indexe in activation_candidates_indexes:
|
|
86
|
+
for alpha_candidate in alpha_candidates:
|
|
87
|
+
for learning_rate_init_candidate in learning_rate_init_candidates:
|
|
88
|
+
all_candidate_combinations.append([hidden_layer_sizes_index, activation_candidates_indexe, alpha_candidate, learning_rate_init_candidate])
|
|
89
|
+
all_candidate_combinations = np.array(all_candidate_combinations)
|
|
90
|
+
all_candidate_combinations_df = pd.DataFrame(all_candidate_combinations, columns=['hidden_layer_sizes', 'activation', 'alpha', 'learning_rate_init'])
|
|
91
|
+
|
|
92
|
+
if all_candidate_combinations_df.shape[0] <= doe_number_of_selecting_samples + bo_iteration_number:
|
|
93
|
+
optimal_hidden_layer_sizes, optimal_activation, optimal_alpha, optimal_learning_rate_init = gs_dnn_hyperparams_class(x, y,
|
|
94
|
+
hidden_layer_sizes_candidates=hidden_layer_sizes_candidates,
|
|
95
|
+
activation_candidates=activation_candidates,
|
|
96
|
+
alpha_candidates=alpha_candidates,
|
|
97
|
+
learning_rate_init_candidates=learning_rate_init_candidates,
|
|
98
|
+
parameter=parameter, display_flag=display_flag)
|
|
99
|
+
return optimal_hidden_layer_sizes, optimal_activation, optimal_alpha, optimal_learning_rate_init
|
|
100
|
+
|
|
101
|
+
numerical_variable_numbers = np.array([2, 3])
|
|
102
|
+
category_variable_numbers = np.array([0, 1])
|
|
103
|
+
#category_variable_names = all_candidate_combinations_df.columns[category_variable_numbers]
|
|
104
|
+
numerical_x = all_candidate_combinations_df.iloc[:, numerical_variable_numbers]
|
|
105
|
+
if len(np.where(alpha_candidates <= 0)[0]) == 0:
|
|
106
|
+
numerical_x.iloc[:, 0] = np.log10(numerical_x.iloc[:, 0])
|
|
107
|
+
if len(np.where(learning_rate_init_candidates <= 0)[0]) == 0:
|
|
108
|
+
numerical_x.iloc[:, 1] = np.log10(numerical_x.iloc[:, 1])
|
|
109
|
+
category_x = all_candidate_combinations_df.iloc[:, category_variable_numbers]
|
|
110
|
+
dummy_x = pd.get_dummies(category_x.astype(str))
|
|
111
|
+
# all_x = pd.concat([numerical_x, dummy_x], axis=1)
|
|
112
|
+
params_df = pd.concat([numerical_x, dummy_x], axis=1)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
cross_validation = StratifiedKFold(n_splits=parameter, random_state=9, shuffle=True)
|
|
116
|
+
# ベイズ最適化の繰り返し
|
|
117
|
+
for bo_iter in bo_iterations:
|
|
118
|
+
if display_flag:
|
|
119
|
+
print(f'Bayesian optimization iteration : {bo_iter + 1} / {bo_iteration_number}')
|
|
120
|
+
# print('='*10)
|
|
121
|
+
if bo_iter == 0: # 最初の試行ではD最適基準を計算
|
|
122
|
+
# D最適基準の計算
|
|
123
|
+
autoscaled_params_df = (params_df - params_df.mean(axis=0)) / params_df.std(axis=0, ddof=1) # 計算のために標準化
|
|
124
|
+
|
|
125
|
+
all_indexes = list(range(autoscaled_params_df.shape[0])) # indexを取得
|
|
126
|
+
|
|
127
|
+
np.random.seed(11) # 乱数を生成するためのシードを固定
|
|
128
|
+
for random_search_number in range(doe_number_of_random_searches):
|
|
129
|
+
# 1. ランダムに候補を選択
|
|
130
|
+
new_selected_indexes = np.random.choice(all_indexes, doe_number_of_selecting_samples, replace=False)
|
|
131
|
+
new_selected_samples = autoscaled_params_df.iloc[new_selected_indexes, :]
|
|
132
|
+
# 2. D 最適基準を計算
|
|
133
|
+
xt_x = np.dot(new_selected_samples.T, new_selected_samples)
|
|
134
|
+
d_optimal_value = np.linalg.det(xt_x)
|
|
135
|
+
# 3. D 最適基準が前回までの最大値を上回ったら、選択された候補を更新
|
|
136
|
+
if random_search_number == 0:
|
|
137
|
+
best_d_optimal_value = d_optimal_value.copy()
|
|
138
|
+
selected_sample_indexes = new_selected_indexes.copy()
|
|
139
|
+
else:
|
|
140
|
+
if best_d_optimal_value < d_optimal_value:
|
|
141
|
+
best_d_optimal_value = d_optimal_value.copy()
|
|
142
|
+
selected_sample_indexes = new_selected_indexes.copy()
|
|
143
|
+
selected_sample_indexes = list(selected_sample_indexes) # リスト型に変換
|
|
144
|
+
# 選択されたサンプル、選択されなかったサンプル
|
|
145
|
+
selected_params_df = params_df.iloc[selected_sample_indexes, :] # 選択されたサンプル
|
|
146
|
+
true_selected_params_df = all_candidate_combinations_df.iloc[selected_sample_indexes, :]
|
|
147
|
+
bo_params_df = selected_params_df.copy() # BOのGPモデル構築用データを作成
|
|
148
|
+
remaining_indexes = np.delete(all_indexes, selected_sample_indexes) # 選択されなかったサンプルのインデックス
|
|
149
|
+
remaining_params_df = params_df.iloc[remaining_indexes, :] # 選択されなかったサンプル
|
|
150
|
+
true_remaining_params_df = all_candidate_combinations_df.iloc[remaining_indexes, :]
|
|
151
|
+
|
|
152
|
+
# 選択された全候補でGMRの計算
|
|
153
|
+
params_with_score_df = params_df.copy() # cvのscoreが含まれるdataframe
|
|
154
|
+
params_with_score_df['score'] = np.nan # 初期値はnanを設定
|
|
155
|
+
|
|
156
|
+
else: # 2回目以降では前回の結果をもとにする
|
|
157
|
+
selected_sample_indexes = next_samples_df.index # 提案サンプルのindex
|
|
158
|
+
selected_params_df = params_df.loc[selected_sample_indexes, :] # 次に計算するサンプル
|
|
159
|
+
true_selected_params_df = all_candidate_combinations_df.loc[selected_sample_indexes, :] # 次に計算するサンプル
|
|
160
|
+
bo_params_df = pd.concat([bo_params_df, selected_params_df], axis=0) # BOのGPモデル構築用データは前回のデータと提案サンプルをマージする
|
|
161
|
+
remaining_params_df = params_df.loc[params_with_score_df['score'].isna(), :] # 選択されなかったサンプル
|
|
162
|
+
remaining_params_df = remaining_params_df.drop(index=selected_sample_indexes)
|
|
163
|
+
true_remaining_params_df = all_candidate_combinations_df.loc[params_with_score_df['score'].isna(), :] # 選択されなかったサンプル
|
|
164
|
+
true_remaining_params_df = true_remaining_params_df.drop(index=selected_sample_indexes)
|
|
165
|
+
|
|
166
|
+
# 選ばれたサンプル(パラメータの組み合わせ)を一つずつ計算する
|
|
167
|
+
for i_n, selected_params_idx in enumerate(selected_sample_indexes):
|
|
168
|
+
selected_params = true_selected_params_df.loc[selected_params_idx, :] # サンプルの選択
|
|
169
|
+
model = MLPClassifier(hidden_layer_sizes=hidden_layer_sizes_candidates[int(selected_params.iloc[0])],
|
|
170
|
+
activation=activation_candidates[int(selected_params.iloc[1])],
|
|
171
|
+
alpha=selected_params.iloc[2],
|
|
172
|
+
learning_rate_init=selected_params.iloc[3],
|
|
173
|
+
random_state=99)
|
|
174
|
+
estimated_y = cross_val_predict(model, x, y, cv=cross_validation)
|
|
175
|
+
params_with_score_df.loc[selected_params_idx, 'score'] = accuracy_score(y, estimated_y) # データの保存
|
|
176
|
+
if display_flag:
|
|
177
|
+
print('Best score :', params_with_score_df['score'].max())
|
|
178
|
+
print('='*10)
|
|
179
|
+
|
|
180
|
+
# 最後はBOの計算をしないためbreak
|
|
181
|
+
if bo_iter + 1 == bo_iteration_number:
|
|
182
|
+
break
|
|
183
|
+
|
|
184
|
+
# Bayesian optimization
|
|
185
|
+
bo_x_data = bo_params_df.copy() # GP学習用データはGMRの結果があるサンプル
|
|
186
|
+
bo_x_prediction = remaining_params_df.copy() # predictionは選択されていない(GMRの結果がない)サンプル
|
|
187
|
+
bo_y_data = params_with_score_df.loc[bo_params_df.index, 'score'] # yはGMRのr2cv
|
|
188
|
+
|
|
189
|
+
# カーネル 11 種類
|
|
190
|
+
bo_kernels = [ConstantKernel() * DotProduct() + WhiteKernel(),
|
|
191
|
+
ConstantKernel() * RBF() + WhiteKernel(),
|
|
192
|
+
ConstantKernel() * RBF() + WhiteKernel() + ConstantKernel() * DotProduct(),
|
|
193
|
+
ConstantKernel() * RBF(np.ones(bo_x_data.shape[1])) + WhiteKernel(),
|
|
194
|
+
ConstantKernel() * RBF(np.ones(bo_x_data.shape[1])) + WhiteKernel() + ConstantKernel() * DotProduct(),
|
|
195
|
+
ConstantKernel() * Matern(nu=1.5) + WhiteKernel(),
|
|
196
|
+
ConstantKernel() * Matern(nu=1.5) + WhiteKernel() + ConstantKernel() * DotProduct(),
|
|
197
|
+
ConstantKernel() * Matern(nu=0.5) + WhiteKernel(),
|
|
198
|
+
ConstantKernel() * Matern(nu=0.5) + WhiteKernel() + ConstantKernel() * DotProduct(),
|
|
199
|
+
ConstantKernel() * Matern(nu=2.5) + WhiteKernel(),
|
|
200
|
+
ConstantKernel() * Matern(nu=2.5) + WhiteKernel() + ConstantKernel() * DotProduct()]
|
|
201
|
+
|
|
202
|
+
next_samples = pd.DataFrame([], columns=selected_params_df.columns) # 次のサンプルを入れる変数を準備
|
|
203
|
+
|
|
204
|
+
# 次の候補を複数提案する繰り返し工程
|
|
205
|
+
for bo_sample_number in range(bo_number_of_selecting_samples):
|
|
206
|
+
# オートスケーリング
|
|
207
|
+
bo_x_data_std = bo_x_data.std()
|
|
208
|
+
bo_x_data_std[bo_x_data_std == 0] = 1
|
|
209
|
+
autoscaled_bo_y_data = (bo_y_data - bo_y_data.mean()) / bo_y_data.std()
|
|
210
|
+
autoscaled_bo_x_data = (bo_x_data - bo_x_data.mean()) / bo_x_data_std
|
|
211
|
+
autoscaled_bo_x_prediction = (bo_x_prediction - bo_x_data.mean()) / bo_x_data_std
|
|
212
|
+
|
|
213
|
+
# モデル構築
|
|
214
|
+
if bo_regression_method == 'gpr_one_kernel':
|
|
215
|
+
bo_selected_kernel = bo_kernels[bo_kernel_number]
|
|
216
|
+
bo_model = GaussianProcessRegressor(alpha=0, kernel=bo_selected_kernel)
|
|
217
|
+
|
|
218
|
+
elif bo_regression_method == 'gpr_kernels':
|
|
219
|
+
# クロスバリデーションによるカーネル関数の最適化
|
|
220
|
+
bo_cross_validation = StratifiedKFold(n_splits=bo_gp_fold_number, random_state=9, shuffle=True) # クロスバリデーションの分割の設定
|
|
221
|
+
bo_acc_cvs = [] # 空の list。カーネル関数ごとに、クロスバリデーション後の r2 を入れていきます
|
|
222
|
+
for index, bo_kernel in enumerate(bo_kernels):
|
|
223
|
+
bo_model = GaussianProcessRegressor(alpha=0, kernel=bo_kernel)
|
|
224
|
+
estimated_bo_y_in_cv = np.ndarray.flatten(cross_val_predict(bo_model, autoscaled_bo_x_data, autoscaled_bo_y_data, cv=bo_cross_validation))
|
|
225
|
+
estimated_bo_y_in_cv = estimated_bo_y_in_cv * bo_y_data.std(ddof=1) + bo_y_data.mean()
|
|
226
|
+
bo_acc_cvs.append(accuracy_score(bo_y_data, estimated_bo_y_in_cv))
|
|
227
|
+
optimal_bo_kernel_number = np.where(bo_acc_cvs == np.max(bo_acc_cvs))[0][0] # クロスバリデーション後の r2 が最も大きいカーネル関数の番号
|
|
228
|
+
optimal_bo_kernel = bo_kernels[optimal_bo_kernel_number] # クロスバリデーション後の r2 が最も大きいカーネル関数
|
|
229
|
+
|
|
230
|
+
# モデル構築
|
|
231
|
+
bo_model = GaussianProcessRegressor(alpha=0, kernel=optimal_bo_kernel, random_state=9) # GPR モデルの宣言
|
|
232
|
+
|
|
233
|
+
bo_model.fit(autoscaled_bo_x_data, autoscaled_bo_y_data) # モデルの学習
|
|
234
|
+
|
|
235
|
+
# 予測
|
|
236
|
+
estimated_bo_y_prediction, estimated_bo_y_prediction_std = bo_model.predict(autoscaled_bo_x_prediction, return_std=True)
|
|
237
|
+
estimated_bo_y_prediction = estimated_bo_y_prediction * bo_y_data.std() + bo_y_data.mean()
|
|
238
|
+
estimated_bo_y_prediction_std = estimated_bo_y_prediction_std * bo_y_data.std()
|
|
239
|
+
|
|
240
|
+
cumulative_variance = np.zeros(bo_x_prediction.shape[0])
|
|
241
|
+
# 獲得関数の計算
|
|
242
|
+
if acquisition_function == 'MI':
|
|
243
|
+
acquisition_function_prediction = estimated_bo_y_prediction + np.log(2 / delta) ** 0.5 * (
|
|
244
|
+
(estimated_bo_y_prediction_std ** 2 + cumulative_variance) ** 0.5 - cumulative_variance ** 0.5)
|
|
245
|
+
cumulative_variance = cumulative_variance + estimated_bo_y_prediction_std ** 2
|
|
246
|
+
elif acquisition_function == 'EI':
|
|
247
|
+
acquisition_function_prediction = (estimated_bo_y_prediction - max(bo_y_data) - relaxation * bo_y_data.std()) * \
|
|
248
|
+
norm.cdf((estimated_bo_y_prediction - max(bo_y_data) - relaxation * bo_y_data.std()) /
|
|
249
|
+
estimated_bo_y_prediction_std) + \
|
|
250
|
+
estimated_bo_y_prediction_std * \
|
|
251
|
+
norm.pdf((estimated_bo_y_prediction - max(bo_y_data) - relaxation * bo_y_data.std()) /
|
|
252
|
+
estimated_bo_y_prediction_std)
|
|
253
|
+
elif acquisition_function == 'PI':
|
|
254
|
+
acquisition_function_prediction = norm.cdf(
|
|
255
|
+
(estimated_bo_y_prediction - max(bo_y_data) - relaxation * bo_y_data.std()) / estimated_bo_y_prediction_std)
|
|
256
|
+
elif acquisition_function == 'PTR':
|
|
257
|
+
acquisition_function_prediction = norm.cdf(target_range[1],
|
|
258
|
+
loc=estimated_bo_y_prediction,
|
|
259
|
+
scale=estimated_bo_y_prediction_std
|
|
260
|
+
) - norm.cdf(target_range[0],
|
|
261
|
+
loc=estimated_bo_y_prediction,
|
|
262
|
+
scale=estimated_bo_y_prediction_std)
|
|
263
|
+
acquisition_function_prediction[estimated_bo_y_prediction_std <= 0] = 0
|
|
264
|
+
|
|
265
|
+
# 保存
|
|
266
|
+
estimated_bo_y_prediction = pd.DataFrame(estimated_bo_y_prediction, bo_x_prediction.index, columns=['estimated_y'])
|
|
267
|
+
estimated_bo_y_prediction_std = pd.DataFrame(estimated_bo_y_prediction_std, bo_x_prediction.index, columns=['std_of_estimated_y'])
|
|
268
|
+
acquisition_function_prediction = pd.DataFrame(acquisition_function_prediction, index=bo_x_prediction.index, columns=['acquisition_function'])
|
|
269
|
+
#
|
|
270
|
+
# 次のサンプル
|
|
271
|
+
next_samples = pd.concat([next_samples, bo_x_prediction.loc[acquisition_function_prediction.idxmax()]], axis=0)
|
|
272
|
+
|
|
273
|
+
# x, y, x_prediction, cumulative_variance の更新
|
|
274
|
+
bo_x_data = pd.concat([bo_x_data, bo_x_prediction.loc[acquisition_function_prediction.idxmax()]], axis=0)
|
|
275
|
+
bo_y_data = pd.concat([bo_y_data, estimated_bo_y_prediction.loc[acquisition_function_prediction.idxmax()].iloc[0]], axis=0)
|
|
276
|
+
bo_x_prediction = bo_x_prediction.drop(acquisition_function_prediction.idxmax(), axis=0)
|
|
277
|
+
cumulative_variance = np.delete(cumulative_variance, np.where(acquisition_function_prediction.index == acquisition_function_prediction.iloc[:, 0].idxmax())[0][0])
|
|
278
|
+
next_samples_df = next_samples.copy()
|
|
279
|
+
|
|
280
|
+
# 結果の保存
|
|
281
|
+
#params_with_score_df.sort_values('score', ascending=False).to_csv('params_with_score.csv')
|
|
282
|
+
params_with_score_df_best = params_with_score_df.sort_values('score', ascending=False).iloc[0, :] # r2が高い順にソート
|
|
283
|
+
best_acc_cv = params_with_score_df_best['score'].copy()
|
|
284
|
+
# print(best_r2cv)
|
|
285
|
+
best_candidate_combination = all_candidate_combinations_df.loc[params_with_score_df_best.name].copy()
|
|
286
|
+
|
|
287
|
+
optimal_hidden_layer_sizes = hidden_layer_sizes_candidates[int(best_candidate_combination.iloc[0])]
|
|
288
|
+
optimal_activation = activation_candidates[int(best_candidate_combination.iloc[1])]
|
|
289
|
+
optimal_alpha = best_candidate_combination.iloc[2]
|
|
290
|
+
optimal_learning_rate_init = best_candidate_combination.iloc[3]
|
|
291
|
+
|
|
292
|
+
return optimal_hidden_layer_sizes, optimal_activation, optimal_alpha, optimal_learning_rate_init
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def gs_dnn_hyperparams_class(x, y,
|
|
296
|
+
hidden_layer_sizes_candidates=[(50,), (100,), (50, 10), (100, 10), (50, 50, 10), (100, 100, 10), (50, 50, 50, 10), (100, 100, 100, 10)],
|
|
297
|
+
activation_candidates=['identity', 'logistic', 'tanh', 'relu'],
|
|
298
|
+
alpha_candidates=10 ** np.arange(-6, -1, dtype=float),
|
|
299
|
+
learning_rate_init_candidates=10 ** np.arange(-5, 0, dtype=float),
|
|
300
|
+
parameter=5, bo_iteration_number=15, display_flag=False):
|
|
301
|
+
"""
|
|
302
|
+
Grid search of scikit-learn[MLPClassifier]-based deep neural network (DNN) hyperparameters
|
|
303
|
+
|
|
304
|
+
Optimize DNN hyperparameters based on Grid search and cross-validation
|
|
305
|
+
|
|
306
|
+
Parameters
|
|
307
|
+
----------
|
|
308
|
+
x : numpy.array or pandas.DataFrame
|
|
309
|
+
(autoscaled) m x n matrix of X-variables of training data,
|
|
310
|
+
m is the number of training sammples and
|
|
311
|
+
n is the number of X-variables
|
|
312
|
+
y : numpy.array or pandas.DataFrame
|
|
313
|
+
(autoscaled) m x 1 vector of a Y-variable of training data
|
|
314
|
+
hidden_layer_sizes_candidates : list
|
|
315
|
+
vector of candidates of hidden_layer_sizes in MLPClassifier
|
|
316
|
+
activation_candidates : list
|
|
317
|
+
vector of candidates of activation in MLPClassifier
|
|
318
|
+
alpha_candidates : numpy.array or list
|
|
319
|
+
vector of candidates of alpha in MLPClassifier
|
|
320
|
+
learning_rate_init_candidates : numpy.array or list
|
|
321
|
+
vector of candidates of learning_rate_init in MLPClassifier
|
|
322
|
+
parameter : int
|
|
323
|
+
"fold_number"-fold cross-validation in cross-validation, and k in midknn
|
|
324
|
+
|
|
325
|
+
Returns
|
|
326
|
+
-------
|
|
327
|
+
optimal_hidden_layer_sizes : tuple
|
|
328
|
+
optimized hidden_layer_sizes in MLPClassifier
|
|
329
|
+
optimal_activation : str
|
|
330
|
+
optimized activation in MLPClassifier
|
|
331
|
+
optimal_alpha : float
|
|
332
|
+
optimized alpha in MLPClassifier
|
|
333
|
+
optimal_learning_rate_init : float
|
|
334
|
+
optimized learning_rate_init in MLPClassifier
|
|
335
|
+
"""
|
|
336
|
+
|
|
337
|
+
x = np.array(x)
|
|
338
|
+
y = np.array(y)
|
|
339
|
+
cross_validation = StratifiedKFold(n_splits=parameter, random_state=9, shuffle=True)
|
|
340
|
+
|
|
341
|
+
alpha_candidates = np.array(alpha_candidates)
|
|
342
|
+
learning_rate_init_candidates = np.array(learning_rate_init_candidates)
|
|
343
|
+
|
|
344
|
+
hidden_layer_sizes_indexes = [i for i in range(len(hidden_layer_sizes_candidates))]
|
|
345
|
+
activation_candidates_indexes = [i for i in range(len(activation_candidates))]
|
|
346
|
+
all_candidate_combinations = []
|
|
347
|
+
scores = []
|
|
348
|
+
number = 0
|
|
349
|
+
total_number = len(hidden_layer_sizes_indexes) * len(activation_candidates_indexes) * len(alpha_candidates) * len(learning_rate_init_candidates)
|
|
350
|
+
for hidden_layer_sizes_index in hidden_layer_sizes_indexes:
|
|
351
|
+
for activation_candidates_indexe in activation_candidates_indexes:
|
|
352
|
+
for alpha_candidate in alpha_candidates:
|
|
353
|
+
for learning_rate_init_candidate in learning_rate_init_candidates:
|
|
354
|
+
if display_flag:
|
|
355
|
+
number += 1
|
|
356
|
+
print(number, '/', total_number)
|
|
357
|
+
all_candidate_combinations.append([hidden_layer_sizes_index, activation_candidates_indexe, alpha_candidate, learning_rate_init_candidate])
|
|
358
|
+
model = MLPClassifier(hidden_layer_sizes=hidden_layer_sizes_candidates[int(hidden_layer_sizes_index)],
|
|
359
|
+
activation=activation_candidates[int(activation_candidates_indexe)],
|
|
360
|
+
alpha=alpha_candidate,
|
|
361
|
+
learning_rate_init=learning_rate_init_candidate,
|
|
362
|
+
random_state=99)
|
|
363
|
+
estimated_y = cross_val_predict(model, x, y, cv=cross_validation)
|
|
364
|
+
scores.append(accuracy_score(y, estimated_y))
|
|
365
|
+
scores = np.array(scores)
|
|
366
|
+
best_index = np.where(scores == scores.max())[0][0]
|
|
367
|
+
all_candidate_combinations = np.array(all_candidate_combinations)
|
|
368
|
+
|
|
369
|
+
optimal_hidden_layer_sizes = hidden_layer_sizes_candidates[int(all_candidate_combinations[best_index, 0])]
|
|
370
|
+
optimal_activation = activation_candidates[int(all_candidate_combinations[best_index, 1])]
|
|
371
|
+
optimal_alpha = all_candidate_combinations[best_index, 2]
|
|
372
|
+
optimal_learning_rate_init = all_candidate_combinations[best_index, 3]
|
|
373
|
+
|
|
374
|
+
return optimal_hidden_layer_sizes, optimal_activation, optimal_alpha, optimal_learning_rate_init
|
|
@@ -26,6 +26,7 @@ dcekit/optimization/__init__.py
|
|
|
26
26
|
dcekit/optimization/fast_opt_svr_hyperparams.py
|
|
27
27
|
dcekit/optimization/iot.py
|
|
28
28
|
dcekit/optimization/opt_dnn_hyperparams.py
|
|
29
|
+
dcekit/optimization/opt_dnn_hyperparams_class.py
|
|
29
30
|
dcekit/sampling/__init__.py
|
|
30
31
|
dcekit/sampling/kennard_stone.py
|
|
31
32
|
dcekit/validation/__init__.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|