/
xgboost_script_mode_local_training_and_serving.py
91 lines (72 loc) · 3.23 KB
/
xgboost_script_mode_local_training_and_serving.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This is a sample Python program that trains a simple XGBoost model on Abalone dataset.
# This implementation will work on your *local computer* or in the *AWS Cloud*.
#
# Prerequisites:
# 1. Install required Python packages:
# `pip install -r requirements.txt`
# 2. Docker Desktop installed and running on your computer:
# `docker ps`
# 3. You should have AWS credentials configured on your local machine
# in order to be able to pull the docker image from ECR.
###############################################################################################
from sagemaker import TrainingInput
from sagemaker.xgboost import XGBoost, XGBoostModel
from sagemaker.local import LocalSession
DUMMY_IAM_ROLE = 'arn:aws:iam::111111111111:role/service-role/AmazonSageMaker-ExecutionRole-20200101T000001'
LOCAL_SESSION = LocalSession()
LOCAL_SESSION.config = {'local': {'local_code': True}} # Ensure full code locality, see: https://sagemaker.readthedocs.io/en/stable/overview.html#local-mode
FRAMEWORK_VERSION = "1.7-1"
def do_inference_on_local_endpoint(predictor, libsvm_str):
label, *features = libsvm_str.strip().split()
predictions = predictor.predict(" ".join(["-99"] + features)) # use dummy label -99
print("Prediction: {}".format(predictions))
def main():
print('Starting model training.')
print(
'Note: if launching for the first time in local mode, container image download might take a few minutes to complete.')
hyperparameters = {
"max_depth": "5",
"eta": "0.2",
"gamma": "4",
"min_child_weight": "6",
"subsample": "0.7",
"objective": "reg:squarederror",
"num_round": "50",
"verbosity": "2",
}
xgb_script_mode_estimator = XGBoost(
entry_point="./code/abalone.py",
hyperparameters=hyperparameters,
role=DUMMY_IAM_ROLE,
instance_count=1,
instance_type='local',
framework_version=FRAMEWORK_VERSION,
sagemaker_session=LOCAL_SESSION,
output_path='file://model/' # Save trained model and any additional artifacts locally
)
train_input = TrainingInput("file://data/train/abalone", content_type="text/libsvm")
xgb_script_mode_estimator.fit({"train": train_input, "validation": train_input})
print('Completed model training')
model_data = xgb_script_mode_estimator.model_data
print(model_data)
xgb_inference_model = XGBoostModel(
model_data=model_data,
role=DUMMY_IAM_ROLE,
entry_point="inference.py",
source_dir="./code",
framework_version=FRAMEWORK_VERSION,
sagemaker_session=LOCAL_SESSION
)
print('Deploying endpoint in local mode')
predictor = xgb_inference_model.deploy(
initial_instance_count=1,
instance_type="local",
)
a_young_abalone = "6 1:3 2:0.37 3:0.29 4:0.095 5:0.249 6:0.1045 7:0.058 8:0.067"
do_inference_on_local_endpoint(predictor, a_young_abalone)
an_old_abalone = "15 1:1 2:0.655 3:0.53 4:0.175 5:1.2635 6:0.486 7:0.2635 8:0.415"
do_inference_on_local_endpoint(predictor, an_old_abalone)
print('About to delete the endpoint to stop paying (if in cloud mode).')
predictor.delete_endpoint(predictor.endpoint_name)
if __name__ == "__main__":
main()