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 FactorizationMachines(sagemaker_session=sagemaker_session, **test_params)
def test_call_fit_wrong_value_mini_batch_size(sagemaker_session): fm = FactorizationMachines(base_job_name='fm', 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): fm.fit(data, 0)
def test_model_image(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet('s3://{}/{}'.format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') fm.fit(data, MINI_BATCH_SIZE) model = fm.create_model() assert model.image == registry(REGION, 'factorization-machines') + '/factorization-machines:1'
def test_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}) FactorizationMachines(sagemaker_session=sagemaker_session, **test_params)
def test_init_required_named(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert fm.role == COMMON_TRAIN_ARGS['role'] assert fm.train_instance_count == COMMON_TRAIN_ARGS['train_instance_count'] assert fm.train_instance_type == COMMON_TRAIN_ARGS['train_instance_type'] assert fm.num_factors == ALL_REQ_ARGS['num_factors'] assert fm.predictor_type == ALL_REQ_ARGS['predictor_type']
def test_init_required_positional(sagemaker_session): fm = FactorizationMachines('myrole', 1, 'ml.c4.xlarge', 3, 'regressor', sagemaker_session=sagemaker_session) assert fm.role == 'myrole' assert fm.train_instance_count == 1 assert fm.train_instance_type == 'ml.c4.xlarge' assert fm.num_factors == 3 assert fm.predictor_type == 'regressor'
def test_init_required_named(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert fm.role == COMMON_TRAIN_ARGS["role"] assert fm.instance_count == COMMON_TRAIN_ARGS["instance_count"] assert fm.instance_type == COMMON_TRAIN_ARGS["instance_type"] assert fm.num_factors == ALL_REQ_ARGS["num_factors"] assert fm.predictor_type == ALL_REQ_ARGS["predictor_type"]
def test_predictor_type(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet('s3://{}/{}'.format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') fm.fit(data, MINI_BATCH_SIZE) model = fm.create_model() predictor = model.deploy(1, TRAIN_INSTANCE_TYPE) assert isinstance(predictor, FactorizationMachinesPredictor)
def test_init_required_positional(sagemaker_session): fm = FactorizationMachines( "myrole", 1, "ml.c4.xlarge", 3, "regressor", sagemaker_session=sagemaker_session ) assert fm.role == "myrole" assert fm.instance_count == 1 assert fm.instance_type == "ml.c4.xlarge" assert fm.num_factors == 3 assert fm.predictor_type == "regressor"
def test_prepare_for_training_no_mini_batch_size(sagemaker_session): fm = FactorizationMachines(base_job_name='fm', sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet('s3://{}/{}'.format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') fm._prepare_for_training(data)
def test_call_fit(base_fit, sagemaker_session): fm = FactorizationMachines(base_job_name='fm', sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet('s3://{}/{}'.format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel='train') fm.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(sagemaker_session): fm = FactorizationMachines( sagemaker_session=sagemaker_session, epochs=2, clip_gradient=1e2, eps=0.001, rescale_grad=2.2, bias_lr=0.01, linear_lr=0.002, factors_lr=0.0003, bias_wd=0.0004, linear_wd=1.01, factors_wd=1.002, bias_init_method="uniform", bias_init_scale=0.1, bias_init_sigma=0.05, bias_init_value=2.002, linear_init_method="constant", linear_init_scale=0.02, linear_init_sigma=0.003, linear_init_value=1.0, factors_init_method="normal", factors_init_scale=1.101, factors_init_sigma=1.202, factors_init_value=1.303, **ALL_REQ_ARGS, ) assert fm.hyperparameters() == dict( num_factors=str(ALL_REQ_ARGS["num_factors"]), predictor_type=ALL_REQ_ARGS["predictor_type"], epochs="2", clip_gradient="100.0", eps="0.001", rescale_grad="2.2", bias_lr="0.01", linear_lr="0.002", factors_lr="0.0003", bias_wd="0.0004", linear_wd="1.01", factors_wd="1.002", bias_init_method="uniform", bias_init_scale="0.1", bias_init_sigma="0.05", bias_init_value="2.002", linear_init_method="constant", linear_init_scale="0.02", linear_init_sigma="0.003", linear_init_value="1.0", factors_init_method="normal", factors_init_scale="1.101", factors_init_sigma="1.202", factors_init_value="1.303", )
def test_prepare_for_training_wrong_type_mini_batch_size(sagemaker_session): fm = FactorizationMachines(base_job_name='fm', 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((TypeError, ValueError)): fm._prepare_for_training(data, 'some')
def test_model_image(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet( "s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel="train", ) fm.fit(data, MINI_BATCH_SIZE) model = fm.create_model() assert image_uris.retrieve("factorization-machines", REGION) == model.image_uri
def test_prepare_for_training_wrong_value_mini_batch_size(sagemaker_session): fm = FactorizationMachines(base_job_name="fm", 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): fm._prepare_for_training(data, 0)
def test_all_hyperparameters(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, epochs=2, clip_gradient=1e2, eps=0.001, rescale_grad=2.2, bias_lr=0.01, linear_lr=0.002, factors_lr=0.0003, bias_wd=0.0004, linear_wd=1.01, factors_wd=1.002, bias_init_method='uniform', bias_init_scale=0.1, bias_init_sigma=0.05, bias_init_value=2.002, linear_init_method='constant', linear_init_scale=0.02, linear_init_sigma=0.003, linear_init_value=1.0, factors_init_method='normal', factors_init_scale=1.101, factors_init_sigma=1.202, factors_init_value=1.303, **ALL_REQ_ARGS) assert fm.hyperparameters() == dict( num_factors=str(ALL_REQ_ARGS['num_factors']), predictor_type=ALL_REQ_ARGS['predictor_type'], epochs='2', clip_gradient='100.0', eps='0.001', rescale_grad='2.2', bias_lr='0.01', linear_lr='0.002', factors_lr='0.0003', bias_wd='0.0004', linear_wd='1.01', factors_wd='1.002', bias_init_method='uniform', bias_init_scale='0.1', bias_init_sigma='0.05', bias_init_value='2.002', linear_init_method='constant', linear_init_scale='0.02', linear_init_sigma='0.003', linear_init_value='1.0', factors_init_method='normal', factors_init_scale='1.101', factors_init_sigma='1.202', factors_init_value='1.303', )
def test_predictor_custom_serialization(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) data = RecordSet( "s3://{}/{}".format(BUCKET_NAME, PREFIX), num_records=1, feature_dim=FEATURE_DIM, channel="train", ) fm.fit(data, MINI_BATCH_SIZE) model = fm.create_model() custom_serializer = Mock() custom_deserializer = Mock() predictor = model.deploy( 1, INSTANCE_TYPE, serializer=custom_serializer, deserializer=custom_deserializer, ) assert isinstance(predictor, FactorizationMachinesPredictor) assert predictor.serializer is custom_serializer assert predictor.deserializer is custom_deserializer
def test_image(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert image_uris.retrieve("factorization-machines", REGION) == fm.training_image_uri()
def test_image(sagemaker_session): fm = FactorizationMachines(sagemaker_session=sagemaker_session, **ALL_REQ_ARGS) assert fm.train_image() == registry(REGION) + "/factorization-machines:1"