How to use the sagemaker.tuner.HyperparameterTuner function in sagemaker

To help you get started, we’ve selected a few sagemaker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / sagemaker-python-sdk / tests / integ / test_tf_efs_fsx.py View on Github external
entry_point=SCRIPT,
        role=role,
        train_instance_count=1,
        train_instance_type=cpu_instance_type,
        script_mode=True,
        sagemaker_session=sagemaker_session,
        py_version=PY_VERSION,
        framework_version=TensorFlow.LATEST_VERSION,
        subnets=subnets,
        security_group_ids=security_group_ids,
    )

    hyperparameter_ranges = {"epochs": IntegerParameter(1, 2)}
    objective_metric_name = "accuracy"
    metric_definitions = [{"Name": objective_metric_name, "Regex": "accuracy = ([0-9\\.]+)"}]
    tuner = HyperparameterTuner(
        estimator,
        objective_metric_name,
        hyperparameter_ranges,
        metric_definitions,
        max_jobs=MAX_JOBS,
        max_parallel_jobs=MAX_PARALLEL_JOBS,
    )

    file_system_fsx_id = efs_fsx_setup["file_system_fsx_id"]
    file_system_input = FileSystemInput(
        file_system_id=file_system_fsx_id, file_system_type="FSxLustre", directory_path=FSX_DIR_PATH
    )

    with timeout(minutes=TUNING_DEFAULT_TIMEOUT_MINUTES):
        tuning_job_name = unique_name_from_base("test-tuning-tf-script-mode-lustre", max_length=32)
        tuner.fit(file_system_input, job_name=tuning_job_name)
github aws / sagemaker-python-sdk / tests / integ / test_tuner.py View on Github external
def _tune(
    kmeans_estimator,
    kmeans_train_set,
    tuner=None,
    hyperparameter_ranges=None,
    job_name=None,
    warm_start_config=None,
    wait_till_terminal=True,
    max_jobs=2,
    max_parallel_jobs=2,
    early_stopping_type="Off",
):
    with timeout(minutes=TUNING_DEFAULT_TIMEOUT_MINUTES):

        if not tuner:
            tuner = HyperparameterTuner(
                estimator=kmeans_estimator,
                objective_metric_name="test:msd",
                hyperparameter_ranges=hyperparameter_ranges,
                objective_type="Minimize",
                max_jobs=max_jobs,
                max_parallel_jobs=max_parallel_jobs,
                warm_start_config=warm_start_config,
                early_stopping_type=early_stopping_type,
            )

        records = kmeans_estimator.record_set(kmeans_train_set[0][:100])
        test_record_set = kmeans_estimator.record_set(kmeans_train_set[0][:100], channel="test")

        tuner.fit([records, test_record_set], job_name=job_name)
        print("Started hyperparameter tuning job with name:" + tuner.latest_tuning_job.name)
github aws / sagemaker-tensorflow-container / test / integration / sagemaker / test_mnist.py View on Github external
script = os.path.join(resource_path, 'mnist', 'mnist.py')

    estimator = TensorFlow(entry_point=script,
                           role='SageMakerRole',
                           train_instance_type=instance_type,
                           train_instance_count=1,
                           sagemaker_session=sagemaker_session,
                           image_name=ecr_image,
                           framework_version=framework_version,
                           script_mode=True)

    hyperparameter_ranges = {'epochs': IntegerParameter(1, 2)}
    objective_metric_name = 'accuracy'
    metric_definitions = [{'Name': objective_metric_name, 'Regex': 'accuracy = ([0-9\\.]+)'}]

    tuner = HyperparameterTuner(estimator,
                                objective_metric_name,
                                hyperparameter_ranges,
                                metric_definitions,
                                max_jobs=2,
                                max_parallel_jobs=2)

    with timeout(minutes=20):
        inputs = estimator.sagemaker_session.upload_data(
            path=os.path.join(resource_path, 'mnist', 'data'),
            key_prefix='scriptmode/mnist')

        tuning_job_name = unique_name_from_base('test-tf-sm-tuning', max_length=32)
        tuner.fit(inputs, job_name=tuning_job_name)
        tuner.wait()
github aws / sagemaker-python-sdk / tests / integ / test_tuner.py View on Github external
)
        test_input = estimator.sagemaker_session.upload_data(
            path=os.path.join(data_path, "test"), key_prefix="integ-test-data/chainer_mnist/test"
        )

        hyperparameter_ranges = {"alpha": ContinuousParameter(0.001, 0.005)}

        objective_metric_name = "Validation-accuracy"
        metric_definitions = [
            {
                "Name": "Validation-accuracy",
                "Regex": r"\[J1\s+\d\.\d+\s+\d\.\d+\s+\d\.\d+\s+(\d\.\d+)",
            }
        ]

        tuner = HyperparameterTuner(
            estimator,
            objective_metric_name,
            hyperparameter_ranges,
            metric_definitions,
            max_jobs=2,
            max_parallel_jobs=2,
        )

        tuning_job_name = unique_name_from_base("chainer", max_length=32)
        tuner.fit({"train": train_input, "test": test_input}, job_name=tuning_job_name)

        print("Started hyperparameter tuning job with name:" + tuning_job_name)

        time.sleep(15)
        tuner.wait()
