forked from automl/Auto-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstract_evaluator.py
More file actions
1082 lines (938 loc) · 47.6 KB
/
Copy pathabstract_evaluator.py
File metadata and controls
1082 lines (938 loc) · 47.6 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging.handlers
import time
import warnings
from multiprocessing.queues import Queue
from typing import Any, Dict, List, Optional, Tuple, Union, no_type_check
from ConfigSpace import Configuration
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator
from sklearn.dummy import DummyClassifier, DummyRegressor
from sklearn.ensemble import VotingClassifier
from smac.tae import StatusType
import autoPyTorch.pipeline.image_classification
import autoPyTorch.pipeline.tabular_classification
import autoPyTorch.pipeline.tabular_regression
try:
import autoPyTorch.pipeline.time_series_forecasting
forecasting_dependencies_installed = True
except ModuleNotFoundError:
forecasting_dependencies_installed = False
import autoPyTorch.pipeline.traditional_tabular_classification
import autoPyTorch.pipeline.traditional_tabular_regression
from autoPyTorch.automl_common.common.utils.backend import Backend
from autoPyTorch.constants import (
CLASSIFICATION_TASKS,
FORECASTING_BUDGET_TYPE,
FORECASTING_TASKS,
ForecastingDependenciesNotInstalledMSG,
IMAGE_TASKS,
MULTICLASS,
REGRESSION_TASKS,
STRING_TO_OUTPUT_TYPES,
STRING_TO_TASK_TYPES,
TABULAR_TASKS
)
from autoPyTorch.datasets.base_dataset import (
BaseDataset,
BaseDatasetPropertiesType
)
from autoPyTorch.evaluation.utils import (
DisableFileOutputParameters,
VotingRegressorWrapper,
convert_multioutput_multiclass_to_multilabel
)
try:
from autoPyTorch.evaluation.utils_extra import DummyTimeSeriesForecastingPipeline
forecasting_dependencies_installed = True
except ModuleNotFoundError:
forecasting_dependencies_installed = False
from autoPyTorch.pipeline.base_pipeline import BasePipeline
from autoPyTorch.pipeline.components.training.metrics.base import autoPyTorchMetric
from autoPyTorch.pipeline.components.training.metrics.utils import (
calculate_loss,
get_metrics
)
from autoPyTorch.utils.common import dict_repr, subsampler
from autoPyTorch.utils.hyperparameter_search_space_update import HyperparameterSearchSpaceUpdates
from autoPyTorch.utils.logging_ import PicklableClientLogger, get_named_client_logger
from autoPyTorch.utils.pipeline import get_dataset_requirements
__all__ = [
'AbstractEvaluator',
'fit_and_suppress_warnings'
]
class MyTraditionalTabularClassificationPipeline(BaseEstimator):
"""
A wrapper class that holds a pipeline for traditional classification.
Estimators like CatBoost, and Random Forest are considered traditional machine
learning models and are fitted before neural architecture search.
This class is an interface to fit a pipeline containing a traditional machine
learning model, and is the final object that is stored for inference.
Attributes:
dataset_properties (Dict[str, BaseDatasetPropertiesType]):
A dictionary containing dataset specific information
random_state (Optional[np.random.RandomState]):
Object that contains a seed and allows for reproducible results
init_params (Optional[Dict]):
An optional dictionary that is passed to the pipeline's steps. It complies
a similar function as the kwargs
"""
def __init__(self, config: str,
dataset_properties: Dict[str, BaseDatasetPropertiesType],
random_state: Optional[Union[int, np.random.RandomState]] = None,
init_params: Optional[Dict] = None):
self.config = config
self.dataset_properties = dataset_properties
self.random_state = random_state
self.init_params = init_params
self.pipeline = autoPyTorch.pipeline.traditional_tabular_classification. \
TraditionalTabularClassificationPipeline(dataset_properties=dataset_properties,
random_state=self.random_state)
configuration_space = self.pipeline.get_hyperparameter_search_space()
default_configuration = configuration_space.get_default_configuration().get_dictionary()
default_configuration['model_trainer:tabular_traditional_model:traditional_learner'] = config
self.configuration = Configuration(configuration_space, default_configuration)
self.pipeline.set_hyperparameters(self.configuration)
def fit(self, X: Dict[str, Any], y: Any,
sample_weight: Optional[np.ndarray] = None) -> object:
return self.pipeline.fit(X, y)
def predict_proba(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
return self.pipeline.predict_proba(X, batch_size=batch_size)
def predict(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
return self.pipeline.predict(X, batch_size=batch_size)
def get_additional_run_info(self) -> Dict[str, Any]:
"""
Can be used to return additional info for the run.
Returns:
Dict[str, Any]:
Currently contains
1. pipeline_configuration: the configuration of the pipeline, i.e, the traditional model used
2. trainer_configuration: the parameters for the traditional model used.
Can be found in autoPyTorch/pipeline/components/setup/traditional_ml/estimator_configs
"""
return {'pipeline_configuration': self.configuration,
'trainer_configuration': self.pipeline.named_steps['model_trainer'].choice.model.get_config(),
'configuration_origin': 'traditional'}
def get_pipeline_representation(self) -> Dict[str, str]:
return self.pipeline.get_pipeline_representation()
@staticmethod
def get_default_pipeline_options() -> Dict[str, Any]:
return autoPyTorch.pipeline.traditional_tabular_classification. \
TraditionalTabularClassificationPipeline.get_default_pipeline_options()
class MyTraditionalTabularRegressionPipeline(BaseEstimator):
"""
A wrapper class that holds a pipeline for traditional regression.
Estimators like CatBoost, and Random Forest are considered traditional machine
learning models and are fitted before neural architecture search.
This class is an interface to fit a pipeline containing a traditional machine
learning model, and is the final object that is stored for inference.
Attributes:
dataset_properties (Dict[str, Any]):
A dictionary containing dataset specific information
random_state (Optional[np.random.RandomState]):
Object that contains a seed and allows for reproducible results
init_params (Optional[Dict]):
An optional dictionary that is passed to the pipeline's steps. It complies
a similar function as the kwargs
"""
def __init__(self, config: str,
dataset_properties: Dict[str, Any],
random_state: Optional[np.random.RandomState] = None,
init_params: Optional[Dict] = None):
self.config = config
self.dataset_properties = dataset_properties
self.random_state = random_state
self.init_params = init_params
self.pipeline = autoPyTorch.pipeline.traditional_tabular_regression. \
TraditionalTabularRegressionPipeline(dataset_properties=dataset_properties,
random_state=self.random_state)
configuration_space = self.pipeline.get_hyperparameter_search_space()
default_configuration = configuration_space.get_default_configuration().get_dictionary()
default_configuration['model_trainer:tabular_traditional_model:traditional_learner'] = config
self.configuration = Configuration(configuration_space, default_configuration)
self.pipeline.set_hyperparameters(self.configuration)
def fit(self, X: Dict[str, Any], y: Any,
sample_weight: Optional[np.ndarray] = None) -> object:
return self.pipeline.fit(X, y)
def predict(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
return self.pipeline.predict(X, batch_size=batch_size)
def get_additional_run_info(self) -> Dict[str, Any]:
"""
Can be used to return additional info for the run.
Returns:
Dict[str, Any]:
Currently contains
1. pipeline_configuration: the configuration of the pipeline, i.e, the traditional model used
2. trainer_configuration: the parameters for the traditional model used.
Can be found in autoPyTorch/pipeline/components/setup/traditional_ml/estimator_configs
"""
return {'pipeline_configuration': self.configuration,
'trainer_configuration': self.pipeline.named_steps['model_trainer'].choice.model.get_config()}
def get_pipeline_representation(self) -> Dict[str, str]:
return self.pipeline.get_pipeline_representation()
@staticmethod
def get_default_pipeline_options() -> Dict[str, Any]:
return autoPyTorch.pipeline.traditional_tabular_regression.\
TraditionalTabularRegressionPipeline.get_default_pipeline_options()
class DummyClassificationPipeline(DummyClassifier):
"""
A wrapper class that holds a pipeline for dummy classification.
A wrapper over DummyClassifier of scikit learn. This estimator is considered the
worst performing model. In case of failure, at least this model will be fitted.
Attributes:
random_state (Optional[Union[int, np.random.RandomState]]):
Object that contains a seed and allows for reproducible results
init_params (Optional[Dict]):
An optional dictionary that is passed to the pipeline's steps. It complies
a similar function as the kwargs
"""
def __init__(self, config: Configuration,
random_state: Optional[Union[int, np.random.RandomState]] = None,
init_params: Optional[Dict] = None
) -> None:
self.config = config
self.init_params = init_params
self.random_state = random_state
if config == 1:
super(DummyClassificationPipeline, self).__init__(strategy="uniform")
else:
super(DummyClassificationPipeline, self).__init__(strategy="most_frequent")
def fit(self, X: Dict[str, Any], y: Any,
sample_weight: Optional[np.ndarray] = None) -> object:
X_train = subsampler(X['X_train'], X['train_indices'])
y_train = subsampler(X['y_train'], X['train_indices'])
return super(DummyClassificationPipeline, self).fit(np.ones((X_train.shape[0], 1)), y_train,
sample_weight=sample_weight)
def predict_proba(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
new_X = np.ones((X.shape[0], 1))
probas = super(DummyClassificationPipeline, self).predict_proba(new_X)
probas = convert_multioutput_multiclass_to_multilabel(probas).astype(
np.float32)
return probas
def predict(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
new_X = np.ones((X.shape[0], 1))
return super(DummyClassificationPipeline, self).predict(new_X).astype(np.float32)
def get_additional_run_info(self) -> Dict: # pylint: disable=R0201
return {'configuration_origin': 'DUMMY'}
def get_pipeline_representation(self) -> Dict[str, str]:
return {
'Preprocessing': 'None',
'Estimator': 'Dummy',
}
@staticmethod
def get_default_pipeline_options() -> Dict[str, Any]:
return {'budget_type': 'epochs',
'epochs': 1,
'runtime': 1}
class DummyRegressionPipeline(DummyRegressor):
"""
A wrapper class that holds a pipeline for dummy regression.
A wrapper over DummyRegressor of scikit learn. This estimator is considered the
worst performing model. In case of failure, at least this model will be fitted.
Attributes:
random_state (Optional[Union[int, np.random.RandomState]]):
Object that contains a seed and allows for reproducible results
init_params (Optional[Dict]):
An optional dictionary that is passed to the pipeline's steps. It complies
a similar function as the kwargs
"""
def __init__(self, config: Configuration,
random_state: Optional[Union[int, np.random.RandomState]] = None,
init_params: Optional[Dict] = None) -> None:
self.config = config
self.init_params = init_params
self.random_state = random_state
if config == 1:
super(DummyRegressionPipeline, self).__init__(strategy='mean')
else:
super(DummyRegressionPipeline, self).__init__(strategy='median')
def fit(self, X: Dict[str, Any], y: Any,
sample_weight: Optional[np.ndarray] = None) -> object:
X_train = subsampler(X['X_train'], X['train_indices'])
y_train = subsampler(X['y_train'], X['train_indices'])
return super(DummyRegressionPipeline, self).fit(np.ones((X_train.shape[0], 1)), y_train,
sample_weight=sample_weight)
def predict(self, X: Union[np.ndarray, pd.DataFrame],
batch_size: int = 1000) -> np.ndarray:
new_X = np.ones((X.shape[0], 1))
return super(DummyRegressionPipeline, self).predict(new_X).astype(np.float32)
def get_additional_run_info(self) -> Dict: # pylint: disable=R0201
return {'configuration_origin': 'DUMMY'}
def get_pipeline_representation(self) -> Dict[str, str]:
return {
'Preprocessing': 'None',
'Estimator': 'Dummy',
}
@staticmethod
def get_default_pipeline_options() -> Dict[str, Any]:
return {'budget_type': 'epochs',
'epochs': 1,
'runtime': 1}
def fit_and_suppress_warnings(logger: PicklableClientLogger, pipeline: BaseEstimator,
X: Dict[str, Any], y: Any
) -> BaseEstimator:
@no_type_check
def send_warnings_to_log(message, category, filename, lineno,
file=None, line=None) -> None:
logger.debug('%s:%s: %s:%s',
filename, lineno, category.__name__, message)
return
with warnings.catch_warnings():
warnings.showwarning = send_warnings_to_log
pipeline.fit(X, y)
return pipeline
class AbstractEvaluator(object):
"""
This method defines the interface that pipeline evaluators should follow, when
interacting with SMAC through ExecuteTaFuncWithQueue.
An evaluator is an object that:
+ constructs a pipeline (i.e. a classification or regression estimator) for a given
pipeline_config and run settings (budget, seed)
+ Fits and trains this pipeline (TrainEvaluator) or tests a given
configuration (TestEvaluator)
The provided configuration determines the type of pipeline created. For more
details, please read the get_pipeline() method.
Attributes:
backend (Backend):
An object that allows interaction with the disk storage. In particular, allows to
access the train and test datasets
queue (Queue):
Each worker available will instantiate an evaluator, and after completion,
it will append the result to a multiprocessing queue
metric (autoPyTorchMetric):
A scorer object that is able to evaluate how good a pipeline was fit. It
is a wrapper on top of the actual score method (a wrapper on top of
scikit-learn accuracy for example) that formats the predictions accordingly.
budget: (float):
The amount of epochs/time a configuration is allowed to run.
budget_type (str):
The budget type. Currently, only epoch and time are allowed.
pipeline_config (Optional[Dict[str, Any]]):
Defines the content of the pipeline being evaluated. For example, it
contains pipeline specific settings like logging name, or whether or not
to use tensorboard.
configuration (Union[int, str, Configuration]):
Determines the pipeline to be constructed. A dummy estimator is created for
integer configurations, a traditional machine learning pipeline is created
for string based configuration, and NAS is performed when a configuration
object is passed.
seed (int):
A integer that allows for reproducibility of results
output_y_hat_optimization (bool):
Whether this worker should output the target predictions, so that they are
stored on disk. Fundamentally, the resampling strategy might shuffle the
Y_train targets, so we store the split in order to re-use them for ensemble
selection.
num_run (Optional[int]):
An identifier of the current configuration being fit. This number is unique per
configuration.
include (Optional[Dict[str, Any]]):
An optional dictionary to include components of the pipeline steps.
exclude (Optional[Dict[str, Any]]):
An optional dictionary to exclude components of the pipeline steps.
disable_file_output (Optional[List[Union[str, DisableFileOutputParameters]]]):
Used as a list to pass more fine-grained
information on what to save. Must be a member of `DisableFileOutputParameters`.
Allowed elements in the list are:
+ `y_optimization`:
do not save the predictions for the optimization set,
which would later on be used to build an ensemble. Note that SMAC
optimizes a metric evaluated on the optimization set.
+ `pipeline`:
do not save any individual pipeline files
+ `pipelines`:
In case of cross validation, disables saving the joint model of the
pipelines fit on each fold.
+ `y_test`:
do not save the predictions for the test set.
+ `all`:
do not save any of the above.
For more information check `autoPyTorch.evaluation.utils.DisableFileOutputParameters`.
init_params (Optional[Dict[str, Any]]):
Optional argument that is passed to each pipeline step. It is the equivalent of
kwargs for the pipeline steps.
logger_port (Optional[int]):
Logging is performed using a socket-server scheme to be robust against many
parallel entities that want to write to the same file. This integer states the
socket port for the communication channel.
If None is provided, the logging.handlers.DEFAULT_TCP_LOGGING_PORT is used.
all_supported_metrics (bool):
Whether all supported metrics should be calculated for every configuration.
search_space_updates (Optional[HyperparameterSearchSpaceUpdates]):
An object used to fine tune the hyperparameter search space of the pipeline
"""
def __init__(self, backend: Backend,
queue: Queue,
metric: autoPyTorchMetric,
budget: float,
configuration: Union[int, str, Configuration],
budget_type: str = None,
pipeline_config: Optional[Dict[str, Any]] = None,
seed: int = 1,
output_y_hat_optimization: bool = True,
num_run: Optional[int] = None,
include: Optional[Dict[str, Any]] = None,
exclude: Optional[Dict[str, Any]] = None,
disable_file_output: Optional[List[Union[str, DisableFileOutputParameters]]] = None,
init_params: Optional[Dict[str, Any]] = None,
logger_port: Optional[int] = None,
all_supported_metrics: bool = True,
search_space_updates: Optional[HyperparameterSearchSpaceUpdates] = None
) -> None:
self.starttime = time.time()
self.configuration = configuration
self.backend: Backend = backend
self.queue = queue
self.include = include
self.exclude = exclude
self.search_space_updates = search_space_updates
self.metric = metric
self.seed = seed
self._init_datamanager_info()
# Flag to save target for ensemble
self.output_y_hat_optimization = output_y_hat_optimization
disable_file_output = disable_file_output if disable_file_output is not None else []
# check compatibility of disable file output
DisableFileOutputParameters.check_compatibility(disable_file_output)
self.disable_file_output = disable_file_output
self.pipeline_class: Optional[Union[BaseEstimator, BasePipeline]] = None
if self.task_type in REGRESSION_TASKS:
if isinstance(self.configuration, int):
self.pipeline_class = DummyRegressionPipeline
elif isinstance(self.configuration, str):
self.pipeline_class = MyTraditionalTabularRegressionPipeline
elif isinstance(self.configuration, Configuration):
self.pipeline_class = autoPyTorch.pipeline.tabular_regression.TabularRegressionPipeline
else:
raise ValueError('task {} not available'.format(self.task_type))
self.predict_function = self._predict_regression
elif self.task_type in CLASSIFICATION_TASKS:
if isinstance(self.configuration, int):
self.pipeline_class = DummyClassificationPipeline
elif isinstance(self.configuration, str):
if self.task_type in TABULAR_TASKS:
self.pipeline_class = MyTraditionalTabularClassificationPipeline
else:
raise ValueError("Only tabular tasks are currently supported with traditional methods")
elif isinstance(self.configuration, Configuration):
if self.task_type in TABULAR_TASKS:
self.pipeline_class = autoPyTorch.pipeline.tabular_classification.TabularClassificationPipeline
elif self.task_type in IMAGE_TASKS:
self.pipeline_class = autoPyTorch.pipeline.image_classification.ImageClassificationPipeline
else:
raise ValueError('task {} not available'.format(self.task_type))
self.predict_function = self._predict_proba
elif self.task_type in FORECASTING_TASKS:
if isinstance(self.configuration, int):
if not forecasting_dependencies_installed:
raise ModuleNotFoundError(ForecastingDependenciesNotInstalledMSG)
self.pipeline_class = DummyTimeSeriesForecastingPipeline
elif isinstance(self.configuration, str):
raise ValueError("Only tabular classifications tasks "
"are currently supported with traditional methods")
elif isinstance(self.configuration, Configuration):
self.pipeline_class = autoPyTorch.pipeline.time_series_forecasting.TimeSeriesForecastingPipeline
else:
raise ValueError('task {} not available'.format(self.task_type))
self.predict_function = self._predict_regression
self.additional_metrics: Optional[List[autoPyTorchMetric]] = None
metrics_dict: Optional[Dict[str, List[str]]] = None
if all_supported_metrics:
self.additional_metrics = get_metrics(dataset_properties=self.dataset_properties,
all_supported_metrics=all_supported_metrics)
# Update fit dictionary with metrics passed to the evaluator
metrics_dict = {'additional_metrics': []}
metrics_dict['additional_metrics'].append(self.metric.name)
for metric in self.additional_metrics:
metrics_dict['additional_metrics'].append(metric.name)
self._init_params = init_params
assert self.pipeline_class is not None, "Could not infer pipeline class"
pipeline_config = pipeline_config if pipeline_config is not None \
else self.pipeline_class.get_default_pipeline_options()
self.budget_type = pipeline_config['budget_type'] if budget_type is None else budget_type
self.budget = pipeline_config[self.budget_type] if budget == 0 else budget
self.num_run = 0 if num_run is None else num_run
logger_name = '%s(%d)' % (self.__class__.__name__.split('.')[-1],
self.seed)
if logger_port is None:
logger_port = logging.handlers.DEFAULT_TCP_LOGGING_PORT
self.logger = get_named_client_logger(
name=logger_name,
port=logger_port,
)
self._init_fit_dictionary(logger_port=logger_port, pipeline_config=pipeline_config, metrics_dict=metrics_dict)
self.Y_optimization: Optional[np.ndarray] = None
self.Y_actual_train: Optional[np.ndarray] = None
self.pipelines: Optional[List[BaseEstimator]] = None
self.pipeline: Optional[BaseEstimator] = None
self.logger.debug("Fit dictionary in Abstract evaluator: {}".format(dict_repr(self.fit_dictionary)))
self.logger.debug("Search space updates :{}".format(self.search_space_updates))
def _init_datamanager_info(
self,
) -> None:
"""
Initialises instance attributes that come from the datamanager.
For example,
X_train, y_train, etc.
"""
datamanager: BaseDataset = self.backend.load_datamanager()
assert datamanager.task_type is not None, \
"Expected dataset {} to have task_type got None".format(datamanager.__class__.__name__)
self.task_type = STRING_TO_TASK_TYPES[datamanager.task_type]
self.output_type = STRING_TO_OUTPUT_TYPES[datamanager.output_type]
self.issparse = datamanager.issparse
self.X_train, self.y_train = datamanager.train_tensors
if datamanager.val_tensors is not None:
self.X_valid, self.y_valid = datamanager.val_tensors
else:
self.X_valid, self.y_valid = None, None
if datamanager.test_tensors is not None:
self.X_test, self.y_test = datamanager.test_tensors
else:
self.X_test, self.y_test = None, None
self.resampling_strategy = datamanager.resampling_strategy
self.num_classes: Optional[int] = getattr(datamanager, "num_classes", None)
self.dataset_properties = datamanager.get_dataset_properties(
get_dataset_requirements(info=datamanager.get_required_dataset_info(),
include=self.include,
exclude=self.exclude,
search_space_updates=self.search_space_updates
))
self.splits = datamanager.splits
if self.splits is None:
raise AttributeError(f"create_splits on {datamanager.__class__.__name__} must be called "
f"before the instantiation of {self.__class__.__name__}")
# delete datamanager from memory
del datamanager
def _init_fit_dictionary(
self,
logger_port: int,
pipeline_config: Dict[str, Any],
metrics_dict: Optional[Dict[str, List[str]]] = None,
) -> None:
"""
Initialises the fit dictionary
Args:
logger_port (int):
Logging is performed using a socket-server scheme to be robust against many
parallel entities that want to write to the same file. This integer states the
socket port for the communication channel.
pipeline_config (Dict[str, Any]):
Defines the content of the pipeline being evaluated. For example, it
contains pipeline specific settings like logging name, or whether or not
to use tensorboard.
metrics_dict (Optional[Dict[str, List[str]]]):
Contains a list of metric names to be evaluated in Trainer with key `additional_metrics`. Defaults to None.
Returns:
None
"""
self.fit_dictionary: Dict[str, Any] = {'dataset_properties': self.dataset_properties}
if metrics_dict is not None:
self.fit_dictionary.update(metrics_dict)
self.fit_dictionary.update({
'X_train': self.X_train,
'y_train': self.y_train,
'X_test': self.X_test,
'y_test': self.y_test,
'backend': self.backend,
'logger_port': logger_port,
'optimize_metric': self.metric.name
})
self.fit_dictionary.update(pipeline_config)
# If the budget is epochs, we want to limit that in the fit dictionary
if self.budget_type == 'epochs':
self.fit_dictionary['epochs'] = self.budget
self.fit_dictionary.pop('runtime', None)
elif self.budget_type == 'runtime':
self.fit_dictionary['runtime'] = self.budget
self.fit_dictionary.pop('epochs', None)
elif self.budget_type == 'resolution' and self.task_type in FORECASTING_TASKS:
self.fit_dictionary['sample_interval'] = int(np.ceil(1.0 / self.budget))
self.fit_dictionary.pop('epochs', None)
self.fit_dictionary.pop('runtime', None)
elif self.budget_type == 'num_seq':
self.fit_dictionary['fraction_seq'] = self.budget
self.fit_dictionary.pop('epochs', None)
self.fit_dictionary.pop('runtime', None)
elif self.budget_type == 'num_sample_per_seq':
self.fit_dictionary['fraction_samples_per_seq'] = self.budget
self.fit_dictionary.pop('epochs', None)
self.fit_dictionary.pop('runtime', None)
else:
raise ValueError(f"budget type must be `epochs` or `runtime` or {FORECASTING_BUDGET_TYPE} "
f"(Only used by forecasting taskss), but got {self.budget_type}")
def _get_pipeline(self) -> BaseEstimator:
"""
Implements a pipeline object based on the self.configuration attribute.
int: A dummy classifier/dummy regressor is created. This estimator serves
as a baseline model to ignore all models that perform worst than this
fixed estimator. Also, in the worst case scenario, this is the final
estimator created (for instance, in case not enough memory was allocated).
str: A pipeline with traditional classifiers like random forest, SVM, etc is created,
as the configuration will contain an estimator name defining the configuration
to use, for example 'RandomForest'
Configuration: A pipeline object matching this configuration is created. This
is the case of neural architecture search, where different backbones
and head can be passed in the form of a configuration object.
Returns
pipeline (BaseEstimator):
A scikit-learn compliant pipeline which is not yet fit to the data.
"""
assert self.pipeline_class is not None, "Can't return pipeline, pipeline_class not initialised"
if isinstance(self.configuration, int):
pipeline = self.pipeline_class(config=self.configuration,
random_state=np.random.RandomState(self.seed),
init_params=self._init_params)
elif isinstance(self.configuration, Configuration):
pipeline = self.pipeline_class(config=self.configuration,
dataset_properties=self.dataset_properties,
random_state=np.random.RandomState(self.seed),
include=self.include,
exclude=self.exclude,
init_params=self._init_params,
search_space_updates=self.search_space_updates)
elif isinstance(self.configuration, str):
pipeline = self.pipeline_class(config=self.configuration,
dataset_properties=self.dataset_properties,
random_state=np.random.RandomState(self.seed),
init_params=self._init_params)
else:
raise ValueError("Invalid configuration entered")
return pipeline
def _loss(self, y_true: np.ndarray, y_hat: np.ndarray, **metric_kwargs: Any) -> Dict[str, float]:
"""SMAC follows a minimization goal, so the make_scorer
sign is used as a guide to obtain the value to reduce.
The calculate_loss internally translate a score function to
a minimization problem
Args:
y_true (np.ndarray):
The expect labels given by the original dataset
y_hat (np.ndarray):
The prediction of the current pipeline being fit
Returns:
(Dict[str, float]):
A dictionary with metric_name -> metric_loss, for every
supported metric
"""
if isinstance(self.configuration, int):
# We do not calculate performance of the dummy configurations
return {self.metric.name: self.metric._optimum - self.metric._sign * self.metric._worst_possible_result}
if self.additional_metrics is not None:
metrics = self.additional_metrics
else:
metrics = [self.metric]
return calculate_loss(
y_true, y_hat, self.task_type, metrics, **metric_kwargs)
def finish_up(self, loss: Dict[str, float], train_loss: Dict[str, float],
opt_pred: np.ndarray, valid_pred: Optional[np.ndarray],
test_pred: Optional[np.ndarray], additional_run_info: Optional[Dict],
file_output: bool, status: StatusType, **metric_kwargs: Any
) -> Optional[Tuple[float, float, int, Dict]]:
"""This function does everything necessary after the fitting is done:
* predicting
* saving the files for the ensembles_statistics
* generate output for SMAC
We use it as the signal handler so we can recycle the code for the
normal usecase and when the runsolver kills us here :)
Args:
loss (Dict[str, float]):
The optimization loss, calculated on the validation set. This will
be the cost used in SMAC
train_loss (Dict[str, float]):
The train loss, calculated on the train set
opt_pred (np.ndarray):
The predictions on the validation set. This validation set is created
from the resampling strategy
valid_pred (Optional[np.ndarray]):
Predictions on a user provided validation set
test_pred (Optional[np.ndarray]):
Predictions on a user provided test set
additional_run_info (Optional[Dict]):
A dictionary with additional run information, like duration or
the crash error msg, if any.
file_output (bool):
Whether or not this pipeline should output information to disk
status (StatusType)
The status of the run, following SMAC StatusType syntax.
metric_kwargs (Any)
Additional arguments for computing metrics
Returns:
duration (float):
The elapsed time of the training of this evaluator
loss (float):
The optimization loss of this run
seed (int):
The seed used while fitting the pipeline
additional_info (Dict):
Additional run information, like train/test loss
"""
self.duration = time.time() - self.starttime
if file_output:
loss_, additional_run_info_ = self.file_output(
opt_pred, valid_pred, test_pred,
)
else:
loss_ = None
additional_run_info_ = {}
validation_loss, test_loss = self.calculate_auxiliary_losses(
valid_pred, test_pred, **metric_kwargs
)
if loss_ is not None:
return self.duration, loss_, self.seed, additional_run_info_
cost = loss[self.metric.name]
additional_run_info = (
{} if additional_run_info is None else additional_run_info
)
additional_run_info['opt_loss'] = loss
additional_run_info['duration'] = self.duration
additional_run_info['num_run'] = self.num_run
if train_loss is not None:
additional_run_info['train_loss'] = train_loss
if validation_loss is not None:
additional_run_info['validation_loss'] = validation_loss
if test_loss is not None:
additional_run_info['test_loss'] = test_loss
rval_dict = {'loss': cost,
'additional_run_info': additional_run_info,
'status': status}
self.queue.put(rval_dict)
return None
def calculate_auxiliary_losses(
self,
Y_valid_pred: np.ndarray,
Y_test_pred: np.ndarray,
**metric_kwargs: Any
) -> Tuple[Optional[Dict[str, float]], Optional[Dict[str, float]]]:
"""
A helper function to calculate the performance estimate of the
current pipeline in the user provided validation/test set.
Args:
Y_valid_pred (np.ndarray):
predictions on a validation set provided by the user,
matching self.y_valid
Y_test_pred (np.ndarray):
predictions on a test set provided by the user,
matching self.y_test
metric_kwargs (Any)
additional argument for evaluating the loss metric
Returns:
validation_loss_dict (Optional[Dict[str, float]]):
Various validation losses available.
test_loss_dict (Optional[Dict[str, float]]):
Various test losses available.
"""
validation_loss_dict: Optional[Dict[str, float]] = None
if Y_valid_pred is not None:
if self.y_valid is not None:
validation_loss_dict = self._loss(self.y_valid, Y_valid_pred, **metric_kwargs)
test_loss_dict: Optional[Dict[str, float]] = None
if Y_test_pred is not None:
if self.y_test is not None:
test_loss_dict = self._loss(self.y_test, Y_test_pred, **metric_kwargs)
return validation_loss_dict, test_loss_dict
def file_output(
self,
Y_optimization_pred: np.ndarray,
Y_valid_pred: np.ndarray,
Y_test_pred: np.ndarray
) -> Tuple[Optional[float], Dict]:
"""
This method decides what file outputs are written to disk.
It is also the interface to the backed save_numrun_to_dir
which stores all the pipeline related information to a single
directory for easy identification of the current run.
Args:
Y_optimization_pred (np.ndarray):
The pipeline predictions on the validation set internally created
from self.y_train
Y_valid_pred (np.ndarray):
The pipeline predictions on the user provided validation set,
which should match self.y_valid
Y_test_pred (np.ndarray):
The pipeline predictions on the user provided test set,
which should match self.y_test
Returns:
loss (Optional[float]):
A loss in case the run failed to store files to
disk
error_dict (Dict):
A dictionary with an error that explains why a run
was not successfully stored to disk.
"""
# Abort if self.Y_optimization is None
# self.Y_optimization can be None if we use partial-cv, then,
# obviously no output should be saved.
if self.Y_optimization is None:
return None, {}
# Abort in case of shape misalignment
if self.Y_optimization.shape[0] != Y_optimization_pred.shape[0]:
return (
1.0,
{
'error':
"Targets %s and prediction %s don't have "
"the same length. Probably training didn't "
"finish" % (self.Y_optimization.shape, Y_optimization_pred.shape)
},
)
# Abort if predictions contain NaNs
for y, s in [
# Y_train_pred deleted here. Fix unittest accordingly.
[Y_optimization_pred, 'optimization'],
[Y_valid_pred, 'validation'],
[Y_test_pred, 'test']
]:
if y is not None and not np.all(np.isfinite(y)):
return (
1.0,
{
'error':
'Model predictions for %s set contains NaNs.' % s
},
)
# Abort if we don't want to output anything.
if 'all' in self.disable_file_output:
return None, {}
# This file can be written independently of the others down bellow
if 'y_optimization' not in self.disable_file_output:
if self.output_y_hat_optimization:
self.backend.save_targets_ensemble(self.Y_optimization)
if getattr(self, 'pipelines', None) is not None:
if self.pipelines[0] is not None and len(self.pipelines) > 0: # type: ignore[index, arg-type]
if 'pipelines' not in self.disable_file_output:
if self.task_type in CLASSIFICATION_TASKS:
pipelines = VotingClassifier(estimators=None, voting='soft', )
else:
pipelines = VotingRegressorWrapper(estimators=None)
pipelines.estimators_ = self.pipelines
else:
pipelines = None
else:
pipelines = None
else:
pipelines = None
if getattr(self, 'pipeline', None) is not None:
if 'pipeline' not in self.disable_file_output:
pipeline = self.pipeline
else:
pipeline = None
else:
pipeline = None
self.logger.debug("Saving directory {}, {}, {}".format(self.seed, self.num_run, self.budget))
self.backend.save_numrun_to_dir(
seed=int(self.seed),
idx=int(self.num_run),
budget=float(self.budget),
model=pipeline,
cv_model=pipelines,
ensemble_predictions=(
Y_optimization_pred if 'y_optimization' not in
self.disable_file_output else None
),
valid_predictions=(
Y_valid_pred if 'y_valid' not in
self.disable_file_output else None
),
test_predictions=(
Y_test_pred if 'y_test' not in
self.disable_file_output else None
),
)
return None, {}
def _predict_proba(self, X: np.ndarray, pipeline: BaseEstimator,
Y_train: Optional[np.ndarray] = None) -> np.ndarray:
"""
A wrapper function to handle the prediction of classification tasks.
It also makes sure that the predictions has the same dimensionality
as the expected labels
Args:
X (np.ndarray):
A set of features to feed to the pipeline
pipeline (BaseEstimator):
A model that will take the features X return a prediction y
This pipeline must be a classification estimator that supports
the predict_proba method.
Y_train (Optional[np.ndarray]):
Returns:
(np.ndarray):
The predictions of pipeline for the given features X
"""
@no_type_check
def send_warnings_to_log(message, category, filename, lineno,
file=None, line=None):
self.logger.debug('%s:%s: %s:%s' %
(filename, lineno, category.__name__, message))
return