Skip to content

Commit b70de2b

Browse files
committed
Change weighted loss to categorical and fix for test adversarial trainer (#214)
1 parent b274dee commit b70de2b

File tree

6 files changed

+20
-49
lines changed

6 files changed

+20
-49
lines changed

autoPyTorch/pipeline/components/training/trainer/AdversarialTrainer.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AdversarialTrainer(BaseTrainerComponent):
2424
def __init__(
2525
self,
2626
epsilon: float,
27-
weighted_loss: int = 0,
27+
weighted_loss: bool = False,
2828
random_state: Optional[np.random.RandomState] = None,
2929
use_stochastic_weight_averaging: bool = False,
3030
use_snapshot_ensemble: bool = False,
@@ -159,8 +159,8 @@ def get_hyperparameter_search_space(
159159
dataset_properties: Optional[Dict] = None,
160160
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
161161
hyperparameter="weighted_loss",
162-
value_range=[1],
163-
default_value=1),
162+
value_range=[True, False],
163+
default_value=True),
164164
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
165165
hyperparameter="la_steps",
166166
value_range=(5, 10),
@@ -226,16 +226,9 @@ def get_hyperparameter_search_space(
226226
parent_hyperparameter=parent_hyperparameter
227227
)
228228

229-
"""
229+
# TODO, decouple the weighted loss from the trainer
230230
if dataset_properties is not None:
231231
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
232232
add_hyperparameter(cs, weighted_loss, CategoricalHyperparameter)
233-
"""
234-
# TODO, decouple the weighted loss from the trainer. Uncomment the code above and
235-
# remove the code below. Also update the method signature, so the weighted loss
236-
# is not a constant.
237-
if dataset_properties is not None:
238-
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
239-
add_hyperparameter(cs, weighted_loss, Constant)
240233

241234
return cs

autoPyTorch/pipeline/components/training/trainer/StandardTrainer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414

1515
class StandardTrainer(BaseTrainerComponent):
16-
def __init__(self, weighted_loss: int = 0,
16+
def __init__(self,
17+
weighted_loss: bool = False,
1718
use_stochastic_weight_averaging: bool = False,
1819
use_snapshot_ensemble: bool = False,
1920
se_lastk: int = 3,

autoPyTorch/pipeline/components/training/trainer/base_trainer.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
206206
"""
207207
Base class for training
208208
Args:
209-
weighted_loss (int, default=0): In case for classification, whether to weight
209+
weighted_loss (bool, default=False): In case for classification, whether to weight
210210
the loss function according to the distribution of classes in the target
211211
use_stochastic_weight_averaging (bool, default=True): whether to use stochastic
212212
weight averaging. Stochastic weight averaging is a simple average of
@@ -221,7 +221,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
221221
random_state:
222222
**lookahead_config:
223223
"""
224-
def __init__(self, weighted_loss: int = 0,
224+
def __init__(self, weighted_loss: bool = False,
225225
use_stochastic_weight_averaging: bool = True,
226226
use_snapshot_ensemble: bool = True,
227227
se_lastk: int = 3,
@@ -587,8 +587,8 @@ def get_hyperparameter_search_space(
587587
dataset_properties: Optional[Dict] = None,
588588
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
589589
hyperparameter="weighted_loss",
590-
value_range=[1],
591-
default_value=1),
590+
value_range=[True, False],
591+
default_value=True),
592592
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
593593
hyperparameter="la_steps",
594594
value_range=(5, 10),
@@ -636,16 +636,9 @@ def get_hyperparameter_search_space(
636636
parent_hyperparameter=parent_hyperparameter
637637
)
638638

639-
"""
639+
# TODO, decouple the weighted loss from the trainer
640640
if dataset_properties is not None:
641641
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
642642
add_hyperparameter(cs, weighted_loss, CategoricalHyperparameter)
643-
"""
644-
# TODO, decouple the weighted loss from the trainer. Uncomment the code above and
645-
# remove the code below. Also update the method signature, so the weighted loss
646-
# is not a constant.
647-
if dataset_properties is not None:
648-
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
649-
add_hyperparameter(cs, weighted_loss, Constant)
650643

651644
return cs

autoPyTorch/pipeline/components/training/trainer/cutout_utils.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class CutOut:
2121
def __init__(self, patch_ratio: float,
2222
cutout_prob: float,
23-
weighted_loss: int = 0,
23+
weighted_loss: bool = False,
2424
random_state: Optional[np.random.RandomState] = None,
2525
use_stochastic_weight_averaging: bool = False,
2626
use_snapshot_ensemble: bool = False,
@@ -63,9 +63,8 @@ def get_hyperparameter_search_space(
6363
dataset_properties: Optional[Dict] = None,
6464
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
6565
hyperparameter="weighted_loss",
66-
value_range=[1],
67-
default_value=1
68-
),
66+
value_range=[True, False],
67+
default_value=True),
6968
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
7069
hyperparameter="la_steps",
7170
value_range=(5, 10),
@@ -137,16 +136,9 @@ def get_hyperparameter_search_space(
137136
parent_hyperparameter=parent_hyperparameter
138137
)
139138

140-
"""
139+
# TODO, decouple the weighted loss from the trainer
141140
if dataset_properties is not None:
142141
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
143142
add_hyperparameter(cs, weighted_loss, CategoricalHyperparameter)
144-
"""
145-
# TODO, decouple the weighted loss from the trainer. Uncomment the code above and
146-
# remove the code below. Also update the method signature, so the weighted loss
147-
# is not a constant.
148-
if dataset_properties is not None:
149-
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
150-
add_hyperparameter(cs, weighted_loss, Constant)
151143

152144
return cs

autoPyTorch/pipeline/components/training/trainer/mixup_utils.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class MixUp:
2121
def __init__(self, alpha: float,
22-
weighted_loss: int = 0,
22+
weighted_loss: bool = False,
2323
random_state: Optional[np.random.RandomState] = None,
2424
use_stochastic_weight_averaging: bool = False,
2525
use_snapshot_ensemble: bool = False,
@@ -61,9 +61,8 @@ def get_hyperparameter_search_space(
6161
dataset_properties: Optional[Dict] = None,
6262
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
6363
hyperparameter="weighted_loss",
64-
value_range=[1],
65-
default_value=1
66-
),
64+
value_range=[True, False],
65+
default_value=True),
6766
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
6867
hyperparameter="la_steps",
6968
value_range=(5, 10),
@@ -128,16 +127,9 @@ def get_hyperparameter_search_space(
128127
la_config_space,
129128
parent_hyperparameter=parent_hyperparameter
130129
)
131-
"""
130+
# TODO, decouple the weighted loss from the trainer
132131
if dataset_properties is not None:
133132
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
134133
add_hyperparameter(cs, weighted_loss, CategoricalHyperparameter)
135-
"""
136-
# TODO, decouple the weighted loss from the trainer. Uncomment the code above and
137-
# remove the code below. Also update the method signature, so the weighted loss
138-
# is not a constant.
139-
if dataset_properties is not None:
140-
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
141-
add_hyperparameter(cs, weighted_loss, Constant)
142134

143135
return cs

test/test_pipeline/test_tabular_classification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_set_choices_updates(self, fit_dictionary_tabular):
380380
@pytest.mark.parametrize('lr_scheduler', ['CosineAnnealingWarmRestarts',
381381
'ReduceLROnPlateau'])
382382
def test_trainer_cocktails(self, fit_dictionary_tabular, mocker, lr_scheduler, trainer): # noqa F811
383-
fit_dictionary_tabular['epochs'] = 20
383+
fit_dictionary_tabular['epochs'] = 45
384384
fit_dictionary_tabular['early_stopping'] = 20
385385
pipeline = TabularClassificationPipeline(
386386
dataset_properties=fit_dictionary_tabular['dataset_properties'],

0 commit comments

Comments
 (0)