-
Notifications
You must be signed in to change notification settings - Fork 7
/
industrial_strategy.py
267 lines (232 loc) · 12 KB
/
industrial_strategy.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from copy import deepcopy
import numpy as np
from fedot import Fedot
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.data.multi_modal import MultiModalData
from fedot.core.pipelines.pipeline_builder import PipelineBuilder
from fedot.core.repository.dataset_types import DataTypesEnum
from golem.core.tuning.optuna_tuner import OptunaTuner
from fedot_ind.core.ensemble.kernel_ensemble import KernelEnsembler
from fedot_ind.core.ensemble.random_automl_forest import RAFensembler
from fedot_ind.core.repository.constanst_repository import BATCH_SIZE_FOR_FEDOT_WORKER, FEDOT_WORKER_NUM, \
FEDOT_WORKER_TIMEOUT_PARTITION, FEDOT_TUNING_METRICS, FEDOT_TUNER_STRATEGY, FEDOT_TS_FORECASTING_ASSUMPTIONS, \
FEDOT_TASK
from fedot_ind.core.repository.industrial_implementations.abstract import build_tuner
from fedot_ind.core.repository.initializer_industrial_models import IndustrialModels
class IndustrialStrategy:
def __init__(self, industrial_strategy_params,
industrial_strategy,
api_config,
logger=None
):
self.industrial_strategy_params = industrial_strategy_params
self.industrial_strategy = industrial_strategy
self.industrial_strategy_fit = {
'federated_automl': self._federated_strategy,
'kernel_automl': self._kernel_strategy,
'forecasting_assumptions': self._forecasting_strategy,
'forecasting_exogenous': self._forecasting_exogenous_strategy,
'lora_strategy': self._lora_strategy,
}
self.industrial_strategy_predict = {
'federated_automl': self._federated_predict,
'kernel_automl': self._kernel_predict,
'forecasting_assumptions': self._forecasting_predict,
'forecasting_exogenous': self._forecasting_predict,
'lora_strategy': self._lora_predict,
}
self.ensemble_strategy_dict = {'MeanEnsemble': np.mean,
'MedianEnsemble': np.median,
'MinEnsemble': np.min,
'MaxEnsemble': np.max,
'ProductEnsemble': np.prod}
self.ensemble_strategy = list(self.ensemble_strategy_dict.keys())
self.random_label = None
self.config_dict = api_config
self.logger = logger
self.repo = IndustrialModels().setup_repository()
self.kernel_ensembler = KernelEnsembler
self.RAF_workers = None
self.solver = None
def fit(self, input_data):
self.industrial_strategy_fit[self.industrial_strategy](input_data)
return self.solver
def predict(self, input_data, predict_mode):
return self.industrial_strategy_predict[self.industrial_strategy](
input_data, predict_mode)
def _federated_strategy(self, input_data):
if input_data.features.shape[0] > BATCH_SIZE_FOR_FEDOT_WORKER:
self.logger.info('RAF algorithm was applied')
if self.RAF_workers is None:
batch_size = FEDOT_WORKER_NUM
else:
batch_size = round(
input_data.features.shape[0] /
self.RAF_workers)
# batch_size = round(input_data.features.shape[0] / self.RAF_workers if self.RAF_workers
# is not None else FEDOT_WORKER_NUM)
batch_timeout = round(
self.config_dict['timeout'] /
FEDOT_WORKER_TIMEOUT_PARTITION)
self.config_dict['timeout'] = batch_timeout
self.logger.info(
f'Batch_size - {batch_size}. Number of batches - {self.RAF_workers}')
self.solver = RAFensembler(composing_params=self.config_dict,
n_splits=self.RAF_workers,
batch_size=batch_size)
self.logger.info(
f'Number of AutoMl models in ensemble - {self.solver.n_splits}')
def _forecasting_strategy(self, input_data):
self.logger.info('TS forecasting algorithm was applied')
self.config_dict['timeout'] = round(self.config_dict['timeout'] / 3)
self.solver = {}
for model_name, init_assumption in FEDOT_TS_FORECASTING_ASSUMPTIONS.items():
try:
self.config_dict['initial_assumption'] = init_assumption.build()
industrial = Fedot(**self.config_dict)
industrial.fit(input_data)
self.solver.update({model_name: industrial})
except Exception:
self.logger.info(f'Failed during fit stage - {model_name}')
def _forecasting_exogenous_strategy(self, input_data):
self.logger.info('TS exogenous forecasting algorithm was applied')
self.solver = {}
init_assumption = PipelineBuilder().add_node('lagged', 0)
task = FEDOT_TASK[self.config_dict['problem']]
train_lagged, predict_lagged = train_test_data_setup(InputData(idx=np.arange(len(input_data.features)),
features=input_data.features,
target=input_data.features,
task=task,
data_type=DataTypesEnum.ts), 2)
dataset_dict = {'lagged': train_lagged}
exog_variable = self.industrial_strategy_params['exog_variable']
init_assumption.add_node('exog_ts', 1)
# Exogenous time series
train_exog, predict_exog = train_test_data_setup(InputData(idx=np.arange(len(exog_variable)),
features=exog_variable,
target=input_data.features,
task=task,
data_type=DataTypesEnum.ts), 2)
dataset_dict.update({f'exog_ts': train_exog})
train_dataset = MultiModalData(dataset_dict)
init_assumption = init_assumption.join_branches('ridge')
self.config_dict['initial_assumption'] = init_assumption.build()
industrial = Fedot(**self.config_dict)
industrial.fit(train_dataset)
self.solver = {'exog_model': industrial}
def _finetune_loop(self,
kernel_ensemble: dict,
kernel_data: dict,
tuning_params: dict = {}):
tuned_kernels = {}
tuning_params['metric'] = FEDOT_TUNING_METRICS[self.config_dict['problem']]
for generator, kernel_model in kernel_ensemble.items():
tuned_metric = 0
for tuner_name, tuner_type in FEDOT_TUNER_STRATEGY.items():
tuning_params['tuner'] = tuner_type
model_to_tune = deepcopy(kernel_model)
pipeline_tuner, tuned_kernel_model = build_tuner(
self, model_to_tune, tuning_params, kernel_data[generator], 'head')
if abs(pipeline_tuner.obtained_metric) > tuned_metric:
tuned_metric = abs(pipeline_tuner.obtained_metric)
self.solver = tuned_kernel_model
tuned_kernels.update({generator: self.solver})
return tuned_kernels
def _kernel_strategy(self, input_data):
self.kernel_ensembler = KernelEnsembler(
self.industrial_strategy_params)
kernel_ensemble, kernel_data = self.kernel_ensembler.transform(
input_data).predict
self.solver = self._finetune_loop(kernel_ensemble, kernel_data)
# tuning_params = {'metric': FEDOT_TUNING_METRICS[self.config_dict['problem']], 'tuner': OptunaTuner}
# self.solver
# self.solver = build_tuner(self, self.solver, tuning_params, input_data, 'head')
def _lora_strategy(self, input_data):
self.lora_model = PipelineBuilder().add_node(
'lora_model', params=self.industrial_strategy_params).build()
self.lora_model.fit(input_data)
def _federated_predict(self,
input_data,
mode: str = 'labels'):
self.predicted_branch_probs = [
x.predict(input_data).predict for x in self.solver.root_node.nodes_from]
self.predicted_branch_labels = [
np.argmax(x, axis=1) for x in self.predicted_branch_probs]
n_samples, n_channels, n_classes = self.predicted_branch_probs[0].shape[0], \
len(self.predicted_branch_probs), \
self.predicted_branch_probs[0].shape[1]
head_model = deepcopy(self.solver.root_node)
head_model.nodes_from = []
input_data.features = np.hstack(
self.predicted_branch_labels).reshape(
n_samples, n_channels, 1)
head_predict = head_model.predict(self.predict_data).predict
if mode == 'labels':
return head_predict
else:
return np.argmax(head_predict, axis=1)
def _forecasting_predict(self,
input_data,
mode: str = True):
labels_dict = {
k: v.predict(
features=input_data,
in_sample=mode) for k,
v in self.solver.items()}
return labels_dict
def _lora_predict(self,
input_data,
mode: str = True):
labels_dict = {
k: v.predict(
features=input_data,
in_sample=mode) for k,
v in self.solver.items()}
return labels_dict
def _kernel_predict(self,
input_data,
mode: str = 'labels'):
labels_dict = {
k: v.predict(
input_data,
mode).predict for k,
v in self.solver.items()}
return labels_dict
def _check_predictions(self, predictions):
"""Check if the predictions array has the correct size.
Args:
predictions: array of shape (n_samples, n_classifiers). The votes obtained by each classifier
for each sample.
Returns:
predictions: array of shape (n_samples, n_classifiers). The votes obtained by each classifier
for each sample.
Raises:
ValueError: if the array do not contain exactly 3 dimensions: [n_samples, n_classifiers, n_classes]
"""
list_proba = [predictions[model_preds] for model_preds in predictions]
transformed = []
if self.random_label is None:
self.random_label = {
class_by_gen: np.random.choice(self.kernel_ensembler.classes_misses_by_generator[class_by_gen])
for class_by_gen in self.kernel_ensembler.classes_described_by_generator}
for prob_by_gen, class_by_gen in zip(
list_proba, self.kernel_ensembler.classes_described_by_generator):
converted_probs = np.zeros(
(prob_by_gen.shape[0], len(
self.kernel_ensembler.all_classes)))
for true_class, map_class in self.kernel_ensembler.mapper_dict[class_by_gen].items(
):
converted_probs[:, true_class] = prob_by_gen[:, map_class]
random_label = self.random_label[class_by_gen]
converted_probs[:, random_label] = prob_by_gen[:, -1]
transformed.append(converted_probs)
return np.array(transformed).transpose((1, 0, 2))
def ensemble_predictions(self, prediction_dict, strategy):
transformed_predictions = self._check_predictions(prediction_dict)
average_proba_predictions = self.ensemble_strategy_dict[strategy](
transformed_predictions, axis=1)
if average_proba_predictions.shape[1] == 1:
average_proba_predictions = np.concatenate(
[average_proba_predictions, 1 - average_proba_predictions], axis=1)
return average_proba_predictions