<jupyter_start><jupyter_text>ML model trainingMulti-label text classification training using Object2Vec Amazon SageMaker built-in algorithm.<jupyter_code>import os
import boto3
import json
import pickle
import datetime
import pandas as pd
import time
import botocore
import sagemaker
from sagemaker import get_execution_role
from sagemaker.amazon.amazon_estimator import get_image_uri
from sagemaker.predictor import json_serializer, json_deserializer
from sagemaker.tuner import CategoricalParameter, HyperparameterTuner, IntegerParameter, ContinuousParameter
bucket_name = "YOUR_BUCKET_HERE"
prefix = "connect/O2VInput"<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def download_object(bucket_name, key, local_path):
    """Download S3 object to local"""
    s3 = boto3.resource('s3')
    try:
        s3.Bucket(bucket_name).download_file(key,local_path)
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist")
        else:
            raise
def create_dir(directory):
    """Create a directory"""
    if not os.path.exists(directory):
        os.makedirs(directory)
def get_hyperparameter_ranges():
    """returns the hyperparameter ranges configured here"""
    #tunable parameters
    hyperparameter_ranges = {
        'mlp_layers': IntegerParameter(2,6),
        'early_stopping_patience': IntegerParameter(3,5),
        'mlp_activation': CategoricalParameter(['relu','tanh']),
        'dropout': ContinuousParameter(0.4, 0.8),
        'learning_rate': ContinuousParameter(0.0001, 0.001),
        'mini_batch_size': CategoricalParameter([512,1024])
    }
    return hyperparameter_ranges
def update_hyperparameter(hyperparameters, name_hyper, value_hyper):
    """update hyperparameter for non-tunable hyperparams"""
    hyperparameters[name_hyper] = value_hyper
    return hyperparameters<jupyter_output><empty_output><jupyter_text>Specify image URI for Object2Vec<jupyter_code>sess = sagemaker.Session()
from sagemaker import get_execution_role
role = get_execution_role()

## Get docker image of ObjectToVec algorithm
container = get_image_uri(boto3.Session().region_name, 'object2vec')
output_path = 's3://'+os.path.join(bucket_name, prefix.replace("O2VInput", "O2VOutput"))
download_object(bucket_name, prefix+"/meta/token_to_vocab_dict.p", "./token_to_vocab_dict.p")
import pickle
tokens=pickle.load(open("token_to_vocab_dict.p", "br"))
len(tokens)
print("My general output path for the ML model: {}".format(output_path))<jupyter_output><empty_output><jupyter_text>3.2 Sagemaker Estimator set up<jupyter_code>## object2vec estimator - run it within the VPC
o2vec2_class = sagemaker.estimator.Estimator(container,
                                          role, 
                                          instance_count=1, 
                                          instance_type='ml.m4.xlarge',
                                          output_path=output_path,
                                          )
## hyperparameter specification
hyperparameters =   {
     "_kvstore": "device",
      "_num_gpus": 'auto',
      "_num_kv_servers": "auto",
      "bucket_width": 0,
      "dropout": 0.4,
      "early_stopping_patience": 3,
      "early_stopping_tolerance": 0.001,
      "enc0_layers": "auto",
      "enc0_max_seq_len": 50,
      "enc0_network": "bilstm",
      "enc0_token_embedding_dim": 300,
      'enc0_vocab_file': "", 
      "enc0_vocab_size": len(tokens),
      "enc1_network": "enc0",
      "enc_dim": 600,
      "epochs": 100,
      "learning_rate": 0.001,
      "mini_batch_size": 1024,
      "mlp_activation": "relu",
      "mlp_dim": 512,
      "mlp_layers": 2,
      "num_classes": 2, #either 0 or 1.
      "optimizer": "adam",
      "output_layer": "softmax",
      "weight_decay": 0,
}
#input channels for validation, auxiliary (glove pretrained and vocabulary) and training.
channels = {
    'auxiliary': sagemaker.inputs.TrainingInput('s3://'+os.path.join(bucket_name, prefix, 'auxiliary/'),
                                            distribution='FullyReplicated', 
                                            content_type='application/json'),
    'train': sagemaker.inputs.TrainingInput('s3://'+os.path.join(bucket_name, prefix, 'train/train.jsonl'), 
                                            distribution='ShardedByS3Key', 
                                        content_type='application/jsonlines'),
    'validation': sagemaker.inputs.TrainingInput('s3://'+os.path.join(bucket_name, prefix, 'val/val.jsonl'), 
                                             distribution='ShardedByS3Key', 
                                             content_type='application/jsonlines'),
}<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>job_name = 'default' + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
print("Training with job name", job_name)
o2vec2_class.set_hyperparameters(**hyperparameters)
o2vec2_class.fit(channels, job_name=job_name, wait=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1154226885765195, 'fraction_numerical': 0.019798059790140567, 'mean_word_length': 3.206494587843464, 'pattern_counts': {'":': 25, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '171961', 'license': 'MIT-0', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1499, 'n_words': 286, 'path': 'notebooks/connect_02_object2vec.ipynb', 'repo_name': 'aws-samples/contact-lens-for-amazon-connect-data-gathering-mechanism', 'size': '5051'}