PyTorch Non-linear Classifier

This is a demonstration of how to run custom PyTorch model using SageMaker. We are going to implement a non-linear binary classifier that can create a non-linear separation of moon data from sklearn.

Moon Data

sklearn provides utility functions to generate data that look like two moon shapes when they are displayed and they are often referred to as moon data.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split

%matplotlib inline

np.random.seed(0)
num_points = 600
noise_val = 0.25

X, labels = make_moons(num_points, noise=noise_val)
X_train, X_test, labels_train, labels_test = train_test_split(X, labels, test_size=0.25, random_state=1)

plt.figure(figsize=(8,5))
plt.scatter(X_train[:,0], X_train[:, 1], c=labels_train)
plt.title('Moon Data')
plt.show()
png

Data Preparation

Let's initialize the session and configure bucket and format for data upload.

SageMaker expects CSV files as input for both training inference. The CSV files should be a certain format, according to documentation

Amazon SageMaker requires that a CSV doesn't have a header record and that the target variable is in the first column

In order to achieve the desired format of our data, we should use DataFrame to perform the CSV parsing. We should define a function for this task because we don't want a DataFrame to take up unnecessary memory after we are done with it. We define it within the function scope, and when function returns, let garbage collection collects it.

Once the CSV is inside the data directory, we can upload it to S3 using SageMaker helper function.

PyTorch Model

We need to create a directory for our PyTorch scripts. The scripts will define a simple 3-layer neural network that uses Sigmoid as the final output value. Each layer may use ReLU as activiation. Then we pass this directory to SageMaker and let SageMaker runs the scripts on our behalf.

  1. Create a folder, scripts

  2. Create two Python scripts, one for training, one for inference

  3. Create a PyTorch estimator and load the scripts as entry point

Training Script

A typical training script has the following logic,

  • Load training data from a specified directory

  • Parse any training and model hyperparameters

  • Instantiate a model

  • Train the model

  • Save the model artifacts somewhere

Training script has access the following environment variables.

  • SM_NUM_GPUS: An integer representing the number of GPUs available to the host.

  • SM_MODEL_DIR: A string representing the path to the directory to write model artifacts to. These artifacts are uploaded to S3 for model hosting.

  • SM_OUTPUT_DATA_DIR: A string representing the filesystem path to write output artifacts to. Output artifacts may include checkpoints, graphs, and other files to save, not including model artifacts. These artifacts are compressed and uploaded to S3 to the same S3 prefix as the model artifacts.

  • SM_CHANNEL_XXXX: A string that represents the path to the directory that contains the input data for the specified channel. For example, if you specify two input channels in the PyTorch estimator’s fit call, named ‘train’ and ‘test’, the environment variables SM_CHANNEL_TRAIN and SM_CHANNEL_TEST are set.

Examples can be found in scripts.

PyTorch Estimator

Once the scripts are ready, we can launch an estimator to load the scripts and perform training.

PyTorch Model

A custom PyTorch estimator is different from other built-in estimator; it cannot be deployed. If we want to deploy our trained model, we must use a PyTorchModel class to hande the deployment. This model class has a similar API to that of estimator. It accepts an inference script that runs whenever the endpoint is called. The inference script must have the following functions.

  • def model_fn(model_dir)

  • def input_fn(request_body, request_content_type)

  • def predict_fn(input_object, model)

  • def output_fn(prediction, response_content_type)

Examples can be found in scripts. Now we can create the model and deploy it as an endpoint.

Model Evaluation

We need to verify the implementation by running the predictor against test data.

Cleanup

We have successfully deployed a SageMaker endpoint using custom PyTorch model. Now it's time to clean up.

Last updated