Exemple #1
0
def test_algorithm_hyperparameter_integer_range_valid_range(session):
    hyperparameters = [{
        "Description":
        "Grow a tree with max_leaf_nodes in best-first fashion.",
        "Type": "Integer",
        "Name": "max_leaf_nodes",
        "Range": {
            "IntegerParameterRangeSpecification": {
                "MinValue": "1",
                "MaxValue": "100000"
            }
        },
        "IsTunable": True,
        "IsRequired": False,
        "DefaultValue": "100",
    }]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo["TrainingSpecification"][
        "SupportedHyperParameters"] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        "arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees",
        role="SageMakerRole",
        train_instance_type="ml.m4.2xlarge",
        train_instance_count=1,
        sagemaker_session=session,
    )

    estimator.set_hyperparameters(max_leaf_nodes=1)
    estimator.set_hyperparameters(max_leaf_nodes=100000)
Exemple #2
0
def test_algorithm_hyperparameter_continuous_range_invalid_range(sagemaker_session):
    hyperparameters = [
        {
            'Description': 'A continuous hyperparameter',
            'Type': 'Continuous',
            'Name': 'max_leaf_nodes',
            'Range': {
                'ContinuousParameterRangeSpecification': {'MinValue': '0.0', 'MaxValue': '1.0'}
            },
            'IsTunable': True,
            'IsRequired': False,
            'DefaultValue': '100',
        }
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo['TrainingSpecification']['SupportedHyperParameters'] = hyperparameters

    sagemaker_session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn='arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees',
        role='SageMakerRole',
        train_instance_type='ml.m4.2xlarge',
        train_instance_count=1,
        sagemaker_session=sagemaker_session,
    )

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(max_leaf_nodes=1.1)

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(max_leaf_nodes=-0.1)
def test_algorithm_hyperparameter_integer_range_valid_range(session):
    hyperparameters = [{
        'Description':
        'Grow a tree with max_leaf_nodes in best-first fashion.',
        'Type': 'Integer',
        'Name': 'max_leaf_nodes',
        'Range': {
            'IntegerParameterRangeSpecification': {
                'MinValue': '1',
                'MaxValue': '100000'
            }
        },
        'IsTunable': True,
        'IsRequired': False,
        'DefaultValue': '100',
    }]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo['TrainingSpecification'][
        'SupportedHyperParameters'] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        'arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees',
        role='SageMakerRole',
        train_instance_type='ml.m4.2xlarge',
        train_instance_count=1,
        sagemaker_session=session,
    )

    estimator.set_hyperparameters(max_leaf_nodes=1)
    estimator.set_hyperparameters(max_leaf_nodes=100000)
Exemple #4
0
def test_algorithm_hyperparameter_continuous_range_invalid_range(session):
    hyperparameters = [
        {
            "Description": "A continuous hyperparameter",
            "Type": "Continuous",
            "Name": "max_leaf_nodes",
            "Range": {
                "ContinuousParameterRangeSpecification": {"MinValue": "0.0", "MaxValue": "1.0"}
            },
            "IsTunable": True,
            "IsRequired": False,
            "DefaultValue": "100",
        }
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo["TrainingSpecification"]["SupportedHyperParameters"] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn="arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees",
        role="SageMakerRole",
        instance_type="ml.m4.2xlarge",
        instance_count=1,
        sagemaker_session=session,
    )

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(max_leaf_nodes=1.1)

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(max_leaf_nodes=-0.1)
Exemple #5
0
def test_algorithm_hyperparameter_categorical_range(sagemaker_session):
    hyperparameters = [
        {
            'Description': 'A continuous hyperparameter',
            'Type': 'Categorical',
            'Name': 'hp1',
            'Range': {'CategoricalParameterRangeSpecification': {'Values': ['TF', 'MXNet']}},
            'IsTunable': True,
            'IsRequired': False,
            'DefaultValue': '100',
        }
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo['TrainingSpecification']['SupportedHyperParameters'] = hyperparameters

    sagemaker_session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn='arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees',
        role='SageMakerRole',
        train_instance_type='ml.m4.2xlarge',
        train_instance_count=1,
        sagemaker_session=sagemaker_session,
    )

    estimator.set_hyperparameters(hp1='MXNet')
    estimator.set_hyperparameters(hp1='TF')

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp1='Chainer')

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp1='MxNET')
Exemple #6
0
def test_algorithm_hyperparameter_categorical_range(session):
    hyperparameters = [
        {
            "Description": "A continuous hyperparameter",
            "Type": "Categorical",
            "Name": "hp1",
            "Range": {"CategoricalParameterRangeSpecification": {"Values": ["TF", "MXNet"]}},
            "IsTunable": True,
            "IsRequired": False,
            "DefaultValue": "100",
        }
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo["TrainingSpecification"]["SupportedHyperParameters"] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn="arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees",
        role="SageMakerRole",
        train_instance_type="ml.m4.2xlarge",
        train_instance_count=1,
        sagemaker_session=session,
    )

    estimator.set_hyperparameters(hp1="MXNet")
    estimator.set_hyperparameters(hp1="TF")

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp1="Chainer")

    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp1="MxNET")
