<jupyter_start><jupyter_text>Text Classification Using Keras & TensorFlow on Amazon SageMaker  Download Data Download and unzip the dataset<jupyter_code>! wget -q https://archive.ics.uci.edu/ml/machine-learning-databases/00359/NewsAggregatorDataset.zip && unzip -o NewsAggregatorDataset.zip -d data<jupyter_output>Archive:  NewsAggregatorDataset.zip
  inflating: data/2pageSessions.csv  
   creating: data/__MACOSX/
  inflating: data/__MACOSX/._2pageSessions.csv  
  inflating: data/newsCorpora.csv    
  inflating: data/__MACOSX/._newsCorpora.csv  
  inflating: data/readme.txt         
  inflating: data/__MACOSX/._readme.txt<jupyter_text>Now lets also download and unzip the pre-trained glove embedding files<jupyter_code>! wget -q --no-check-certificate https://nlp.stanford.edu/data/glove.6B.zip && unzip -o glove.6B.zip -d data
!rm data/2pageSessions.csv data/glove.6B.200d.txt data/glove.6B.50d.txt data/glove.6B.300d.txt glove.6B.zip data/readme.txt NewsAggregatorDataset.zip && rm -rf data/__MACOSX/
!ls data<jupyter_output>glove.6B.100d.txt  newsCorpora.csv<jupyter_text>Data Exploration<jupyter_code>import pandas as pd
import tensorflow as tf
import re
import numpy as np
import os

from tensorflow.python.keras.preprocessing.text import Tokenizer
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.utils import to_categorical
column_names = ["TITLE", "URL", "PUBLISHER", "CATEGORY", "STORY", "HOSTNAME", "TIMESTAMP"]
news_dataset = pd.read_csv('data/newsCorpora.csv', names=column_names, header=None, delimiter='\t')
news_dataset.head()<jupyter_output><empty_output><jupyter_text>Here we first import the necessary libraries and tools such as TensorFlow, pandas and numpy. An open-source high performance data analysis library, pandas is an essential tool used in almost every Python-based data science experiment. NumPy is another Python library that provides data structures to hold multi-dimensional array data and provides many utility functions to transform that data. TensorFlow is a widely used deep learning framework that also includes the higher-level deep learning Python library called Keras. We will be using Keras to build and iterate our text classification model.Next we define the list of columns contained in this dataset (the format is usually described as part of the dataset as it is here). Finally, we use the ‘read_csv()’ method of the pandas library to read the dataset into memory and look at the first few lines using the ‘head()’ method.Remember, our goal is to accurately predict the category of any news article. So, ‘Category’ is our label or target column. For this example, we will only use the information contained in the ‘Title’ to predict the category. When should I build my own algorithm container?You may not need to create a container to bring your own code to Amazon SageMaker. When you are using a framework such as Apache MXNet or TensorFlow that has direct support in SageMaker, you can simply supply the Python code that implements your algorithm using the SDK entry points for that framework. This set of supported frameworks is regularly added to, so you should check the current list to determine whether your algorithm is written in one of these common machine learning environments.Even if there is direct SDK support for your environment or framework, you may find it more effective to build your own container. If the code that implements your algorithm is quite complex or you need special additions to the framework, building your own container may be the right choice.Some of the reasons to build an already supported framework container are:A specific version isn't supported.Configure and install your dependencies and environment.Use a different training/hosting solution than provided.This walkthrough shows that it is quite straightforward to build your own container. So you can still use SageMaker even if your use case is not covered by the deep learning containers that we've built for you. The DockerfileThe Dockerfile describes the image that we want to build. You can think of it as describing the complete operating system installation of the system that you want to run. A Docker container running is quite a bit lighter than a full operating system, however, because it takes advantage of Linux on the host machine for the basic operations.For the Python science stack, we start from an official TensorFlow docker image and run the normal tools to install TensorFlow Serving. Then we add the code that implements our specific algorithm to the container and set up the right environment for it to run under.Let's look at the Dockerfile for this example.<jupyter_code>!cat container/Dockerfile
%%sh

# The name of our algorithm
algorithm_name=sagemaker-keras-text-classification

cd container

chmod +x sagemaker_keras_text_classification/train
chmod +x sagemaker_keras_text_classification/serve

account=$(aws sts get-caller-identity --query Account --output text)

# Get the region defined in the current configuration (default to us-west-2 if none defined)
region=$(aws configure get region)
region=${region:-us-west-2}

fullname="${account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:latest"

# If the repository doesn't exist in ECR, create it.

aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1

if [ $? -ne 0 ]
then
    aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
fi

# Get the login command from ECR and execute it directly
$(aws ecr get-login --region ${region} --no-include-email)

