Skip to content

Commit cda66c8

Browse files
committed
refactor base_pipeline forbidden conditions
1 parent 989f3ac commit cda66c8

File tree

4 files changed

+68
-102
lines changed

4 files changed

+68
-102
lines changed

autoPyTorch/pipeline/base_pipeline.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from copy import copy
12
import warnings
23
from abc import ABCMeta
34
from collections import Counter
45
from typing import Any, Dict, List, Optional, Tuple, Union
56

67
from ConfigSpace import Configuration
78
from ConfigSpace.configuration_space import ConfigurationSpace
9+
from ConfigSpace.forbidden import ForbiddenAndConjunction, ForbiddenEqualsClause
810

911
import numpy as np
1012

@@ -295,6 +297,67 @@ def _get_hyperparameter_search_space(self,
295297
"""
296298
raise NotImplementedError()
297299

300+
def _add_forbidden_conditions(self, cs):
301+
"""
302+
Add forbidden conditions to ensure valid configurations.
303+
Currently, Learned Entity Embedding is only valid when encoder is one hot encoder
304+
and CyclicLR is disabled when using stochastic weight averaging and snapshot
305+
ensembling.
306+
307+
Args:
308+
cs (ConfigurationSpace):
309+
Configuration space to which forbidden conditions are added.
310+
311+
"""
312+
313+
# Learned Entity Embedding is only valid when encoder is one hot encoder
314+
if 'network_embedding' in self.named_steps.keys() and 'encoder' in self.named_steps.keys():
315+
embeddings = cs.get_hyperparameter('network_embedding:__choice__').choices
316+
if 'LearnedEntityEmbedding' in embeddings:
317+
encoders = cs.get_hyperparameter('encoder:__choice__').choices
318+
possible_default_embeddings = copy(list(embeddings))
319+
del possible_default_embeddings[possible_default_embeddings.index('LearnedEntityEmbedding')]
320+
321+
for encoder in encoders:
322+
if encoder == 'OneHotEncoder':
323+
continue
324+
while True:
325+
try:
326+
cs.add_forbidden_clause(ForbiddenAndConjunction(
327+
ForbiddenEqualsClause(cs.get_hyperparameter(
328+
'network_embedding:__choice__'), 'LearnedEntityEmbedding'),
329+
ForbiddenEqualsClause(cs.get_hyperparameter('encoder:__choice__'), encoder)
330+
))
331+
break
332+
except ValueError:
333+
# change the default and try again
334+
try:
335+
default = possible_default_embeddings.pop()
336+
except IndexError:
337+
raise ValueError("Cannot find a legal default configuration")
338+
cs.get_hyperparameter('network_embedding:__choice__').default_value = default
339+
340+
# Disable CyclicLR until todo is completed.
341+
if 'lr_scheduler' in self.named_steps.keys() and 'trainer' in self.named_steps.keys():
342+
trainers = cs.get_hyperparameter('trainer:__choice__').choices
343+
for trainer in trainers:
344+
available_schedulers = cs.get_hyperparameter('lr_scheduler:__choice__').choices
345+
# TODO: update cyclic lr to use n_restarts and adjust according to batch size
346+
cyclic_lr_name = 'CyclicLR'
347+
if cyclic_lr_name in available_schedulers:
348+
# disable snapshot ensembles and stochastic weight averaging
349+
cs.add_forbidden_clause(ForbiddenAndConjunction(
350+
ForbiddenEqualsClause(cs.get_hyperparameter(
351+
f'trainer:{trainer}:use_snapshot_ensemble'), True),
352+
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
353+
))
354+
cs.add_forbidden_clause(ForbiddenAndConjunction(
355+
ForbiddenEqualsClause(cs.get_hyperparameter(
356+
f'trainer:{trainer}:use_stochastic_weight_averaging'), True),
357+
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
358+
))
359+
return cs
360+
298361
def __repr__(self) -> str:
299362
"""Retrieves a str representation of the current pipeline
300363

autoPyTorch/pipeline/image_classification.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def _get_hyperparameter_search_space(self,
156156

157157
# Here we add custom code, like this with this
158158
# is not a valid configuration
159+
cs = self._add_forbidden_conditions(cs)
159160

160161
self.configuration_space = cs
161162
self.dataset_properties = dataset_properties

autoPyTorch/pipeline/tabular_classification.py

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Any, Dict, List, Optional, Tuple, Union
44

55
from ConfigSpace.configuration_space import Configuration, ConfigurationSpace
6-
from ConfigSpace.forbidden import ForbiddenAndConjunction, ForbiddenEqualsClause
76

87
import numpy as np
98

@@ -261,56 +260,9 @@ def _get_hyperparameter_search_space(self,
261260
cs=cs, dataset_properties=dataset_properties,
262261
exclude=exclude, include=include, pipeline=self.steps)
263262

264-
# Here we add custom code, that is used to ensure valid configurations, For example
265-
# Learned Entity Embedding is only valid when encoder is one hot encoder
266-
if 'network_embedding' in self.named_steps.keys() and 'encoder' in self.named_steps.keys():
267-
embeddings = cs.get_hyperparameter('network_embedding:__choice__').choices
268-
if 'LearnedEntityEmbedding' in embeddings:
269-
encoders = cs.get_hyperparameter('encoder:__choice__').choices
270-
possible_default_embeddings = copy.copy(list(embeddings))
271-
del possible_default_embeddings[possible_default_embeddings.index('LearnedEntityEmbedding')]
272-
273-
for encoder in encoders:
274-
if encoder == 'OneHotEncoder':
275-
continue
276-
while True:
277-
try:
278-
cs.add_forbidden_clause(ForbiddenAndConjunction(
279-
ForbiddenEqualsClause(cs.get_hyperparameter(
280-
'network_embedding:__choice__'), 'LearnedEntityEmbedding'),
281-
ForbiddenEqualsClause(cs.get_hyperparameter('encoder:__choice__'), encoder)
282-
))
283-
break
284-
except ValueError:
285-
# change the default and try again
286-
try:
287-
default = possible_default_embeddings.pop()
288-
except IndexError:
289-
raise ValueError("Cannot find a legal default configuration")
290-
cs.get_hyperparameter('network_embedding:__choice__').default_value = default
291-
292-
# Disable CyclicLR until todo is completed.
293-
if 'lr_scheduler' in self.named_steps.keys() and 'trainer' in self.named_steps.keys():
294-
trainers = cs.get_hyperparameter('trainer:__choice__').choices
295-
for trainer in trainers:
296-
available_schedulers = self.named_steps['lr_scheduler'].get_available_components(
297-
dataset_properties=dataset_properties,
298-
exclude=exclude if bool(exclude) else None,
299-
include=include if bool(include) else None)
300-
# TODO: update cyclic lr to use n_restarts and adjust according to batch size
301-
cyclic_lr_name = 'CyclicLR'
302-
if cyclic_lr_name in available_schedulers:
303-
# disable snapshot ensembles and stochastic weight averaging
304-
cs.add_forbidden_clause(ForbiddenAndConjunction(
305-
ForbiddenEqualsClause(cs.get_hyperparameter(
306-
f'trainer:{trainer}:use_snapshot_ensemble'), True),
307-
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
308-
))
309-
cs.add_forbidden_clause(ForbiddenAndConjunction(
310-
ForbiddenEqualsClause(cs.get_hyperparameter(
311-
f'trainer:{trainer}:use_stochastic_weight_averaging'), True),
312-
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
313-
))
263+
# Here we add custom code, like this with this
264+
# is not a valid configuration
265+
cs = self._add_forbidden_conditions(cs)
314266

315267
self.configuration_space = cs
316268
self.dataset_properties = dataset_properties

autoPyTorch/pipeline/tabular_regression.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Any, Dict, List, Optional, Tuple, Union
44

55
from ConfigSpace.configuration_space import Configuration, ConfigurationSpace
6-
from ConfigSpace.forbidden import ForbiddenAndConjunction, ForbiddenEqualsClause
76

87
import numpy as np
98

@@ -210,56 +209,7 @@ def _get_hyperparameter_search_space(self,
210209

211210
# Here we add custom code, like this with this
212211
# is not a valid configuration
213-
# Learned Entity Embedding is only valid when encoder is one hot encoder
214-
if 'network_embedding' in self.named_steps.keys() and 'encoder' in self.named_steps.keys():
215-
embeddings = cs.get_hyperparameter('network_embedding:__choice__').choices
216-
if 'LearnedEntityEmbedding' in embeddings:
217-
encoders = cs.get_hyperparameter('encoder:__choice__').choices
218-
default = cs.get_hyperparameter('network_embedding:__choice__').default_value
219-
possible_default_embeddings = copy.copy(list(embeddings))
220-
del possible_default_embeddings[possible_default_embeddings.index(default)]
221-
222-
for encoder in encoders:
223-
if encoder == 'OneHotEncoder':
224-
continue
225-
while True:
226-
try:
227-
cs.add_forbidden_clause(ForbiddenAndConjunction(
228-
ForbiddenEqualsClause(cs.get_hyperparameter(
229-
'network_embedding:__choice__'), 'LearnedEntityEmbedding'),
230-
ForbiddenEqualsClause(cs.get_hyperparameter('encoder:__choice__'), encoder)
231-
))
232-
break
233-
except ValueError:
234-
# change the default and try again
235-
try:
236-
default = possible_default_embeddings.pop()
237-
except IndexError:
238-
raise ValueError("Cannot find a legal default configuration")
239-
cs.get_hyperparameter('network_embedding:__choice__').default_value = default
240-
241-
# Disable CyclicLR until todo is completed.
242-
if 'lr_scheduler' in self.named_steps.keys() and 'trainer' in self.named_steps.keys():
243-
trainers = cs.get_hyperparameter('trainer:__choice__').choices
244-
for trainer in trainers:
245-
available_schedulers = self.named_steps['lr_scheduler'].get_available_components(
246-
dataset_properties=dataset_properties,
247-
exclude=exclude if bool(exclude) else None,
248-
include=include if bool(include) else None)
249-
# TODO: update cyclic lr to use n_restarts and adjust according to batch size
250-
cyclic_lr_name = 'CyclicLR'
251-
if cyclic_lr_name in available_schedulers:
252-
# disable snapshot ensembles and stochastic weight averaging
253-
cs.add_forbidden_clause(ForbiddenAndConjunction(
254-
ForbiddenEqualsClause(cs.get_hyperparameter(
255-
f'trainer:{trainer}:use_snapshot_ensemble'), True),
256-
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
257-
))
258-
cs.add_forbidden_clause(ForbiddenAndConjunction(
259-
ForbiddenEqualsClause(cs.get_hyperparameter(
260-
f'trainer:{trainer}:use_stochastic_weight_averaging'), True),
261-
ForbiddenEqualsClause(cs.get_hyperparameter('lr_scheduler:__choice__'), cyclic_lr_name)
262-
))
212+
cs = self._add_forbidden_conditions(cs)
263213

264214
self.configuration_space = cs
265215
self.dataset_properties = dataset_properties

0 commit comments

Comments
 (0)