Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying the network type in include #78

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -63,7 +63,7 @@ def _add_layer(self, layers: List[nn.Module], in_features: int, out_features: in
layers.append(nn.Dropout(self.config["dropout_%d" % layer_id]))

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'MLPBackbone',
'name': 'MLPBackbone',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -86,7 +86,7 @@ def _add_group(self, in_features: int, out_features: int,
return nn.Sequential(*blocks)

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'ResNetBackbone',
'name': 'ResidualNetworkBackbone',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -68,7 +68,7 @@ def _add_layer(self, layers: List[nn.Module],
layers.append(nn.Dropout(dropout))

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'ShapedMLPBackbone',
'name': 'ShapedMLPBackbone',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -71,7 +71,7 @@ def build_backbone(self, input_shape: Tuple[int, ...]) -> None:
return backbone

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'ShapedResNetBackbone',
'name': 'ShapedResidualNetworkBackbone',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import torch
from torch import nn

from autoPyTorch.constants import CLASSIFICATION_TASKS, STRING_TO_TASK_TYPES
from autoPyTorch.pipeline.components.base_component import BaseEstimator
from autoPyTorch.pipeline.components.base_choice import autoPyTorchChoice
from autoPyTorch.pipeline.components.base_component import (
autoPyTorchComponent,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Tuple
from typing import Any, Dict, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -42,7 +42,7 @@ def build_head(self, input_shape: Tuple[int, ...], output_shape: Tuple[int, ...]
return nn.Sequential(*layers)

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'FullyConnectedHead',
'name': 'FullyConnectedHead',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple, Union

import ConfigSpace as CS
from ConfigSpace.configuration_space import ConfigurationSpace
Expand Down Expand Up @@ -68,7 +68,7 @@ def build_head(self, input_shape: Tuple[int, ...], output_shape: Tuple[int, ...]
for i in range(1, self.config["num_layers"])])

@staticmethod
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, str]:
def get_properties(dataset_properties: Optional[Dict[str, Any]] = None) -> Dict[str, Union[str, bool]]:
return {
'shortname': 'FullyConvolutionalHead',
'name': 'FullyConvolutionalHead',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def get_torchvision_datasets(self) -> Dict[str, torchvision.datasets.VisionDatas
def get_hyperparameter_search_space(dataset_properties: Optional[Dict] = None
) -> ConfigurationSpace:
batch_size = UniformIntegerHyperparameter(
"batch_size", 32, 320, default_value=64)
"batch_size", 16, 512, default_value=64)
cs = ConfigurationSpace()
cs.add_hyperparameters([batch_size])
return cs
Expand Down
65 changes: 65 additions & 0 deletions test/test_pipeline/components/test_setup_networks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import sys

import pytest

import torch

from autoPyTorch.pipeline.tabular_classification import TabularClassificationPipeline


# Disable
def blockPrint():
sys.stdout = open(os.devnull, 'w')


# Restore
def enablePrint():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this haha

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as the traditional classifiers had way too many things printed but then I added it to base model I think when the classifiers are fitted. I'll remove them from here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh but this is useless here :P

sys.stdout = sys.__stdout__


@pytest.fixture(params=['MLPBackbone', 'ResNetBackbone', 'ShapedMLPBackbone', 'ShapedResNetBackbone'])
def backbone(request):
return request.param


@pytest.fixture(params=['fully_connected'])
def head(request):
return request.param


@pytest.mark.parametrize("fit_dictionary", ['fit_dictionary_numerical_only',
'fit_dictionary_categorical_only',
'fit_dictionary_num_and_categorical'], indirect=True)
class TestNetworks:
def test_pipeline_fit(self, fit_dictionary, backbone, head):
"""This test makes sure that the pipeline is able to fit
given random combinations of hyperparameters across the pipeline"""

pipeline = TabularClassificationPipeline(
dataset_properties=fit_dictionary['dataset_properties'],
include={'network_backbone': [backbone], 'network_head': [head]})
cs = pipeline.get_hyperparameter_search_space()
config = cs.get_default_configuration()

assert backbone == config.get('network_backbone:__choice__', None)
assert head == config.get('network_head:__choice__', None)
pipeline.set_hyperparameters(config)
pipeline.fit(fit_dictionary)

# To make sure we fitted the model, there should be a
# run summary object with accuracy
run_summary = pipeline.named_steps['trainer'].run_summary
assert run_summary is not None

# Make sure that performance was properly captured
assert run_summary.performance_tracker['train_loss'][1] > 0
assert run_summary.total_parameter_count > 0
assert 'accuracy' in run_summary.performance_tracker['train_metrics'][1]

# Commented out the next line as some pipelines are not
# achieving this accuracy with default configuration and 10 epochs
# To be added once we fix the search space
# assert run_summary.performance_tracker['val_metrics'][fit_dictionary['epochs']]['accuracy'] >= 0.8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you create an issue for this? Yeah I think this is one of the most important things to fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

# Make sure a network was fit
assert isinstance(pipeline.named_steps['network'].get_network(), torch.nn.Module)