# Build the docker image locally with the image name and then push it to ECR
# with the full name.

docker build  -t ${algorithm_name} .
docker tag ${algorithm_name} ${fullname}

docker push ${fullname}<jupyter_output>Login Succeeded
Sending build context to Docker daemon  25.09kB

Step 1/8 : FROM tensorflow/tensorflow:1.8.0-py3
 ---> a83a3dd79ff9
Step 2/8 : RUN apt-get update && apt-get install -y --no-install-recommends nginx curl
 ---> Using cache
 ---> b2c6ee34bd63
Step 3/8 : RUN echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | tee /etc/apt/sources.list.d/tensorflow-serving.list
 ---> Using cache
 ---> a6abb67693c2
Step 4/8 : RUN curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | apt-key add -
 ---> Using cache
 ---> 191bced6e844
Step 5/8 : RUN apt-get update && apt-get install tensorflow-model-server
 ---> Using cache
 ---> 6a5c372c80ac
Step 6/8 : ENV PATH="/opt/ml/code:${PATH}"
 ---> Using cache
 ---> fafb737091f1
Step 7/8 : COPY /sagemaker_keras_text_classification /opt/ml/code
 ---> Using cache
 ---> 322614cc2708
Step 8/8 : WORKDIR /opt/ml/code
 ---> Us[...]<jupyter_text>Once you have your container packaged, you can use it to train and serve models. Let's do that with the algorithm we made above. Set up the environmentHere we specify a bucket to use and the role that will be used for working with SageMaker.<jupyter_code># S3 prefix
prefix = 'sagemaker-keras-text-classification'

# Define IAM role
import boto3
import re

import os
import numpy as np
import pandas as pd
from sagemaker import get_execution_role

role = get_execution_role()<jupyter_output><empty_output><jupyter_text>Create the sessionThe session remembers our connection parameters to SageMaker. We'll use it to perform all of our SageMaker operations.<jupyter_code>import sagemaker as sage
from time import gmtime, strftime

sess = sage.Session()<jupyter_output><empty_output><jupyter_text>Upload the data for trainingWhen training large models with huge amounts of data, you'll typically use big data tools, like Amazon Athena, AWS Glue, or Amazon EMR, to create your data in S3.  We can use use the tools provided by the SageMaker Python SDK to upload the data to a default bucket.<jupyter_code>WORK_DIRECTORY = 'data'

data_location = sess.upload_data(WORK_DIRECTORY, key_prefix=prefix)<jupyter_output><empty_output><jupyter_text>Create an estimator and fit the modelIn order to use SageMaker to fit our algorithm, we'll create an `Estimator` that defines how to use the container to to train. This includes the configuration we need to invoke SageMaker training:* The __container name__. This is constucted as in the shell commands above.* The __role__. As defined above.* The __instance count__ which is the number of machines to use for training.* The __instance type__ which is the type of machine to use for training.* The __output path__ determines where the model artifact will be written.* The __session__ is the SageMaker session object that we defined above.Then we use fit() on the estimator to train against the data that we uploaded above.<jupyter_code>account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name
image = '{}.dkr.ecr.{}.amazonaws.com/sagemaker-keras-text-classification'.format(account, region)

tree = sage.estimator.Estimator(image,
                       role, 1, 'ml.c5.2xlarge',
                       output_path="s3://{}/output".format(sess.default_bucket()),
                       sagemaker_session=sess)

tree.fit(data_location)<jupyter_output>2019-10-09 17:51:04 Starting - Starting the training job...
2019-10-09 17:51:06 Starting - Launching requested ML instances....<jupyter_text>Deploy the modelDeploying the model to SageMaker hosting just requires a `deploy` call on the fitted model. This call takes an instance count, instance type, and optionally serializer and deserializer functions. These are used when the resulting predictor is created on the endpoint.<jupyter_code>from sagemaker.predictor import json_serializer
predictor = tree.deploy(1, 'ml.m5.xlarge', serializer=json_serializer)
#request = { "input": "‘Deadpool 2’ Has More Swearing, Slicing and Dicing from <NAME>"}
#print(predictor.predict(request).decode('utf-8'))<jupyter_output><empty_output><jupyter_text>Clean Up<jupyter_code>sess.delete_endpoint(predictor.endpoint)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07674809613292453, 'fraction_numerical': 0.014439719117792503, 'mean_word_length': 4.818181818181818, 'pattern_counts': {'":': 1, '<': 38, '<?xml version=': 0, '>': 55, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '532006', 'license': 'Apache-2.0', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2827, 'n_words': 1168, 'path': 'tf-keras/sm_tf_keras_example_updated/sagemaker-keras-text-classification-updated.ipynb', 'repo_name': 'veerathp/forecastimmersionday', 'size': '10118'}