github aws / sagemaker-python-sdk / tests / integ / test_marketplace.py View on Github external
role="SageMakerRole",
        train_instance_count=1,
        train_instance_type=cpu_instance_type,
        sagemaker_session=sagemaker_session,
        base_job_name="test-marketplace",
    )

    train_input = mktplace.sagemaker_session.upload_data(
        path=data_path, key_prefix="integ-test-data/marketplace/train"
    )

    mktplace.set_hyperparameters(max_leaf_nodes=10)

    hyperparameter_ranges = {"max_leaf_nodes": IntegerParameter(1, 100000)}

    tuner = HyperparameterTuner(
        estimator=mktplace,
        base_tuning_job_name="byo",
        objective_metric_name="validation:accuracy",
        hyperparameter_ranges=hyperparameter_ranges,
        max_jobs=2,
        max_parallel_jobs=2,
    )

    tuner.fit({"training": train_input}, include_cls_metadata=False)
    time.sleep(15)
    tuner.wait()
github aws / sagemaker-python-sdk / tests / unit / test_tuner.py View on Github external
sagemaker_session=sagemaker_session,
    )

    pca = PCA(
        ROLE,
        TRAIN_INSTANCE_COUNT,
        TRAIN_INSTANCE_TYPE,
        NUM_COMPONENTS,
        base_job_name="pca",
        sagemaker_session=sagemaker_session,
    )
    pca.algorithm_mode = "randomized"
    pca.subtract_mean = True
    pca.extra_components = 5

    tuner = HyperparameterTuner.create(
        base_tuning_job_name=BASE_JOB_NAME,
        estimator_dict={ESTIMATOR_NAME: mxnet, ESTIMATOR_NAME_TWO: pca},
        objective_metric_name_dict={
            ESTIMATOR_NAME: OBJECTIVE_METRIC_NAME,
            ESTIMATOR_NAME_TWO: OBJECTIVE_METRIC_NAME_TWO,
        },
        hyperparameter_ranges_dict={
            ESTIMATOR_NAME: HYPERPARAMETER_RANGES,
            ESTIMATOR_NAME_TWO: HYPERPARAMETER_RANGES_TWO,
        },
        metric_definitions_dict={ESTIMATOR_NAME: METRIC_DEFINITIONS},
        strategy=STRATEGY,
        objective_type=OBJECTIVE_TYPE,
        max_jobs=MAX_JOBS,
        max_parallel_jobs=MAX_PARALLEL_JOBS,
        tags=TAGS,
github aws / sagemaker-python-sdk / tests / unit / test_airflow.py View on Github external
sagemaker_session=sagemaker_session,
        base_job_name="{{ base_job_name }}",
        hyperparameters={"batch_size": 100},
    )

    hyperparameter_ranges = {
        "optimizer": tuner.CategoricalParameter(["sgd", "Adam"]),
        "learning_rate": tuner.ContinuousParameter(0.01, 0.2),
        "num_epoch": tuner.IntegerParameter(10, 50),
    }
    objective_metric_name = "Validation-accuracy"
    metric_definitions = [
        {"Name": "Validation-accuracy", "Regex": "Validation-accuracy=([0-9\\.]+)"}
    ]

    mxnet_tuner = tuner.HyperparameterTuner(
        estimator=mxnet_estimator,
        objective_metric_name=objective_metric_name,
        hyperparameter_ranges=hyperparameter_ranges,
        metric_definitions=metric_definitions,
        strategy="Bayesian",
        objective_type="Maximize",
        max_jobs="{{ max_job }}",
        max_parallel_jobs="{{ max_parallel_job }}",
        tags=[{"{{ key }}": "{{ value }}"}],
        base_tuning_job_name="{{ base_job_name }}",
    )

    data = "{{ training_data }}"

    config = airflow.tuning_config(mxnet_tuner, data)
    expected_config = {
github aws / sagemaker-python-sdk / tests / unit / test_tuner.py View on Github external
def test_attach_tuning_job_with_multi_estimators(sagemaker_session):
    job_details = copy.deepcopy(MULTI_ALGO_TUNING_JOB_DETAILS)
    tuner = HyperparameterTuner.attach(
        JOB_NAME,
        sagemaker_session=sagemaker_session,
        estimator_cls={ESTIMATOR_NAME_TWO: "sagemaker.estimator.Estimator"},
        job_details=job_details,
    )

    assert tuner.latest_tuning_job.name == JOB_NAME
    assert tuner.strategy == "Bayesian"
    assert tuner.objective_type == "Minimize"
    assert tuner.max_jobs == 4
    assert tuner.max_parallel_jobs == 2
    assert tuner.early_stopping_type == "Off"
    assert tuner.warm_start_config is None

    assert tuner.estimator is None
    assert tuner.objective_metric_name is None
github Kenza-AI / sagify / sagify / sagemaker / sagemaker.py View on Github external
train_instance_type=instance_type,
            train_volume_size=volume_size,
            train_max_run=max_run,
            input_mode='File',
            output_path=output_path,
            sagemaker_session=self.sagemaker_session
        )

        metric_definitions = [
            {
                'Name': objective_metric_name,
                'Regex': '{}: {}'.format(objective_metric_name, _METRIC_REGEX)
            }
        ]

        tuner = sagemaker.tuner.HyperparameterTuner(
            estimator=estimator,
            objective_metric_name=objective_metric_name,
            hyperparameter_ranges=hyperparams_ranges_dict,
            metric_definitions=metric_definitions,
            max_jobs=max_jobs,
            max_parallel_jobs=max_parallel_jobs,
            objective_type=objective_type,
            base_tuning_job_name=base_job_name
        )

        if tags:
            tuner.tags = tags

        tuner.fit(input_s3_data_location, job_name=job_name)

        if wait: