Skip to content

Commit 5d6062f

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

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
@@ -213,7 +213,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
213213
"""
214214
Base class for training
215215
Args:
216-
weighted_loss (int, default=0): In case for classification, whether to weight
216+
weighted_loss (bool, default=False): In case for classification, whether to weight
217217
the loss function according to the distribution of classes in the target
218218
use_stochastic_weight_averaging (bool, default=True): whether to use stochastic
219219
weight averaging. Stochastic weight averaging is a simple average of
@@ -228,7 +228,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
228228
random_state:
229229
**lookahead_config:
230230
"""
231-
def __init__(self, weighted_loss: int = 0,
231+
def __init__(self, weighted_loss: bool = False,
232232
use_stochastic_weight_averaging: bool = True,
233233
use_snapshot_ensemble: bool = True,
234234
se_lastk: int = 3,
@@ -596,8 +596,8 @@ def get_hyperparameter_search_space(
596596
dataset_properties: Optional[Dict] = None,
597597
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
598598
hyperparameter="weighted_loss",
599-
value_range=[1],
600-
default_value=1),
599+
value_range=[True, False],
600+
default_value=True),
601601
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
602602
hyperparameter="la_steps",
603603
value_range=(5, 10),
@@ -645,16 +645,9 @@ def get_hyperparameter_search_space(
645645
parent_hyperparameter=parent_hyperparameter
646646
)
647647

648-
"""
648+
# TODO, decouple the weighted loss from the trainer
649649
if dataset_properties is not None:
650650
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
651651
add_hyperparameter(cs, weighted_loss, CategoricalHyperparameter)
652-
"""
653-
# TODO, decouple the weighted loss from the trainer. Uncomment the code above and
654-
# remove the code below. Also update the method signature, so the weighted loss
655-
# is not a constant.
656-
if dataset_properties is not None:
657-
if STRING_TO_TASK_TYPES[dataset_properties['task_type']] in CLASSIFICATION_TASKS:
658-
add_hyperparameter(cs, weighted_loss, Constant)
659652

660653
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
@@ -398,7 +398,7 @@ def test_set_choices_updates(self, fit_dictionary_tabular):
398398
@pytest.mark.parametrize('lr_scheduler', ['CosineAnnealingWarmRestarts',
399399
'ReduceLROnPlateau'])
400400
def test_trainer_cocktails(self, fit_dictionary_tabular, mocker, lr_scheduler, trainer): # noqa F811
401-
fit_dictionary_tabular['epochs'] = 20
401+
fit_dictionary_tabular['epochs'] = 45
402402
fit_dictionary_tabular['early_stopping'] = 20
403403
pipeline = TabularClassificationPipeline(
404404
dataset_properties=fit_dictionary_tabular['dataset_properties'],

0 commit comments

Comments
 (0)