-
Notifications
You must be signed in to change notification settings - Fork 7k
Expand file tree
/
Copy pathquery_training_status.py
More file actions
43 lines (34 loc) · 1.41 KB
/
query_training_status.py
File metadata and controls
43 lines (34 loc) · 1.41 KB
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
import json
import logging
import boto3
logger = logging.getLogger()
logger.setLevel(logging.INFO)
sm_client = boto3.client("sagemaker")
# Retrieve transform job name from event and return transform job status.
def lambda_handler(event, context):
if "TrainingJobName" in event:
job_name = event["TrainingJobName"]
else:
raise KeyError(
"TrainingJobName key not found in function input!"
+ " The input received was: {}.".format(json.dumps(event))
)
# Query boto3 API to check training status.
try:
response = sm_client.describe_training_job(TrainingJobName=job_name)
logger.info(
"Training job:{} has status:{}.".format(job_name, response["TrainingJobStatus"])
)
except Exception as e:
response = (
"Failed to read training status!"
+ " The training job may not exist or the job name may be incorrect."
+ " Check SageMaker to confirm the job name."
)
print(e)
print("{} Attempted to read job name: {}.".format(response, job_name))
# We can't marshall datetime objects in JSON response. So convert
# all datetime objects returned to unix time.
for index, metric in enumerate(response["FinalMetricDataList"]):
metric["Timestamp"] = metric["Timestamp"].timestamp()
return {"statusCode": 200, "trainingMetrics": response["FinalMetricDataList"]}