TheodorosNikoleris
TheodorosNikoleris

Reputation: 65

Sagemaker -managed containers - script mode

Newbie at Sagemaker. I am trying my hand at the SKLearn managed container and bringing in my own code in script mode. I train my model as follows

sklearn=SKLearn(
    entry_point='skl_iris.py',
    framework_version='1.2-1',
    instance_type='ml.m5.large',
    #instance_type='local',
    role=myrole,
    sagemaker_session=mysess,
    hyperparameters={'learning_rate': 0.005}
    )

sklearn.fit(inputs={'train': train_input})

I deploy the trained model

skl_predictor = sklearn.deploy(
                        initial_instance_count=1, 
                        instance_type='ml.m5.large'
                        )

and get the predictions by invoking the automatically created endpoint

sgm_runtime = boto3.client('runtime.sagemaker')
inputX=dftest.iloc[0:2,1:]
body=inputX.to_csv(header=False, index=False)
body=json.dumps(body)

response=sgm_runtime.invoke_endpoint(
    EndpointName=predictor.endpoint_name, 
    ContentType="text/csv",
    Body=body
    )

response=response['Body'].read().decode('utf-8')
print(response)

I have one question and 2 problems:

Question: Is it possible in script mode and with the SKLearn managed container, to create a model,endpoint config and endpoint instead of having that done automatically with the deploy command?

Problem-1: The invocation is fine when I ask the endpoint to predict for two or more rows of input data. However, when I try to predict only one row of input I get a CloudWatch error, saying that the input shape is wrong. It is driving me crazy. Mind you, and probably this is important, in my script I only have a model_fn function. I have not yet come around incorporating input_fn,predict_fn and output_fn functions. Would that be the culprit?

Problem-2: When I try to train locally, I get the following error message

'docker-compose' is not installed. Local Mode features will not work without docker-compose. For more information on how to install 'docker-compose', please, see https://docs.docker.com/compose/install/

I never encountered that in any of the tutorials that I have been through. Is that normal?

I thank you in advance.

PS. In case you need the entire sagemaker ipynb as well as the py script just let me know. I will be grateful for the time you put in.

Upvotes: 1

Views: 191

Answers (1)

durga_sury
durga_sury

Reputation: 1152

For your first question, yes, you can manually create the model, endpoint config, endpoint in that order instead of model.deploy(). The SageMaker SDK is a higher level client that makes it easier to deploy a trained model. However, you can use the Boto3 client to manually create the endpoint. See a sample notebook on creating model, config and endpoint here - https://github.com/aws/amazon-sagemaker-examples/blob/main/boto3/built-in-frameworks/scikit-learn/boto3_scikit_retrain_model_and_deploy_to_existing_endpoint/boto3_scikit_retrain_model_and_deploy_to_existing_endpoint.ipynb

Problem 1: I don't think the input_fn/output_fn should cause any such issue. Wonder if you're sending an index in your input to the endpoint and that's causing a mismatch when you send multiple rows vs a single row?

Problem 2: Where are you running your notebook? Local mode uses Docker to spin up a container to mimic the training job, so if you don't have Docker installed, you'll see this error and won't be able to use local mode. SageMaker notebook instances come with Docker pre-installed, so it's a good alternative to running your notebooks if you need to run training in local mode.

Upvotes: 1

Related Questions