This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import pandas as pd | |
import os | |
from sklearn.externals import joblib | |
from sklearn.neighbors import NearestNeighbors | |
import numpy as np | |
import subprocess | |
import sys | |
def install(package): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict_fn(input_data, model): | |
input_data_reshaped = input_data.reshape(1, -1) | |
distance, indice = model.kneighbors(input_data_reshaped, 11) | |
distance_list = distance[0].tolist()[1:] | |
indice_list = indice[0].tolist()[1:] | |
nearest_neighbors = [distance_list, indice_list] | |
print('predict_fn output is', nearest_neighbors) | |
return np.array(nearest_neighbors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sagemaker_containers.beta.framework import worker, encoders | |
from six import BytesIO | |
def _npy_dumps(data): | |
# Serializes a numpy array into a stream of npy-formatted bytes. | |
buffer = BytesIO() | |
np.save(buffer, data) | |
return buffer.getvalue() | |
def output_fn(prediction_output, accept): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model_fn(model_dir): | |
clf = joblib.load(os.path.join(model_dir, "model.joblib")) | |
return clf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sagemaker.sklearn.estimator import SKLearn | |
script_path = 'sklearn_nearest_neighbors.py' | |
sess = sagemaker.Session() | |
# run the Scikit-Learn script | |
sklearn = SKLearn( | |
entry_point=script_path, | |
train_instance_type="ml.m5.large", | |
role=role, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
recommendations = predictor.predict(sample_vector) | |
print(recommendations) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_data = 's3://{}/wine-corpus.txt'.format(bucket) | |
s3_output_location = 's3://{}/output'.format(bucket) | |
region_name = boto3.Session().region_name | |
container = sagemaker.amazon.amazon_estimator.get_image_uri(region_name, "blazingtext", "latest") | |
print('Using SageMaker BlazingText container: {} ({})'.format(container, region_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sess = sagemaker.Session() | |
# define the specifications of the sagemaker training instance | |
bt_model = sagemaker.estimator.Estimator(container, | |
role, | |
train_instance_count=2, | |
train_instance_type='ml.c4.2xlarge', | |
train_volume_size = 5, | |
train_max_run = 360000, | |
input_mode= 'File', |
OlderNewer