Deploy to SageMaker

Hi, I successfully trained and tested a model from week2. I uploaded the .pkl and .py files to S3 and Sagemaker using the " Deploying on Amazon SageMaker" instructions. When I run predictor = model.deploy, the SageMaker models and endpoint configurations are built successfully, however the endpoint build is failing on return dict line.

sagemaker_containers._errors.ImportModuleError: invalid syntax (horse.py, line 36)
Traceback (most recent call last):
File “/usr/local/lib/python3.6/dist-packages/sagemaker_containers/_modules.py”, line 246, in import_module
module = importlib.import_module(name)
File “/usr/lib/python3.6/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 994, in _gcd_import
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 665, in _load_unlocked
File “”, line 674, in exec_module
File “”, line 781, in get_code
File “”, line 741, in source_to_code
File “”, line 219, in _call_with_frames_removed
File “/usr/local/lib/python3.6/dist-packages/horse.py”, line 36
return dict(class = str(predict_class),

Line 36 is in predict_fn function.
def predict_fn(input_object, model):
logger.info(“Calling model”)
start_time = time.time()
predict_class,predict_idx,predict_values = model.predict(input_object)
print("— Inference time: %s seconds —" % (time.time() - start_time))
print(f’Predicted class is {str(predict_class)}’)
print(f’Predict confidence score is {predict_values[predict_idx.item()].item()}’)
return dict(class = str(predict_class),
** confidence = predict_values[predict_idx.item()].item())**

Has this been encountered before ?

1 Like

Hi David,
I don’t know if you’ve fixed your problem yet, but for posterity: this is happening because ‘class’ is a keyword in python. You cannot name things in python - in this case a key in a dictionary - ‘class’. (Or ‘for’, ‘if’, etc.)
Instead of return dict(class = str(predict_class),, the bolded line in your code should
be e.g. return dict(predicted_class= str(predict_class),
where the ‘class’ key name is replaced by something like ‘predicted_class’ or whatever name you want to use.

This is a problem with the sagemaker tutorial.

1 Like

Thanks Ben,

1 Like