Skip to content

Commit c4c3c12

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

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
@@ -194,7 +194,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
194194
"""
195195
Base class for training
196196
Args:
197-
weighted_loss (int, default=0): In case for classification, whether to weight
197+
weighted_loss (bool, default=False): In case for classification, whether to weight
198198
the loss function according to the distribution of classes in the target
199199
use_stochastic_weight_averaging (bool, default=True): whether to use stochastic
200200
weight averaging. Stochastic weight averaging is a simple average of
@@ -209,7 +209,7 @@ class BaseTrainerComponent(autoPyTorchTrainingComponent):
209209
random_state:
210210
**lookahead_config:
211211
"""
212-
def __init__(self, weighted_loss: int = 0,
212+
def __init__(self, weighted_loss: bool = False,
213213
use_stochastic_weight_averaging: bool = True,
214214
use_snapshot_ensemble: bool = True,
215215
se_lastk: int = 3,
@@ -572,8 +572,8 @@ def get_hyperparameter_search_space(
572572
dataset_properties: Optional[Dict] = None,
573573
weighted_loss: HyperparameterSearchSpace = HyperparameterSearchSpace(
574574
hyperparameter="weighted_loss",
575-
value_range=[1],
576-
default_value=1),
575+
value_range=[True, False],
576+
default_value=True),
577577
la_steps: HyperparameterSearchSpace = HyperparameterSearchSpace(
578578
hyperparameter="la_steps",
579579
value_range=(5, 10),
@@ -634,16 +634,9 @@ def get_hyperparameter_search_space(
634634
parent_hyperparameter=parent_hyperparameter
635635
)
636636

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

649642
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
@@ -379,7 +379,7 @@ def test_set_choices_updates(self, fit_dictionary_tabular):
379379
@pytest.mark.parametrize('lr_scheduler', ['CosineAnnealingWarmRestarts',
380380
'ReduceLROnPlateau'])
381381
def test_trainer_cocktails(self, fit_dictionary_tabular, mocker, lr_scheduler, trainer): # noqa F811
382-
fit_dictionary_tabular['epochs'] = 20
382+
fit_dictionary_tabular['epochs'] = 45
383383
fit_dictionary_tabular['early_stopping'] = 20
384384
pipeline = TabularClassificationPipeline(
385385
dataset_properties=fit_dictionary_tabular['dataset_properties'],

0 commit comments

Comments
 (0)