def test_error_optional_hyper_parameters_value(sagemaker_session, optional_hyper_parameters, value): with pytest.raises(ValueError): test_params = ALL_REQ_ARGS.copy() test_params.update({optional_hyper_parameters: value}) KNN(sagemaker_session=sagemaker_session, **test_params)
def test_init_required_named(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert knn.role == COMMON_TRAIN_ARGS["role"] assert knn.train_instance_count == TRAIN_INSTANCE_COUNT assert knn.train_instance_type == COMMON_TRAIN_ARGS["train_instance_type"] assert knn.k == ALL_REQ_ARGS["k"]
def test_model_image(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet("s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') knn.fit(data, MINI_BATCH_SIZE) model = knn.create_model() assert model.image == registry(REGION, "knn") + '/knn:1'
def test_call_fit_none_mini_batch_size(sagemaker_session): knn = KNN(base_job_name="knn", sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet("s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') knn.fit(data)
def test_predictor_type(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet("s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') knn.fit(data, MINI_BATCH_SIZE) model = knn.create_model() predictor = model.deploy(1, TRAIN_INSTANCE_TYPE) assert isinstance(predictor, KNNPredictor)
def test_prepare_for_training_wrong_value_lower_mini_batch_size(sagemaker_session): knn = KNN(base_job_name="knn", sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet( "s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel="train", ) with pytest.raises(ValueError): knn._prepare_for_training(data, 0)
def test_init_required_positional(sagemaker_session): knn = KNN(ROLE, TRAIN_INSTANCE_COUNT, TRAIN_INSTANCE_TYPE, K, SAMPLE_SIZE, PREDICTOR_TYPE_REGRESSOR, sagemaker_session=sagemaker_session) assert knn.role == ROLE assert knn.train_instance_count == TRAIN_INSTANCE_COUNT assert knn.train_instance_type == TRAIN_INSTANCE_TYPE assert knn.k == K
def test_model_image(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet( "s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel="train", ) knn.fit(data, MINI_BATCH_SIZE) model = knn.create_model() assert image_uris.retrieve("knn", REGION) == model.image_uri
def test_call_fit(base_fit, sagemaker_session): knn = KNN(base_job_name="knn", sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet("s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') knn.fit(data, MINI_BATCH_SIZE) base_fit.assert_called_once() assert len(base_fit.call_args[0]) == 2 assert base_fit.call_args[0][0] == data assert base_fit.call_args[0][1] == MINI_BATCH_SIZE
def test_all_hyperparameters_regressor(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, dimension_reduction_type='sign', dimension_reduction_target='2', index_type='faiss.Flat', index_metric='COSINE', faiss_index_ivf_nlists='auto', faiss_index_pq_m=1, **ALL_REQ_ARGS) assert knn.hyperparameters() == dict( k=str(ALL_REQ_ARGS['k']), sample_size=str(ALL_REQ_ARGS['sample_size']), predictor_type=str(ALL_REQ_ARGS['predictor_type']), dimension_reduction_type='sign', dimension_reduction_target='2', index_type='faiss.Flat', index_metric='COSINE', faiss_index_ivf_nlists='auto', faiss_index_pq_m='1')
def test_all_hyperparameters_regressor(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, dimension_reduction_type="sign", dimension_reduction_target="2", index_type="faiss.Flat", index_metric="COSINE", faiss_index_ivf_nlists="auto", faiss_index_pq_m=1, **ALL_REQ_ARGS) assert knn.hyperparameters() == dict( k=str(ALL_REQ_ARGS["k"]), sample_size=str(ALL_REQ_ARGS["sample_size"]), predictor_type=str(ALL_REQ_ARGS["predictor_type"]), dimension_reduction_type="sign", dimension_reduction_target="2", index_type="faiss.Flat", index_metric="COSINE", faiss_index_ivf_nlists="auto", faiss_index_pq_m="1", )
def test_all_hyperparameters_classifier(sagemaker_session): test_params = ALL_REQ_ARGS.copy() test_params['predictor_type'] = PREDICTOR_TYPE_CLASSIFIER knn = KNN(sagemaker_session=sagemaker_session, dimension_reduction_type='fjlt', dimension_reduction_target='2', index_type='faiss.IVFFlat', index_metric='L2', faiss_index_ivf_nlists='20', **test_params) assert knn.hyperparameters() == dict( k=str(ALL_REQ_ARGS['k']), sample_size=str(ALL_REQ_ARGS['sample_size']), predictor_type=str(PREDICTOR_TYPE_CLASSIFIER), dimension_reduction_type='fjlt', dimension_reduction_target='2', index_type='faiss.IVFFlat', index_metric='L2', faiss_index_ivf_nlists='20')
def test_all_hyperparameters_classifier(sagemaker_session): test_params = ALL_REQ_ARGS.copy() test_params["predictor_type"] = PREDICTOR_TYPE_CLASSIFIER knn = KNN(sagemaker_session=sagemaker_session, dimension_reduction_type="fjlt", dimension_reduction_target="2", index_type="faiss.IVFFlat", index_metric="L2", faiss_index_ivf_nlists="20", **test_params) assert knn.hyperparameters() == dict( k=str(ALL_REQ_ARGS["k"]), sample_size=str(ALL_REQ_ARGS["sample_size"]), predictor_type=str(PREDICTOR_TYPE_CLASSIFIER), dimension_reduction_type="fjlt", dimension_reduction_target="2", index_type="faiss.IVFFlat", index_metric="L2", faiss_index_ivf_nlists="20", )
def test_predictor_custom_serialization(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet( "s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel="train", ) knn.fit(data, MINI_BATCH_SIZE) model = knn.create_model() custom_serializer = Mock() custom_deserializer = Mock() predictor = model.deploy( 1, INSTANCE_TYPE, serializer=custom_serializer, deserializer=custom_deserializer, ) assert isinstance(predictor, KNNPredictor) assert predictor.serializer is custom_serializer assert predictor.deserializer is custom_deserializer
def test_error_conditional_hyper_parameters_value( sagemaker_session, conditional_hyper_parameters): with pytest.raises(ValueError): test_params = ALL_REQ_ARGS.copy() test_params.update(conditional_hyper_parameters) KNN(sagemaker_session=sagemaker_session, **test_params)
def test_required_hyper_parameters_value(sagemaker_session, required_hyper_parameters, value): with pytest.raises(ValueError): test_params = ALL_REQ_ARGS.copy() test_params[required_hyper_parameters] = value KNN(sagemaker_session=sagemaker_session, **test_params)
def test_image(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert knn.train_image() == registry(REGION, "knn") + "/knn:1"
def test_image(sagemaker_session): knn = KNN(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert image_uris.retrieve("knn", REGION) == knn.training_image_uri()