# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: conda_mxnet_p36
#     language: python
#     name: conda_mxnet_p36
# ---

# # Sentiment Analysis with MXNet on SageMaker
#
# In this notebook, we will build and train a sentiment analysis model with MXNet on SageMaker.
# Our model will learn to classify movie reviews as positive (1) or negative (0).
#
# We will use the SST-2 dataset (Stanford Sentiment Treebank 2), which consists of of movie reviews with one sentence per review.
#
# This example is based on [SageMaker PythonSDK example](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/mxnet_gluon_sentiment/sentiment.py)

# ## Session Initialization and imports
#
# We will start by importing the modules needed, and creating a SageMaker session.

# +
import os
import boto3
import sagemaker
from sagemaker.mxnet import MXNet
from sagemaker import get_execution_role

sagemaker_session = sagemaker.Session()
# -

# ## Dataset download and preparation
#
# Next let's download the datasets into a /data dir, and then upload it to SageMaker's S3 bucket.
# Each line in the dataset has space separated tokens, the first token being the label: 1 for positive and 0 for negative.
#
# We can also check out the downloaded files in Jupyter!

# +
# Download the training data. We're downloading the Stanford Sentiment dataset
# https://nlp.stanford.edu/sentiment/index.html

# !mkdir data
# !curl https://raw.githubusercontent.com/saurabh3949/Text-Classification-Datasets/master/stsa.binary.phrases.train > data/train
# !curl https://raw.githubusercontent.com/saurabh3949/Text-Classification-Datasets/master/stsa.binary.test > data/test
# -

inputs = sagemaker_session.upload_data(path='data', key_prefix='data/sentiment-analysis')

# ## Implementing the training function
#
# Now we will wanto to implement the training logic that will run on the SageMaker platform. 
# The training scripts are essentially the same as one you would write for local training,  except that you need to provide a train function with a specific signature. 
#
# When SageMaker calls your function, it will pass in arguments that describe the training environment. Let's checkout the example below.

# !cat 'sentiment-analysis.py'

# ## Running the training script on SageMaker
#
# SageMaker's MXNet class allows us to run our training function on SageMaker infrastructure. 
# We need to configure it with our training script, an IAM role, the number of training instances, training instance type and hyper parameters. 

# +
role = get_execution_role()

m = MXNet("sentiment-analysis.py", 
          role=role, 
          train_instance_count=1, 
          train_instance_type="ml.c5.4xlarge",
          hyperparameters={'batch_size': 8, 
                         'epochs': 2, 
                         'learning_rate': 0.01, 
                         'embedding_size': 50, 
                         'log_interval': 1000})
# -

# After we've constructed our MXNet object, we can fit it using the data we uploaded to S3. SageMaker makes sure our data is available in the local filesystem, so our training script can simply read the data from disk.

m.fit(inputs)

# ## Hosting our trained model for inference
#
# As can be seen from the logs, we got > 80% accuracy on the test set.
# After training, we can host the trained MXNet model, and use it for inference.
#
# Let's deploy the model, starting with a single C5 instance:

predictor = m.deploy(initial_instance_count=1, instance_type='ml.c5.4xlarge')

# Let's use the created predictor object and run inference:

# +
data = ["this was an awesome movie!",
        "come on, you call this a movie?",
        "best one I've seen in ages",
        "i just could not watch it till the end.",
        "the movie was so enthralling !"]

response = predictor.predict(data)
print (response)
# -

# ## Cleanup
#
# After you have finished with this example, and do not need the endpoint any more, remember to delete the prediction endpoint to release the instance associated with it.

sagemaker.Session().delete_endpoint(predictor.endpoint)



StarcoderdataJupyterScriptsDedupFiltered