Skip to content

Commit 829ec26

Browse files
goswamigjasdel
authored andcommitted
example/service/sagemaker: Add create training job example (#330)
Adds Amazon SageMaker Create and List Training Job examples.
1 parent ed4e589 commit 829ec26

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// +build example
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
10+
"github.com/aws/aws-sdk-go-v2/aws/external"
11+
"github.com/aws/aws-sdk-go-v2/service/sagemaker"
12+
)
13+
14+
func exitErrorf(msg string, args ...interface{}) {
15+
fmt.Fprintf(os.Stderr, msg+"\n", args...)
16+
os.Exit(1)
17+
}
18+
19+
// Usage: go run -tags example createTrainingJobs.go
20+
func main() {
21+
22+
cfg, err := external.LoadDefaultAWSConfig()
23+
if err != nil {
24+
exitErrorf("failed to load config, %v", err)
25+
}
26+
27+
sagemakerSvc := sagemaker.New(cfg)
28+
29+
//Define intput variables
30+
role := "arn:aws:iam::xxxxxx:role/<name of role>"
31+
name := "k-means-in-sagemaker"
32+
MaxRuntimeInSeconds := int64(60 * 60)
33+
S3OutputPath := "s3://<bucket where your model artifact will be saved"
34+
InstanceCount := int64(2)
35+
VolumeSizeInGB := int64(75)
36+
TrainingInstanceType := sagemaker.TrainingInstanceType("ml.c4.8xlarge")
37+
TrainingImage := "174872318107.dkr.ecr.us-west-2.amazonaws.com/kmeans:1"
38+
TrainingInputMode := sagemaker.TrainingInputMode("File")
39+
40+
ChannelName := "train"
41+
S3DataDistributionType := sagemaker.S3DataDistribution("FullyReplicated")
42+
S3DataType := sagemaker.S3DataType("S3Prefix")
43+
S3Uri := "s3://<bucket where the input data is available>"
44+
45+
HyperParameters := map[string]string{
46+
"k": "10",
47+
"feature_dim": "784",
48+
"mini_batch_size": "500",
49+
}
50+
51+
params := &sagemaker.CreateTrainingJobInput{
52+
RoleArn: &role,
53+
TrainingJobName: &name,
54+
55+
StoppingCondition: &sagemaker.StoppingCondition{
56+
MaxRuntimeInSeconds: &MaxRuntimeInSeconds,
57+
},
58+
59+
OutputDataConfig: &sagemaker.OutputDataConfig{
60+
S3OutputPath: &S3OutputPath,
61+
},
62+
63+
ResourceConfig: &sagemaker.ResourceConfig{
64+
InstanceCount: &InstanceCount,
65+
VolumeSizeInGB: &VolumeSizeInGB,
66+
InstanceType: TrainingInstanceType,
67+
},
68+
69+
AlgorithmSpecification: &sagemaker.AlgorithmSpecification{
70+
TrainingImage: &TrainingImage,
71+
TrainingInputMode: TrainingInputMode,
72+
},
73+
74+
InputDataConfig: []sagemaker.Channel{
75+
{
76+
ChannelName: &ChannelName,
77+
DataSource: &sagemaker.DataSource{
78+
S3DataSource: &sagemaker.S3DataSource{
79+
S3DataDistributionType: S3DataDistributionType,
80+
S3DataType: S3DataType,
81+
S3Uri: &S3Uri,
82+
},
83+
},
84+
},
85+
},
86+
HyperParameters: HyperParameters,
87+
}
88+
89+
req := sagemakerSvc.CreateTrainingJobRequest(params)
90+
91+
resp, err := req.Send(context.TODO())
92+
if err != nil {
93+
exitErrorf("Error creating TrainingJob, %v", err)
94+
return
95+
}
96+
97+
fmt.Println(resp)
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Example using [Amazon SageMaker](https://aws.amazon.com/sagemaker/) with the
2+
AWS SDK for Go to create a training job.
3+
4+
## Usage
5+
6+
Under `Define intput variables` update your account number, input, output
7+
[Amazon S3](https://aws.amazon.com/s3/) buckets and then run the example.
8+
9+
```sh
10+
go run -tags example createTrainingJobs.go
11+
```
12+
13+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// +build example
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
"strconv"
10+
11+
"github.com/aws/aws-sdk-go-v2/aws/external"
12+
"github.com/aws/aws-sdk-go-v2/service/sagemaker"
13+
)
14+
15+
func exitErrorf(msg string, args ...interface{}) {
16+
fmt.Fprintf(os.Stderr, msg+"\n", args...)
17+
os.Exit(1)
18+
}
19+
20+
// This code serves an example on how to use it for sagemaker
21+
//
22+
// Usage: go run -tags example listTrainingJobs <int_value>
23+
func main() {
24+
if len(os.Args) < 2 {
25+
exitErrorf("you must specify a MaxItems")
26+
}
27+
28+
x, err := strconv.ParseInt(os.Args[1], 10, 64)
29+
if err != nil {
30+
exitErrorf("failed to parse argument %v", err)
31+
}
32+
33+
cfg, err := external.LoadDefaultAWSConfig()
34+
if err != nil {
35+
exitErrorf("failed to load config, %v", err)
36+
}
37+
38+
cfg.Region = "us-west-2"
39+
40+
sagemakerSvc := sagemaker.New(cfg)
41+
42+
req := sagemakerSvc.ListTrainingJobsRequest(&sagemaker.ListTrainingJobsInput{MaxResults: &x})
43+
resp, err := req.Send(context.TODO())
44+
if err != nil {
45+
exitErrorf("failed to list training jobs, %v", err)
46+
}
47+
48+
fmt.Println(resp)
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Example using [Amazon SageMaker](https://aws.amazon.com/sagemaker/) with the
2+
AWS SDK for Go to list training jobs that have been created, and their status.
3+
4+
## Usage
5+
Use the example to list the created training jobs and their status, passing in
6+
the number of jobs to show.
7+
8+
```sh
9+
go run listTrainingJobs.go <number of jobs to be displayed>
10+
11+
# E.g.
12+
go run listTrainingJobs.go 1
13+
```
14+
15+
## Output
16+
Example response of a training job and its status.
17+
18+
```
19+
{
20+
NextToken: "xcskfskdfksdffksdhfjhjghjshdfgjhfjgh"
21+
TrainingJobSummaries: [{
22+
CreationTime: 2019-07-03 18:17:34 +0000 UTC,
23+
LastModifiedTime: 2019-07-03 18:22:15 +0000 UTC,
24+
TrainingEndTime: 2019-07-03 18:22:15 +0000 UTC,
25+
TrainingJobArn: "arn:aws:sagemaker:us-west-2:<account_number>:training-job/hpojob-20190703181725-leaj-001-d345f443",
26+
TrainingJobName: "HPOJob-20190703181725-LEAJ-001-d345f443",
27+
TrainingJobStatus: Completed
28+
}]
29+
}
30+
```

0 commit comments

Comments
 (0)