Exemple #7
0
def test_algorithm_required_hyperparameters_are_provided(session):
    hyperparameters = [
        {
            "Description": "A categorical hyperparameter",
            "Type": "Categorical",
            "Name": "hp1",
            "Range": {
                "CategoricalParameterRangeSpecification": {
                    "Values": ["TF", "MXNet"]
                }
            },
            "IsTunable": True,
            "IsRequired": True,
        },
        {
            "Name": "hp2",
            "Description": "A categorical hyperparameter",
            "Type": "Categorical",
            "IsTunable": False,
            "IsRequired": True,
        },
        {
            "Name": "free_text_hp1",
            "Description": "You can write anything here",
            "Type": "FreeText",
            "IsTunable": False,
            "IsRequired": True,
        },
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo["TrainingSpecification"][
        "SupportedHyperParameters"] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        "arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees",
        role="SageMakerRole",
        train_instance_type="ml.m4.2xlarge",
        train_instance_count=1,
        sagemaker_session=session,
    )

    # All 3 Hyperparameters are provided
    estimator.set_hyperparameters(hp1="TF", hp2="TF2", free_text_hp1="Hello!")
Exemple #8
0
def test_algorithm_required_hyperparameters_not_provided(session):
    hyperparameters = [
        {
            "Description": "A continuous hyperparameter",
            "Type": "Categorical",
            "Name": "hp1",
            "Range": {
                "CategoricalParameterRangeSpecification": {
                    "Values": ["TF", "MXNet"]
                }
            },
            "IsTunable": True,
            "IsRequired": True,
        },
        {
            "Name": "hp2",
            "Description": "A continuous hyperparameter",
            "Type": "Categorical",
            "IsTunable": False,
            "IsRequired": True,
        },
    ]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo["TrainingSpecification"][
        "SupportedHyperParameters"] = hyperparameters

    session.sagemaker_client.describe_algorithm = Mock(return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        "arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees",
        role="SageMakerRole",
        train_instance_type="ml.m4.2xlarge",
        train_instance_count=1,
        sagemaker_session=session,
    )

    # hp1 is required and was not provided
    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp2="TF2")

    # Calling fit with unset required hyperparameters should fail
    # this covers the use case of not calling set_hyperparameters() explicitly
    with pytest.raises(ValueError):
        estimator.fit({"training": "s3://some/place"})
def test_algorithm_required_hyperparameters_are_provided(sagemaker_session):
    hyperparameters = [{
        'Description': 'A categorical hyperparameter',
        'Type': 'Categorical',
        'Name': 'hp1',
        'Range': {
            'CategoricalParameterRangeSpecification': {
                'Values': ['TF', 'MXNet']
            }
        },
        'IsTunable': True,
        'IsRequired': True,
    }, {
        'Name': 'hp2',
        'Description': 'A categorical hyperparameter',
        'Type': 'Categorical',
        'IsTunable': False,
        'IsRequired': True
    }, {
        'Name': 'free_text_hp1',
        'Description': 'You can write anything here',
        'Type': 'FreeText',
        'IsTunable': False,
        'IsRequired': True
    }]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo['TrainingSpecification'][
        'SupportedHyperParameters'] = hyperparameters

    sagemaker_session.sagemaker_client.describe_algorithm = Mock(
        return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        'arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees',
        role='SageMakerRole',
        train_instance_type='ml.m4.2xlarge',
        train_instance_count=1,
        sagemaker_session=sagemaker_session,
    )

    # All 3 Hyperparameters are provided
    estimator.set_hyperparameters(hp1='TF', hp2='TF2', free_text_hp1='Hello!')
def test_algorithm_required_hyperparameters_not_provided(sagemaker_session):
    hyperparameters = [{
        'Description': 'A continuous hyperparameter',
        'Type': 'Categorical',
        'Name': 'hp1',
        'Range': {
            'CategoricalParameterRangeSpecification': {
                'Values': ['TF', 'MXNet']
            }
        },
        'IsTunable': True,
        'IsRequired': True,
    }, {
        'Name': 'hp2',
        'Description': 'A continuous hyperparameter',
        'Type': 'Categorical',
        'IsTunable': False,
        'IsRequired': True
    }]

    some_algo = copy.deepcopy(DESCRIBE_ALGORITHM_RESPONSE)
    some_algo['TrainingSpecification'][
        'SupportedHyperParameters'] = hyperparameters

    sagemaker_session.sagemaker_client.describe_algorithm = Mock(
        return_value=some_algo)

    estimator = AlgorithmEstimator(
        algorithm_arn=
        'arn:aws:sagemaker:us-east-2:1234:algorithm/scikit-decision-trees',
        role='SageMakerRole',
        train_instance_type='ml.m4.2xlarge',
        train_instance_count=1,
        sagemaker_session=sagemaker_session,
    )

    # hp1 is required and was not provided
    with pytest.raises(ValueError):
        estimator.set_hyperparameters(hp2='TF2')

    # Calling fit with unset required hyperparameters should fail
    # this covers the use case of not calling set_hyperparameters() explicitly
    with pytest.raises(ValueError):
        estimator.fit({'training': 's3://some/place'})