def test_add_network_head(self): """Makes sure that a component can be added to the CS""" # No third party components to start with assert len(base_network_head_choice._addons.components) == 0 # Then make sure the head can be added base_network_head_choice.add_head(DummyHead) assert len(base_network_head_choice._addons.components) == 1 cs = NetworkHeadChoice(dataset_properties={}). \ get_hyperparameter_search_space(dataset_properties={"task_type": "tabular_classification"}) assert "DummyHead" in str(cs) # clear addons base_network_head_choice._addons = ThirdPartyComponents( NetworkHeadComponent)
def test_third_party_component_failure(): _addons = ThirdPartyComponents(autoPyTorchComponent) with pytest.raises(ValueError, match=r"Property required not specified for .*"): _addons.add_component(DummyComponentRequiredFailuire) with pytest.raises( ValueError, match= r"Property must_not_be_there must not be specified for algorithm .*" ): _addons.add_component(DummyComponentExtraPropFailuire) with pytest.raises( TypeError, match=r"add_component works only with a subclass of .*"): _addons.add_component(1)
_traditional_learners = { # Sort by more robust models # Depending on the allocated time budget, only the # top models from this dict are two be fitted. # LGBM is the more robust model, with # internal measures to prevent crashes, overfit # Additionally, it is one of the state of the art # methods for tabular prediction. # Then follow with catboost for categorical heavy # datasets. The other models are complementary and # their ordering is not critical 'lgb': LGBModel, 'catboost': CatboostModel, 'random_forest': RFModel, 'extra_trees': ExtraTreesModel, 'svm': SVMModel, 'knn': KNNModel, } _addons = ThirdPartyComponents(BaseTraditionalLearner) def add_traditional_learner(traditional_learner: BaseTraditionalLearner) -> None: _addons.add_component(traditional_learner) def get_available_traditional_learners() -> Dict[str, Union[Type[BaseTraditionalLearner], Any]]: traditional_learners = dict() traditional_learners.update(_traditional_learners) return traditional_learners
import numpy as np from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.network_embedding.base_network_embedding import ( NetworkEmbeddingComponent, ) directory = os.path.split(__file__)[0] _embeddings = find_components(__package__, directory, NetworkEmbeddingComponent) _addons = ThirdPartyComponents(NetworkEmbeddingComponent) def add_embedding(embedding: NetworkEmbeddingComponent) -> None: _addons.add_component(embedding) class NetworkEmbeddingChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available embedding components Args: None Returns: Dict[str, autoPyTorchComponent]: all NetworkEmbeddingComponents available
from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.preprocessing.image_preprocessing.normalise.base_normalizer import BaseNormalizer normalise_directory = os.path.split(__file__)[0] _normalizers = find_components(__package__, normalise_directory, BaseNormalizer) _addons = ThirdPartyComponents(BaseNormalizer) def add_normalizer(normalizer: BaseNormalizer) -> None: _addons.add_component(normalizer) class NormalizerChoice(autoPyTorchChoice): """ Allows for dynamically choosing normalizer component at runtime """ def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available normalizer components Args:
import numpy as np from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, find_components, ) from autoPyTorch.pipeline.components.setup.augmentation.image.base_image_augmenter import BaseImageAugmenter augmenter_directory = os.path.split(__file__)[0] _augmenters = find_components(__package__, augmenter_directory, BaseImageAugmenter) _addons = ThirdPartyComponents(BaseImageAugmenter) def add_augmenter(augmenter: BaseImageAugmenter) -> None: _addons.add_component(augmenter) def get_components() -> Dict[str, BaseImageAugmenter]: """Returns the available augmenter components Args: None Returns: Dict[str, BaseImageAugmenter]: all BaseImageAugmenter components available as choices
import numpy as np from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.network_backbone.base_network_backbone import ( NetworkBackboneComponent, ) directory = os.path.split(__file__)[0] _backbones = find_components(__package__, directory, NetworkBackboneComponent) _addons = ThirdPartyComponents(NetworkBackboneComponent) def add_backbone(backbone: NetworkBackboneComponent) -> None: _addons.add_component(backbone) class NetworkBackboneChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available backbone components Args: None Returns: Dict[str, autoPyTorchComponent]: all basebackbone components available
import numpy as np from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.network_head.base_network_head import ( NetworkHeadComponent, ) directory = os.path.split(__file__)[0] _heads = find_components(__package__, directory, NetworkHeadComponent) _addons = ThirdPartyComponents(NetworkHeadComponent) def add_head(head: NetworkHeadComponent) -> None: _addons.add_component(head) class NetworkHeadChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available head components Args: None Returns: Dict[str, autoPyTorchComponent]: all NetworkHeadComponents available
import ConfigSpace.hyperparameters as CSH from ConfigSpace.configuration_space import ConfigurationSpace from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.scaling.base_scaler import BaseScaler scaling_directory = os.path.split(__file__)[0] _scalers = find_components(__package__, scaling_directory, BaseScaler) _addons = ThirdPartyComponents(BaseScaler) def add_scaler(scaler: BaseScaler) -> None: _addons.add_component(scaler) class ScalerChoice(autoPyTorchChoice): """ Allows for dynamically choosing scaling component at runtime """ def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available scaler components Args: None
from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.encoding.base_encoder import BaseEncoder encoding_directory = os.path.split(__file__)[0] _encoders = find_components(__package__, encoding_directory, BaseEncoder) _addons = ThirdPartyComponents(BaseEncoder) def add_encoder(encoder: BaseEncoder) -> None: _addons.add_component(encoder) class EncoderChoice(autoPyTorchChoice): """ Allows for dynamically choosing encoding component at runtime """ def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available encoder components Args:
import numpy as np from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.network.base_network import BaseNetworkComponent directory = os.path.split(__file__)[0] _networks = find_components(__package__, directory, BaseNetworkComponent) _addons = ThirdPartyComponents(BaseNetworkComponent) def add_network(network: BaseNetworkComponent) -> None: _addons.add_component(network) class NetworkChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available network components Args: None Returns: Dict[str, autoPyTorchComponent]: all baseNetwork components available as choices
) from autoPyTorch.pipeline.components.training.losses import get_loss_instance from autoPyTorch.pipeline.components.training.metrics.utils import get_metrics from autoPyTorch.pipeline.components.training.trainer.base_trainer import ( BaseTrainerComponent, BudgetTracker, RunSummary, ) from autoPyTorch.utils.common import FitRequirement from autoPyTorch.utils.logging_ import get_named_client_logger trainer_directory = os.path.split(__file__)[0] _trainers = find_components(__package__, trainer_directory, BaseTrainerComponent) _addons = ThirdPartyComponents(BaseTrainerComponent) def add_trainer(trainer: BaseTrainerComponent) -> None: _addons.add_component(trainer) class TrainerChoice(autoPyTorchChoice): """This class is an interface to the PyTorch trainer. To map to pipeline terminology, a choice component will implement the epoch loop through fit, whereas the component who is chosen will dictate how a single epoch happens, that is, how batches of data are fed and used to train the network. """
from ConfigSpace.configuration_space import ConfigurationSpace import numpy as np from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.traditional_ml.base_model import BaseModelComponent directory = os.path.split(__file__)[0] _models = find_components(__package__, directory, BaseModelComponent) _addons = ThirdPartyComponents(BaseModelComponent) def add_model(model: BaseModelComponent) -> None: _addons.add_component(model) class ModelChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available model components Args: None Returns: Dict[str, autoPyTorchComponent]: all baseNetwork components available as choices """
from collections import OrderedDict from typing import Dict, Type from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents ) from autoPyTorch.pipeline.components.setup.network.head.base_head import BaseHead from autoPyTorch.pipeline.components.setup.network.head.fully_connected import FullyConnectedHead from autoPyTorch.pipeline.components.setup.network.head.fully_convolutional import FullyConvolutional2DHead _heads = { FullyConnectedHead.get_name(): FullyConnectedHead, FullyConvolutional2DHead.get_name(): FullyConvolutional2DHead } _addons = ThirdPartyComponents(BaseHead) def add_head(head: BaseHead) -> None: _addons.add_component(head) def get_available_heads() -> Dict[str, Type[BaseHead]]: heads = OrderedDict() heads.update(_heads) heads.update(_addons.components) return heads
from ConfigSpace.configuration_space import ConfigurationSpace from autoPyTorch.datasets.base_dataset import BaseDatasetPropertiesType from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.feature_preprocessing. \ base_feature_preprocessor import autoPyTorchFeaturePreprocessingComponent preprocessing_directory = os.path.split(__file__)[0] _preprocessors = find_components(__package__, preprocessing_directory, autoPyTorchFeaturePreprocessingComponent) _addons = ThirdPartyComponents(autoPyTorchFeaturePreprocessingComponent) def add_feature_preprocessor( feature_preprocessor: autoPyTorchFeaturePreprocessingComponent ) -> None: _addons.add_component(feature_preprocessor) class FeatureProprocessorChoice(autoPyTorchChoice): """ Allows for dynamically choosing feature_preprocessor component at runtime """ def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available feature_preprocessor components
import ConfigSpace.hyperparameters as CSH from ConfigSpace.configuration_space import ConfigurationSpace import numpy as np from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.lr_scheduler.base_scheduler import BaseLRComponent directory = os.path.split(__file__)[0] _schedulers = find_components(__package__, directory, BaseLRComponent) _addons = ThirdPartyComponents(BaseLRComponent) def add_scheduler(scheduler: BaseLRComponent) -> None: _addons.add_component(scheduler) class SchedulerChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available scheduler components Args: None Returns: Dict[str, autoPyTorchComponent]: all baseScheduler components available
import ConfigSpace.hyperparameters as CSH from ConfigSpace.configuration_space import ConfigurationSpace import numpy as np from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.optimizer.base_optimizer import BaseOptimizerComponent directory = os.path.split(__file__)[0] _optimizers = find_components(__package__, directory, BaseOptimizerComponent) _addons = ThirdPartyComponents(BaseOptimizerComponent) def add_optimizer(optimizer: BaseOptimizerComponent) -> None: _addons.add_component(optimizer) class OptimizerChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available optimizer components Args: None Returns: Dict[str, autoPyTorchComponent]: all BaseOptimizerComponents available
import numpy as np from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, autoPyTorchComponent, find_components, ) from autoPyTorch.pipeline.components.setup.network_initializer.base_network_initializer import ( BaseNetworkInitializerComponent) directory = os.path.split(__file__)[0] _initializers = find_components(__package__, directory, BaseNetworkInitializerComponent) _addons = ThirdPartyComponents(BaseNetworkInitializerComponent) def add_network_initializer( initializer: BaseNetworkInitializerComponent) -> None: _addons.add_component(initializer) class NetworkInitializerChoice(autoPyTorchChoice): def get_components(self) -> Dict[str, autoPyTorchComponent]: """Returns the available initializer components Args: None Returns:
from autoPyTorch.pipeline.components.base_component import ( ThirdPartyComponents, ) from autoPyTorch.pipeline.components.setup.network.backbone.base_backbone import BaseBackbone from autoPyTorch.pipeline.components.setup.network.backbone.image import ConvNetImageBackbone, DenseNetBackbone from autoPyTorch.pipeline.components.setup.network.backbone.tabular import MLPBackbone, ResNetBackbone, \ ShapedMLPBackbone from autoPyTorch.pipeline.components.setup.network.backbone.time_series import InceptionTimeBackbone, TCNBackbone _backbones = { ConvNetImageBackbone.get_name(): ConvNetImageBackbone, DenseNetBackbone.get_name(): DenseNetBackbone, ResNetBackbone.get_name(): ResNetBackbone, ShapedMLPBackbone.get_name(): ShapedMLPBackbone, MLPBackbone.get_name(): MLPBackbone, TCNBackbone.get_name(): TCNBackbone, InceptionTimeBackbone.get_name(): InceptionTimeBackbone } _addons = ThirdPartyComponents(BaseBackbone) def add_backbone(backbone: BaseBackbone) -> None: _addons.add_component(backbone) def get_available_backbones() -> Dict[str, Union[Type[BaseBackbone], Any]]: backbones = dict() backbones.update(_backbones) backbones.update(_addons.components) return backbones