Skip to content

Commit 5ece1d2

Browse files
committed
Added TF2 California housing local training and serving
1 parent 9845dd7 commit 5ece1d2

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import argparse
2+
import numpy as np
3+
import os
4+
import tensorflow as tf
5+
6+
def parse_args():
7+
8+
parser = argparse.ArgumentParser()
9+
10+
# hyperparameters sent by the client are passed as command-line arguments to the script
11+
parser.add_argument('--epochs', type=int, default=1)
12+
parser.add_argument('--batch_size', type=int, default=64)
13+
parser.add_argument('--learning_rate', type=float, default=0.1)
14+
15+
# data directories
16+
parser.add_argument('--train', type=str, default=os.environ.get('SM_CHANNEL_TRAIN'))
17+
parser.add_argument('--test', type=str, default=os.environ.get('SM_CHANNEL_TEST'))
18+
19+
# model directory
20+
parser.add_argument('--sm-model-dir', type=str, default=os.environ.get('SM_MODEL_DIR'))
21+
22+
return parser.parse_known_args()
23+
24+
25+
def get_train_data(train_dir):
26+
27+
x_train = np.load(os.path.join(train_dir, 'x_train.npy'))
28+
y_train = np.load(os.path.join(train_dir, 'y_train.npy'))
29+
print('x train', x_train.shape,'y train', y_train.shape)
30+
31+
return x_train, y_train
32+
33+
34+
def get_test_data(test_dir):
35+
36+
x_test = np.load(os.path.join(test_dir, 'x_test.npy'))
37+
y_test = np.load(os.path.join(test_dir, 'y_test.npy'))
38+
print('x test', x_test.shape,'y test', y_test.shape)
39+
40+
return x_test, y_test
41+
42+
43+
def get_model():
44+
45+
inputs = tf.keras.Input(shape=(8,))
46+
hidden_1 = tf.keras.layers.Dense(8, activation='tanh')(inputs)
47+
hidden_2 = tf.keras.layers.Dense(4, activation='sigmoid')(hidden_1)
48+
outputs = tf.keras.layers.Dense(1)(hidden_2)
49+
return tf.keras.Model(inputs=inputs, outputs=outputs)
50+
51+
52+
if __name__ == "__main__":
53+
54+
args, _ = parse_args()
55+
56+
print('Training data location: {}'.format(args.train))
57+
print('Test data location: {}'.format(args.test))
58+
x_train, y_train = get_train_data(args.train)
59+
x_test, y_test = get_test_data(args.test)
60+
61+
batch_size = args.batch_size
62+
epochs = args.epochs
63+
learning_rate = args.learning_rate
64+
print('batch_size = {}, epochs = {}, learning rate = {}'.format(batch_size, epochs, learning_rate))
65+
66+
67+
model = get_model()
68+
optimizer = tf.keras.optimizers.SGD(learning_rate)
69+
model.compile(optimizer=optimizer, loss='mse')
70+
model.fit(x_train,
71+
y_train,
72+
batch_size=batch_size,
73+
epochs=epochs,
74+
validation_data=(x_test, y_test))
75+
76+
# evaluate on test set
77+
scores = model.evaluate(x_test, y_test, batch_size, verbose=2)
78+
print("\nTest MSE :", scores)
79+
80+
# save model
81+
model.save(args.sm_model_dir + '/1')
82+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy
2+
pandas
3+
sagemaker>=2.0.0<3.0.0
4+
sagemaker[local]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# This is a sample Python program that trains a simple TensorFlow CIFAR-10 model.
2+
# This implementation will work on your *local computer* or in the *AWS Cloud*.
3+
# To run training and inference *locally* set: `config = get_config(LOCAL_MODE)`
4+
# To run training and inference on the *cloud* set: `config = get_config(CLOUD_MODE)` and set a valid IAM role value in get_config()
5+
#
6+
# Prerequisites:
7+
# 1. Install required Python packages:
8+
# `pip install -r requirements.txt`
9+
# 2. Docker Desktop installed and running on your computer:
10+
# `docker ps`
11+
# 3. You should have AWS credentials configured on your local machine
12+
# in order to be able to pull the docker image from ECR.
13+
###############################################################################################
14+
15+
import os
16+
17+
import numpy as np
18+
import pandas as pd
19+
from sklearn.datasets import *
20+
import sklearn.model_selection
21+
from sklearn.preprocessing import StandardScaler
22+
from sagemaker.tensorflow import TensorFlow
23+
24+
25+
DUMMY_IAM_ROLE = 'arn:aws:iam::111111111111:role/service-role/AmazonSageMaker-ExecutionRole-20200101T000001'
26+
27+
28+
def download_training_and_eval_data():
29+
if os.path.isfile('./data/train/x_train.npy') and \
30+
os.path.isfile('./data/test/x_test.npy') and \
31+
os.path.isfile('./data/train/y_train.npy') and \
32+
os.path.isfile('./data/test/y_test.npy'):
33+
print('Training and evaluation datasets exist. Skipping Download')
34+
else:
35+
print('Downloading training and evaluation dataset')
36+
data_dir = os.path.join(os.getcwd(), 'data')
37+
os.makedirs(data_dir, exist_ok=True)
38+
39+
train_dir = os.path.join(os.getcwd(), 'data/train')
40+
os.makedirs(train_dir, exist_ok=True)
41+
42+
test_dir = os.path.join(os.getcwd(), 'data/test')
43+
os.makedirs(test_dir, exist_ok=True)
44+
45+
data_set = fetch_california_housing()
46+
47+
X = pd.DataFrame(data_set.data, columns=data_set.feature_names)
48+
Y = pd.DataFrame(data_set.target)
49+
50+
# We partition the dataset into 2/3 training and 1/3 test set.
51+
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, Y, test_size=0.33)
52+
53+
scaler = StandardScaler()
54+
scaler.fit(x_train)
55+
x_train = scaler.transform(x_train)
56+
x_test = scaler.transform(x_test)
57+
58+
np.save(os.path.join(train_dir, 'x_train.npy'), x_train)
59+
np.save(os.path.join(test_dir, 'x_test.npy'), x_test)
60+
np.save(os.path.join(train_dir, 'y_train.npy'), y_train)
61+
np.save(os.path.join(test_dir, 'y_test.npy'), y_test)
62+
63+
print('Downloading completed')
64+
65+
66+
def do_inference_on_local_endpoint(predictor):
67+
print(f'\nStarting Inference on endpoint (local).')
68+
69+
x_test = np.load('./data/test/x_test.npy')
70+
y_test = np.load('./data/test/y_test.npy')
71+
72+
results = predictor.predict(x_test[:10])['predictions']
73+
flat_list = [float('%.1f' % (item)) for sublist in results for item in sublist]
74+
print('predictions: \t{}'.format(np.array(flat_list)))
75+
print('target values: \t{}'.format(y_test[:10].round(decimals=1)))
76+
77+
78+
def main():
79+
download_training_and_eval_data()
80+
81+
print('Starting model training.')
82+
print(
83+
'Note: if launching for the first time in local mode, container image download might take a few minutes to complete.')
84+
california_housing_estimator = TensorFlow(entry_point='california_housing_tf2.py',
85+
source_dir='code',
86+
role=DUMMY_IAM_ROLE,
87+
instance_count=1,
88+
instance_type='local',
89+
framework_version='2.4.1',
90+
py_version='py37')
91+
92+
inputs = {'train': 'file://./data/train', 'test': 'file://./data/test'}
93+
california_housing_estimator.fit(inputs)
94+
print('Completed model training')
95+
96+
print('Deploying endpoint in local mode')
97+
predictor = california_housing_estimator.deploy(initial_instance_count=1, instance_type='local')
98+
99+
do_inference_on_local_endpoint(predictor)
100+
101+
print('About to delete the endpoint to stop paying (if in cloud mode).')
102+
predictor.delete_endpoint(predictor.endpoint_name)
103+
104+
105+
if __name__ == "__main__":
106+
main()

0 commit comments

Comments
 (0)