class KNN(AmazonAlgorithmEstimatorBase): """An index-based algorithm. It uses a non-parametric method for classification or regression. For classification problems, the algorithm queries the k points that are closest to the sample point and returns the most frequently used label of their class as the predicted label. For regression problems, the algorithm queries the k closest points to the sample point and returns the average of their feature values as the predicted value. """ repo_name = "knn" repo_version = 1 k = hp("k", (ge(1)), "An integer greater than 0", int) sample_size = hp("sample_size", (ge(1)), "An integer greater than 0", int) predictor_type = hp("predictor_type", isin("classifier", "regressor"), 'One of "classifier" or "regressor"', str) dimension_reduction_target = hp( "dimension_reduction_target", (ge(1)), "An integer greater than 0 and less than feature_dim", int, ) dimension_reduction_type = hp("dimension_reduction_type", isin("sign", "fjlt"), 'One of "sign" or "fjlt"', str) index_metric = hp( "index_metric", isin("COSINE", "INNER_PRODUCT", "L2"), 'One of "COSINE", "INNER_PRODUCT", "L2"', str, ) index_type = hp( "index_type", isin("faiss.Flat", "faiss.IVFFlat", "faiss.IVFPQ"), 'One of "faiss.Flat", "faiss.IVFFlat", "faiss.IVFPQ"', str, ) faiss_index_ivf_nlists = hp("faiss_index_ivf_nlists", (), '"auto" or an integer greater than 0', str) faiss_index_pq_m = hp("faiss_index_pq_m", (ge(1)), "An integer greater than 0", int) def __init__(self, role, instance_count=None, instance_type=None, k=None, sample_size=None, predictor_type=None, dimension_reduction_type=None, dimension_reduction_target=None, index_type=None, index_metric=None, faiss_index_ivf_nlists=None, faiss_index_pq_m=None, **kwargs): """k-nearest neighbors (KNN) is :class:`Estimator` used for classification and regression. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. It requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. There is an utility :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.record_set` that can be used to upload data to S3 and creates :class:`~sagemaker.amazon.amazon_estimator.RecordSet` to be passed to the `fit` call. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, deploy returns a :class:`~sagemaker.amazon.knn.KNNPredictor` object that can be used for inference calls using the trained model hosted in the SageMaker Endpoint. KNN Estimators can be configured by setting hyperparameters. The available hyperparameters for KNN are documented below. For further information on the AWS KNN algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/knn.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. instance_count: instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. k (int): Required. Number of nearest neighbors. sample_size (int): Required. Number of data points to be sampled from the training data set. predictor_type (str): Required. Type of inference to use on the data's labels, allowed values are 'classifier' and 'regressor'. dimension_reduction_type (str): Optional. Type of dimension reduction technique to use. Valid values: "sign", "fjlt" dimension_reduction_target (int): Optional. Target dimension to reduce to. Required when dimension_reduction_type is specified. index_type (str): Optional. Type of index to use. Valid values are "faiss.Flat", "faiss.IVFFlat", "faiss.IVFPQ". index_metric (str): Optional. Distance metric to measure between points when finding nearest neighbors. Valid values are "COSINE", "INNER_PRODUCT", "L2" faiss_index_ivf_nlists (str): Optional. Number of centroids to construct in the index if index_type is "faiss.IVFFlat" or "faiss.IVFPQ". faiss_index_pq_m (int): Optional. Number of vector sub-components to construct in the index, if index_type is "faiss.IVFPQ". **kwargs: base class keyword argument values. .. tip:: You can find additional parameters for initializing this class at :class:`~sagemaker.estimator.amazon_estimator.AmazonAlgorithmEstimatorBase` and :class:`~sagemaker.estimator.EstimatorBase`. """ super(KNN, self).__init__(role, instance_count, instance_type, **kwargs) self.k = k self.sample_size = sample_size self.predictor_type = predictor_type self.dimension_reduction_type = dimension_reduction_type self.dimension_reduction_target = dimension_reduction_target self.index_type = index_type self.index_metric = index_metric self.faiss_index_ivf_nlists = faiss_index_ivf_nlists self.faiss_index_pq_m = faiss_index_pq_m if dimension_reduction_type and not dimension_reduction_target: raise ValueError( '"dimension_reduction_target" is required when "dimension_reduction_type" is set.' ) def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT, **kwargs): """Return a :class:`~sagemaker.amazon.KNNModel`. It references the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. **kwargs: Additional kwargs passed to the KNNModel constructor. """ return KNNModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), **kwargs) def _prepare_for_training(self, records, mini_batch_size=None, job_name=None): """Placeholder docstring""" super(KNN, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name)
class FactorizationMachines(AmazonAlgorithmEstimatorBase): repo = 'factorization-machines:1' num_factors = hp('num_factors', (gt(0), isint), 'An integer greater than zero') predictor_type = hp('predictor_type', isin('binary_classifier', 'regressor'), 'Value "binary_classifier" or "regressor"') epochs = hp('epochs', (gt(0), isint), "An integer greater than 0") clip_gradient = hp('clip_gradient', isnumber, "A float value") eps = hp('eps', isnumber, "A float value") rescale_grad = hp('rescale_grad', isnumber, "A float value") bias_lr = hp('bias_lr', (ge(0), isnumber), "A non-negative float") linear_lr = hp('linear_lr', (ge(0), isnumber), "A non-negative float") factors_lr = hp('factors_lr', (ge(0), isnumber), "A non-negative float") bias_wd = hp('bias_wd', (ge(0), isnumber), "A non-negative float") linear_wd = hp('linear_wd', (ge(0), isnumber), "A non-negative float") factors_wd = hp('factors_wd', (ge(0), isnumber), "A non-negative float") bias_init_method = hp('bias_init_method', isin('normal', 'uniform', 'constant'), 'Value "normal", "uniform" or "constant"') bias_init_scale = hp('bias_init_scale', (ge(0), isnumber), "A non-negative float") bias_init_sigma = hp('bias_init_sigma', (ge(0), isnumber), "A non-negative float") bias_init_value = hp('bias_init_value', isnumber, "A float value") linear_init_method = hp('linear_init_method', isin('normal', 'uniform', 'constant'), 'Value "normal", "uniform" or "constant"') linear_init_scale = hp('linear_init_scale', (ge(0), isnumber), "A non-negative float") linear_init_sigma = hp('linear_init_sigma', (ge(0), isnumber), "A non-negative float") linear_init_value = hp('linear_init_value', isnumber, "A float value") factors_init_method = hp('factors_init_method', isin('normal', 'uniform', 'constant'), 'Value "normal", "uniform" or "constant"') factors_init_scale = hp('factors_init_scale', (ge(0), isnumber), "A non-negative float") factors_init_sigma = hp('factors_init_sigma', (ge(0), isnumber), "A non-negative float") factors_init_value = hp('factors_init_value', isnumber, "A float value") def __init__(self, role, train_instance_count, train_instance_type, num_factors, predictor_type, epochs=None, clip_gradient=None, eps=None, rescale_grad=None, bias_lr=None, linear_lr=None, factors_lr=None, bias_wd=None, linear_wd=None, factors_wd=None, bias_init_method=None, bias_init_scale=None, bias_init_sigma=None, bias_init_value=None, linear_init_method=None, linear_init_scale=None, linear_init_sigma=None, linear_init_value=None, factors_init_method=None, factors_init_scale=None, factors_init_sigma=None, factors_init_value=None, **kwargs): """Factorization Machines is :class:`Estimator` for general-purpose supervised learning. Amazon SageMaker Factorization Machines is a general-purpose supervised learning algorithm that you can use for both classification and regression tasks. It is an extension of a linear model that is designed to parsimoniously capture interactions between features within high dimensional sparse datasets. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. It requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. There is an utility :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.record_set` that can be used to upload data to S3 and creates :class:`~sagemaker.amazon.amazon_estimator.RecordSet` to be passed to the `fit` call. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, deploy returns a :class:`~sagemaker.amazon.pca.FactorizationMachinesPredictor` object that can be used for inference calls using the trained model hosted in the SageMaker Endpoint. FactorizationMachines Estimators can be configured by setting hyperparameters. The available hyperparameters for FactorizationMachines are documented below. For further information on the AWS FactorizationMachines algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/fact-machines.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count (int): Number of Amazon EC2 instances to use for training. train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. num_factors (int): Dimensionality of factorization. predictor_type (str): Type of predictor 'binary_classifier' or 'regressor'. epochs (int): Number of training epochs to run. clip_gradient (float): Optimizer parameter. Clip the gradient by projecting onto the box [-clip_gradient, +clip_gradient] eps (float): Optimizer parameter. Small value to avoid division by 0. rescale_grad (float): Optimizer parameter. If set, multiplies the gradient with rescale_grad before updating. Often choose to be 1.0/batch_size. bias_lr (float): Non-negative learning rate for the bias term. linear_lr (float): Non-negative learning rate for linear terms. factors_lr (float): Noon-negative learning rate for factorization terms. bias_wd (float): Non-negative weight decay for the bias term. linear_wd (float): Non-negative weight decay for linear terms. factors_wd (float): Non-negative weight decay for factorization terms. bias_init_method (string): Initialization method for the bias term: 'normal', 'uniform' or 'constant'. bias_init_scale (float): Non-negative range for initialization of the bias term that takes effect when bias_init_method parameter is 'uniform' bias_init_sigma (float): Non-negative standard deviation for initialization of the bias term that takes effect when bias_init_method parameter is 'normal'. bias_init_value (float): Initial value of the bias term that takes effect when bias_init_method parameter is 'constant'. linear_init_method (string): Initialization method for linear term: 'normal', 'uniform' or 'constant'. linear_init_scale (float): Non-negative range for initialization of linear terms that takes effect when linear_init_method parameter is 'uniform'. linear_init_sigma (float): Non-negative standard deviation for initialization of linear terms that takes effect when linear_init_method parameter is 'normal'. linear_init_value (float): Initial value of linear terms that takes effect when linear_init_method parameter is 'constant'. factors_init_method (string): Initialization method for factorization term: 'normal', 'uniform' or 'constant'. factors_init_scale (float): Non-negative range for initialization of factorization terms that takes effect when factors_init_method parameter is 'uniform'. factors_init_sigma (float): Non-negative standard deviation for initialization of factorization terms that takes effect when factors_init_method parameter is 'normal'. factors_init_value (float): Initial value of factorization terms that takes effect when factors_init_method parameter is 'constant'. **kwargs: base class keyword argument values. """ super(FactorizationMachines, self).__init__(role, train_instance_count, train_instance_type, **kwargs) self.num_factors = num_factors self.predictor_type = predictor_type self.epochs = epochs self.clip_gradient = clip_gradient self.eps = eps self.rescale_grad = rescale_grad self.bias_lr = bias_lr self.linear_lr = linear_lr self.factors_lr = factors_lr self.bias_wd = bias_wd self.linear_wd = linear_wd self.factors_wd = factors_wd self.bias_init_method = bias_init_method self.bias_init_scale = bias_init_scale self.bias_init_sigma = bias_init_sigma self.bias_init_value = bias_init_value self.linear_init_method = linear_init_method self.linear_init_scale = linear_init_scale self.linear_init_sigma = linear_init_sigma self.linear_init_value = linear_init_value self.factors_init_method = factors_init_method self.factors_init_scale = factors_init_scale self.factors_init_sigma = factors_init_sigma self.factors_init_value = factors_init_value def create_model(self): """Return a :class:`~sagemaker.amazon.FactorizationMachinesModel` referencing the latest s3 model data produced by this Estimator.""" return FactorizationMachinesModel( self.model_data, self.role, sagemaker_session=self.sagemaker_session)
class Object2Vec(AmazonAlgorithmEstimatorBase): """Placeholder docstring""" repo_name = "object2vec" repo_version = 1 MINI_BATCH_SIZE = 32 enc_dim = hp("enc_dim", (ge(4), le(10000)), "An integer in [4, 10000]", int) mini_batch_size = hp("mini_batch_size", (ge(1), le(10000)), "An integer in [1, 10000]", int) epochs = hp("epochs", (ge(1), le(100)), "An integer in [1, 100]", int) early_stopping_patience = hp("early_stopping_patience", (ge(1), le(5)), "An integer in [1, 5]", int) early_stopping_tolerance = hp("early_stopping_tolerance", (ge(1e-06), le(0.1)), "A float in [1e-06, 0.1]", float) dropout = hp("dropout", (ge(0.0), le(1.0)), "A float in [0.0, 1.0]", float) weight_decay = hp("weight_decay", (ge(0.0), le(10000.0)), "A float in [0.0, 10000.0]", float) bucket_width = hp("bucket_width", (ge(0), le(100)), "An integer in [0, 100]", int) num_classes = hp("num_classes", (ge(2), le(30)), "An integer in [2, 30]", int) mlp_layers = hp("mlp_layers", (ge(1), le(10)), "An integer in [1, 10]", int) mlp_dim = hp("mlp_dim", (ge(2), le(10000)), "An integer in [2, 10000]", int) mlp_activation = hp("mlp_activation", isin("tanh", "relu", "linear"), 'One of "tanh", "relu", "linear"', str) output_layer = hp( "output_layer", isin("softmax", "mean_squared_error"), 'One of "softmax", "mean_squared_error"', str, ) optimizer = hp( "optimizer", isin("adagrad", "adam", "rmsprop", "sgd", "adadelta"), 'One of "adagrad", "adam", "rmsprop", "sgd", "adadelta"', str, ) learning_rate = hp("learning_rate", (ge(1e-06), le(1.0)), "A float in [1e-06, 1.0]", float) negative_sampling_rate = hp("negative_sampling_rate", (ge(0), le(100)), "An integer in [0, 100]", int) comparator_list = hp( "comparator_list", _list_check_subset(["hadamard", "concat", "abs_diff"]), 'Comma-separated of hadamard, concat, abs_diff. E.g. "hadamard,abs_diff"', str, ) tied_token_embedding_weight = hp("tied_token_embedding_weight", (), "Either True or False", bool) token_embedding_storage_type = hp( "token_embedding_storage_type", isin("dense", "row_sparse"), 'One of "dense", "row_sparse"', str, ) enc0_network = hp( "enc0_network", isin("hcnn", "bilstm", "pooled_embedding"), 'One of "hcnn", "bilstm", "pooled_embedding"', str, ) enc1_network = hp( "enc1_network", isin("hcnn", "bilstm", "pooled_embedding", "enc0"), 'One of "hcnn", "bilstm", "pooled_embedding", "enc0"', str, ) enc0_cnn_filter_width = hp("enc0_cnn_filter_width", (ge(1), le(9)), "An integer in [1, 9]", int) enc1_cnn_filter_width = hp("enc1_cnn_filter_width", (ge(1), le(9)), "An integer in [1, 9]", int) enc0_max_seq_len = hp("enc0_max_seq_len", (ge(1), le(5000)), "An integer in [1, 5000]", int) enc1_max_seq_len = hp("enc1_max_seq_len", (ge(1), le(5000)), "An integer in [1, 5000]", int) enc0_token_embedding_dim = hp("enc0_token_embedding_dim", (ge(2), le(1000)), "An integer in [2, 1000]", int) enc1_token_embedding_dim = hp("enc1_token_embedding_dim", (ge(2), le(1000)), "An integer in [2, 1000]", int) enc0_vocab_size = hp("enc0_vocab_size", (ge(2), le(3000000)), "An integer in [2, 3000000]", int) enc1_vocab_size = hp("enc1_vocab_size", (ge(2), le(3000000)), "An integer in [2, 3000000]", int) enc0_layers = hp("enc0_layers", (ge(1), le(4)), "An integer in [1, 4]", int) enc1_layers = hp("enc1_layers", (ge(1), le(4)), "An integer in [1, 4]", int) enc0_freeze_pretrained_embedding = hp("enc0_freeze_pretrained_embedding", (), "Either True or False", bool) enc1_freeze_pretrained_embedding = hp("enc1_freeze_pretrained_embedding", (), "Either True or False", bool) def __init__(self, role, instance_count, instance_type, epochs, enc0_max_seq_len, enc0_vocab_size, enc_dim=None, mini_batch_size=None, early_stopping_patience=None, early_stopping_tolerance=None, dropout=None, weight_decay=None, bucket_width=None, num_classes=None, mlp_layers=None, mlp_dim=None, mlp_activation=None, output_layer=None, optimizer=None, learning_rate=None, negative_sampling_rate=None, comparator_list=None, tied_token_embedding_weight=None, token_embedding_storage_type=None, enc0_network=None, enc1_network=None, enc0_cnn_filter_width=None, enc1_cnn_filter_width=None, enc1_max_seq_len=None, enc0_token_embedding_dim=None, enc1_token_embedding_dim=None, enc1_vocab_size=None, enc0_layers=None, enc1_layers=None, enc0_freeze_pretrained_embedding=None, enc1_freeze_pretrained_embedding=None, **kwargs): """Object2Vec is :class:`Estimator` used for anomaly detection. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. There is an utility :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.record_set` that can be used to upload data to S3 and creates :class:`~sagemaker.amazon.amazon_estimator.RecordSet` to be passed to the `fit` call. After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, deploy returns a :class:`~sagemaker.amazon.Predictor` object that can be used for inference calls using the trained model hosted in the SageMaker Endpoint. Object2Vec Estimators can be configured by setting hyperparameters. The available hyperparameters for Object2Vec are documented below. For further information on the AWS Object2Vec algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/object2vec.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. instance_count (int): Number of Amazon EC2 instances to use for training. instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. epochs (int): Total number of epochs for SGD training enc0_max_seq_len (int): Maximum sequence length enc0_vocab_size (int): Vocabulary size of tokens enc_dim (int): Optional. Dimension of the output of the embedding layer mini_batch_size (int): Optional. mini batch size for SGD training early_stopping_patience (int): Optional. The allowed number of consecutive epochs without improvement before early stopping is applied early_stopping_tolerance (float): Optional. The value used to determine whether the algorithm has made improvement between two consecutive epochs for early stopping dropout (float): Optional. Dropout probability on network layers weight_decay (float): Optional. Weight decay parameter during optimization bucket_width (int): Optional. The allowed difference between data sequence length when bucketing is enabled num_classes (int): Optional. Number of classes for classification training (ignored for regression problems) mlp_layers (int): Optional. Number of MLP layers in the network mlp_dim (int): Optional. Dimension of the output of MLP layer mlp_activation (str): Optional. Type of activation function for the MLP layer output_layer (str): Optional. Type of output layer optimizer (str): Optional. Type of optimizer for training learning_rate (float): Optional. Learning rate for SGD training negative_sampling_rate (int): Optional. Negative sampling rate comparator_list (str): Optional. Customization of comparator operator tied_token_embedding_weight (bool): Optional. Tying of token embedding layer weight token_embedding_storage_type (str): Optional. Type of token embedding storage enc0_network (str): Optional. Network model of encoder "enc0" enc1_network (str): Optional. Network model of encoder "enc1" enc0_cnn_filter_width (int): Optional. CNN filter width enc1_cnn_filter_width (int): Optional. CNN filter width enc1_max_seq_len (int): Optional. Maximum sequence length enc0_token_embedding_dim (int): Optional. Output dimension of token embedding layer enc1_token_embedding_dim (int): Optional. Output dimension of token embedding layer enc1_vocab_size (int): Optional. Vocabulary size of tokens enc0_layers (int): Optional. Number of layers in encoder enc1_layers (int): Optional. Number of layers in encoder enc0_freeze_pretrained_embedding (bool): Optional. Freeze pretrained embedding weights enc1_freeze_pretrained_embedding (bool): Optional. Freeze pretrained embedding weights **kwargs: base class keyword argument values. .. tip:: You can find additional parameters for initializing this class at :class:`~sagemaker.estimator.amazon_estimator.AmazonAlgorithmEstimatorBase` and :class:`~sagemaker.estimator.EstimatorBase`. """ super(Object2Vec, self).__init__(role, instance_count, instance_type, **kwargs) self.enc_dim = enc_dim self.mini_batch_size = mini_batch_size self.epochs = epochs self.early_stopping_patience = early_stopping_patience self.early_stopping_tolerance = early_stopping_tolerance self.dropout = dropout self.weight_decay = weight_decay self.bucket_width = bucket_width self.num_classes = num_classes self.mlp_layers = mlp_layers self.mlp_dim = mlp_dim self.mlp_activation = mlp_activation self.output_layer = output_layer self.optimizer = optimizer self.learning_rate = learning_rate self.negative_sampling_rate = negative_sampling_rate self.comparator_list = comparator_list self.tied_token_embedding_weight = tied_token_embedding_weight self.token_embedding_storage_type = token_embedding_storage_type self.enc0_network = enc0_network self.enc1_network = enc1_network self.enc0_cnn_filter_width = enc0_cnn_filter_width self.enc1_cnn_filter_width = enc1_cnn_filter_width self.enc0_max_seq_len = enc0_max_seq_len self.enc1_max_seq_len = enc1_max_seq_len self.enc0_token_embedding_dim = enc0_token_embedding_dim self.enc1_token_embedding_dim = enc1_token_embedding_dim self.enc0_vocab_size = enc0_vocab_size self.enc1_vocab_size = enc1_vocab_size self.enc0_layers = enc0_layers self.enc1_layers = enc1_layers self.enc0_freeze_pretrained_embedding = enc0_freeze_pretrained_embedding self.enc1_freeze_pretrained_embedding = enc1_freeze_pretrained_embedding def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT, **kwargs): """Return a :class:`~sagemaker.amazon.Object2VecModel` referencing the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. **kwargs: Additional kwargs passed to the Object2VecModel constructor. """ return Object2VecModel( self.model_data, self.role, sagemaker_session=self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), **kwargs) def _prepare_for_training(self, records, mini_batch_size=None, job_name=None): """ Args: records: mini_batch_size: job_name: """ if mini_batch_size is None: mini_batch_size = self.MINI_BATCH_SIZE super(Object2Vec, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name)
class KMeans(AmazonAlgorithmEstimatorBase): """Placeholder docstring""" repo_name = "kmeans" repo_version = 1 k = hp("k", gt(1), "An integer greater-than 1", int) init_method = hp("init_method", isin("random", "kmeans++"), 'One of "random", "kmeans++"', str) max_iterations = hp("local_lloyd_max_iter", gt(0), "An integer greater-than 0", int) tol = hp("local_lloyd_tol", (ge(0), le(1)), "An float in [0, 1]", float) num_trials = hp("local_lloyd_num_trials", gt(0), "An integer greater-than 0", int) local_init_method = hp("local_lloyd_init_method", isin("random", "kmeans++"), 'One of "random", "kmeans++"', str) half_life_time_size = hp("half_life_time_size", ge(0), "An integer greater-than-or-equal-to 0", int) epochs = hp("epochs", gt(0), "An integer greater-than 0", int) center_factor = hp("extra_center_factor", gt(0), "An integer greater-than 0", int) eval_metrics = hp( name="eval_metrics", validation_message='A comma separated list of "msd" or "ssd"', data_type=list, ) def __init__(self, role, train_instance_count, train_instance_type, k, init_method=None, max_iterations=None, tol=None, num_trials=None, local_init_method=None, half_life_time_size=None, epochs=None, center_factor=None, eval_metrics=None, **kwargs): """A k-means clustering :class:`~sagemaker.amazon.AmazonAlgorithmEstimatorBase`. Finds k clusters of data in an unlabeled dataset. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit_ndarray` or :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. The former allows a KMeans model to be fit on a 2-dimensional numpy array. The latter requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html. After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, ``deploy`` returns a :class:`~sagemaker.amazon.kmeans.KMeansPredictor` object that can be used to k-means cluster assignments, using the trained k-means model hosted in the SageMaker Endpoint. KMeans Estimators can be configured by setting hyperparameters. The available hyperparameters for KMeans are documented below. For further information on the AWS KMeans algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/k-means.html. Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count (int): Number of Amazon EC2 instances to use for training. train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. k (int): The number of clusters to produce. init_method (str): How to initialize cluster locations. One of 'random' or 'kmeans++'. max_iterations (int): Maximum iterations for Lloyds EM procedure in the local kmeans used in finalize stage. tol (float): Tolerance for change in ssd for early stopping in local kmeans. num_trials (int): Local version is run multiple times and the one with the best loss is chosen. This determines how many times. local_init_method (str): Initialization method for local version. One of 'random', 'kmeans++' half_life_time_size (int): The points can have a decayed weight. When a point is observed its weight, with regard to the computation of the cluster mean is 1. This weight will decay exponentially as we observe more points. The exponent coefficient is chosen such that after observing ``half_life_time_size`` points after the mentioned point, its weight will become 1/2. If set to 0, there will be no decay. epochs (int): Number of passes done over the training data. center_factor (int): The algorithm will create ``num_clusters * extra_center_factor`` as it runs and reduce the number of centers to ``k`` when finalizing eval_metrics (list): JSON list of metrics types to be used for reporting the score for the model. Allowed values are "msd" Means Square Error, "ssd": Sum of square distance. If test data is provided, the score shall be reported in terms of all requested metrics. **kwargs: base class keyword argument values. """ super(KMeans, self).__init__(role, train_instance_count, train_instance_type, **kwargs) self.k = k self.init_method = init_method self.max_iterations = max_iterations self.tol = tol self.num_trials = num_trials self.local_init_method = local_init_method self.half_life_time_size = half_life_time_size self.epochs = epochs self.center_factor = center_factor self.eval_metrics = eval_metrics def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT, **kwargs): """Return a :class:`~sagemaker.amazon.kmeans.KMeansModel` referencing the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. **kwargs: Additional kwargs passed to the KMeansModel constructor. """ return KMeansModel(self.model_data, self.role, self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), **kwargs) def _prepare_for_training(self, records, mini_batch_size=5000, job_name=None): """ Args: records: mini_batch_size: job_name: """ super(KMeans, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name) def hyperparameters(self): """Return the SageMaker hyperparameters for training this KMeans Estimator """ hp_dict = dict(force_dense="True" ) # KMeans requires this hp to fit on Record objects hp_dict.update(super(KMeans, self).hyperparameters()) return hp_dict
class NTM(AmazonAlgorithmEstimatorBase): """Placeholder docstring""" repo_name = "ntm" repo_version = 1 num_topics = hp("num_topics", (ge(2), le(1000)), "An integer in [2, 1000]", int) encoder_layers = hp( name="encoder_layers", validation_message="A comma separated list of " "positive integers", data_type=list, ) epochs = hp("epochs", (ge(1), le(100)), "An integer in [1, 100]", int) encoder_layers_activation = hp( "encoder_layers_activation", isin("sigmoid", "tanh", "relu"), 'One of "sigmoid", "tanh" or "relu"', str, ) optimizer = hp( "optimizer", isin("adagrad", "adam", "rmsprop", "sgd", "adadelta"), 'One of "adagrad", "adam", "rmsprop", "sgd" and "adadelta"', str, ) tolerance = hp("tolerance", (ge(1e-6), le(0.1)), "A float in [1e-6, 0.1]", float) num_patience_epochs = hp("num_patience_epochs", (ge(1), le(10)), "An integer in [1, 10]", int) batch_norm = hp(name="batch_norm", validation_message="Value must be a boolean", data_type=bool) rescale_gradient = hp("rescale_gradient", (ge(1e-3), le(1.0)), "A float in [1e-3, 1.0]", float) clip_gradient = hp("clip_gradient", ge(1e-3), "A float greater equal to 1e-3", float) weight_decay = hp("weight_decay", (ge(0.0), le(1.0)), "A float in [0.0, 1.0]", float) learning_rate = hp("learning_rate", (ge(1e-6), le(1.0)), "A float in [1e-6, 1.0]", float) def __init__( self, role, train_instance_count, train_instance_type, num_topics, encoder_layers=None, epochs=None, encoder_layers_activation=None, optimizer=None, tolerance=None, num_patience_epochs=None, batch_norm=None, rescale_gradient=None, clip_gradient=None, weight_decay=None, learning_rate=None, **kwargs ): """Neural Topic Model (NTM) is :class:`Estimator` used for unsupervised learning. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. It requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. There is an utility :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.record_set` that can be used to upload data to S3 and creates :class:`~sagemaker.amazon.amazon_estimator.RecordSet` to be passed to the `fit` call. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, deploy returns a :class:`~sagemaker.amazon.ntm.NTMPredictor` object that can be used for inference calls using the trained model hosted in the SageMaker Endpoint. NTM Estimators can be configured by setting hyperparameters. The available hyperparameters for NTM are documented below. For further information on the AWS NTM algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/ntm.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count: train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. num_topics (int): Required. The number of topics for NTM to find within the data. encoder_layers (list): Optional. Represents number of layers in the encoder and the output size of each layer. epochs (int): Optional. Maximum number of passes over the training data. encoder_layers_activation (str): Optional. Activation function to use in the encoder layers. optimizer (str): Optional. Optimizer to use for training. tolerance (float): Optional. Maximum relative change in the loss function within the last num_patience_epochs number of epochs below which early stopping is triggered. num_patience_epochs (int): Optional. Number of successive epochs over which early stopping criterion is evaluated. batch_norm (bool): Optional. Whether to use batch normalization during training. rescale_gradient (float): Optional. Rescale factor for gradient. clip_gradient (float): Optional. Maximum magnitude for each gradient component. weight_decay (float): Optional. Weight decay coefficient. Adds L2 regularization. learning_rate (float): Optional. Learning rate for the optimizer. **kwargs: base class keyword argument values. """ super(NTM, self).__init__(role, train_instance_count, train_instance_type, **kwargs) self.num_topics = num_topics self.encoder_layers = encoder_layers self.epochs = epochs self.encoder_layers_activation = encoder_layers_activation self.optimizer = optimizer self.tolerance = tolerance self.num_patience_epochs = num_patience_epochs self.batch_norm = batch_norm self.rescale_gradient = rescale_gradient self.clip_gradient = clip_gradient self.weight_decay = weight_decay self.learning_rate = learning_rate def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT): """Return a :class:`~sagemaker.amazon.NTMModel` referencing the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. """ return NTMModel( self.model_data, self.role, sagemaker_session=self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), ) def _prepare_for_training( # pylint: disable=signature-differs self, records, mini_batch_size, job_name=None ): """ Args: records: mini_batch_size: job_name: """ if mini_batch_size is not None and (mini_batch_size < 1 or mini_batch_size > 10000): raise ValueError("mini_batch_size must be in [1, 10000]") super(NTM, self)._prepare_for_training( records, mini_batch_size=mini_batch_size, job_name=job_name )
class LinearLearner(AmazonAlgorithmEstimatorBase): """Placeholder docstring""" repo_name = "linear-learner" repo_version = 1 DEFAULT_MINI_BATCH_SIZE = 1000 binary_classifier_model_selection_criteria = hp( "binary_classifier_model_selection_criteria", isin( "accuracy", "f1", "f_beta", "precision_at_target_recall", "recall_at_target_precision", "cross_entropy_loss", "loss_function", ), data_type=str, ) target_recall = hp("target_recall", (gt(0), lt(1)), "A float in (0,1)", float) target_precision = hp("target_precision", (gt(0), lt(1)), "A float in (0,1)", float) positive_example_weight_mult = hp( "positive_example_weight_mult", (), "A float greater than 0 or 'auto' or 'balanced'", str) epochs = hp("epochs", gt(0), "An integer greater-than 0", int) predictor_type = hp( "predictor_type", isin("binary_classifier", "regressor", "multiclass_classifier"), 'One of "binary_classifier" or "multiclass_classifier" or "regressor"', str, ) use_bias = hp("use_bias", (), "Either True or False", bool) num_models = hp("num_models", gt(0), "An integer greater-than 0", int) num_calibration_samples = hp("num_calibration_samples", gt(0), "An integer greater-than 0", int) init_method = hp("init_method", isin("uniform", "normal"), 'One of "uniform" or "normal"', str) init_scale = hp("init_scale", gt(0), "A float greater-than 0", float) init_sigma = hp("init_sigma", gt(0), "A float greater-than 0", float) init_bias = hp("init_bias", (), "A number", float) optimizer = hp( "optimizer", isin("sgd", "adam", "rmsprop", "auto"), 'One of "sgd", "adam", "rmsprop" or "auto', str, ) loss = hp( "loss", isin( "logistic", "squared_loss", "absolute_loss", "hinge_loss", "eps_insensitive_squared_loss", "eps_insensitive_absolute_loss", "quantile_loss", "huber_loss", "softmax_loss", "auto", ), '"logistic", "squared_loss", "absolute_loss", "hinge_loss", "eps_insensitive_squared_loss",' ' "eps_insensitive_absolute_loss", "quantile_loss", "huber_loss", "softmax_loss" or "auto"', str, ) wd = hp("wd", ge(0), "A float greater-than or equal to 0", float) l1 = hp("l1", ge(0), "A float greater-than or equal to 0", float) momentum = hp("momentum", (ge(0), lt(1)), "A float in [0,1)", float) learning_rate = hp("learning_rate", gt(0), "A float greater-than 0", float) beta_1 = hp("beta_1", (ge(0), lt(1)), "A float in [0,1)", float) beta_2 = hp("beta_2", (ge(0), lt(1)), "A float in [0,1)", float) bias_lr_mult = hp("bias_lr_mult", gt(0), "A float greater-than 0", float) bias_wd_mult = hp("bias_wd_mult", ge(0), "A float greater-than or equal to 0", float) use_lr_scheduler = hp("use_lr_scheduler", (), "A boolean", bool) lr_scheduler_step = hp("lr_scheduler_step", gt(0), "An integer greater-than 0", int) lr_scheduler_factor = hp("lr_scheduler_factor", (gt(0), lt(1)), "A float in (0,1)", float) lr_scheduler_minimum_lr = hp("lr_scheduler_minimum_lr", gt(0), "A float greater-than 0", float) normalize_data = hp("normalize_data", (), "A boolean", bool) normalize_label = hp("normalize_label", (), "A boolean", bool) unbias_data = hp("unbias_data", (), "A boolean", bool) unbias_label = hp("unbias_label", (), "A boolean", bool) num_point_for_scaler = hp("num_point_for_scaler", gt(0), "An integer greater-than 0", int) margin = hp("margin", ge(0), "A float greater-than or equal to 0", float) quantile = hp("quantile", (gt(0), lt(1)), "A float in (0,1)", float) loss_insensitivity = hp("loss_insensitivity", gt(0), "A float greater-than 0", float) huber_delta = hp("huber_delta", ge(0), "A float greater-than or equal to 0", float) early_stopping_patience = hp("early_stopping_patience", gt(0), "An integer greater-than 0", int) early_stopping_tolerance = hp("early_stopping_tolerance", gt(0), "A float greater-than 0", float) num_classes = hp("num_classes", (gt(0), le(1000000)), "An integer in [1,1000000]", int) accuracy_top_k = hp("accuracy_top_k", (gt(0), le(1000000)), "An integer in [1,1000000]", int) f_beta = hp("f_beta", gt(0), "A float greater-than 0", float) balance_multiclass_weights = hp("balance_multiclass_weights", (), "A boolean", bool) def __init__(self, role, instance_count, instance_type, predictor_type, binary_classifier_model_selection_criteria=None, target_recall=None, target_precision=None, positive_example_weight_mult=None, epochs=None, use_bias=None, num_models=None, num_calibration_samples=None, init_method=None, init_scale=None, init_sigma=None, init_bias=None, optimizer=None, loss=None, wd=None, l1=None, momentum=None, learning_rate=None, beta_1=None, beta_2=None, bias_lr_mult=None, bias_wd_mult=None, use_lr_scheduler=None, lr_scheduler_step=None, lr_scheduler_factor=None, lr_scheduler_minimum_lr=None, normalize_data=None, normalize_label=None, unbias_data=None, unbias_label=None, num_point_for_scaler=None, margin=None, quantile=None, loss_insensitivity=None, huber_delta=None, early_stopping_patience=None, early_stopping_tolerance=None, num_classes=None, accuracy_top_k=None, f_beta=None, balance_multiclass_weights=None, **kwargs): """An :class:`Estimator` for binary classification and regression. Amazon SageMaker Linear Learner provides a solution for both classification and regression problems, allowing for exploring different training objectives simultaneously and choosing the best solution from a validation set. It allows the user to explore a large number of models and choose the best, which optimizes either continuous objectives such as mean square error, cross entropy loss, absolute error, etc., or discrete objectives suited for classification such as F1 measure, precision@recall, accuracy. The implementation provides a significant speedup over naive hyperparameter optimization techniques and an added convenience, when compared with solutions providing a solution only to continuous objectives. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit_ndarray` or :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. The former allows a LinearLearner model to be fit on a 2-dimensional numpy array. The latter requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, ``deploy`` returns a :class:`~sagemaker.amazon.linear_learner.LinearLearnerPredictor` object that can be used to make class or regression predictions, using the trained model. LinearLearner Estimators can be configured by setting hyperparameters. The available hyperparameters for LinearLearner are documented below. For further information on the AWS LinearLearner algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/linear-learner.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. instance_count (int): Number of Amazon EC2 instances to use for training. instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. predictor_type (str): The type of predictor to learn. Either "binary_classifier" or "multiclass_classifier" or "regressor". binary_classifier_model_selection_criteria (str): One of 'accuracy', 'f1', 'f_beta', 'precision_at_target_recall', 'recall_at_target_precision', 'cross_entropy_loss', 'loss_function' target_recall (float): Target recall. Only applicable if binary_classifier_model_selection_criteria is precision_at_target_recall. target_precision (float): Target precision. Only applicable if binary_classifier_model_selection_criteria is recall_at_target_precision. positive_example_weight_mult (float): The importance weight of positive examples is multiplied by this constant. Useful for skewed datasets. Only applies for classification tasks. epochs (int): The maximum number of passes to make over the training data. use_bias (bool): Whether to include a bias field num_models (int): Number of models to train in parallel. If not set, the number of parallel models to train will be decided by the algorithm itself. One model will be trained according to the given training parameter (regularization, optimizer, loss) and the rest by close by parameters. num_calibration_samples (int): Number of observations to use from validation dataset for doing model calibration (finding the best threshold). init_method (str): Function to use to set the initial model weights. One of "uniform" or "normal" init_scale (float): For "uniform" init, the range of values. init_sigma (float): For "normal" init, the standard-deviation. init_bias (float): Initial weight for bias term optimizer (str): One of 'sgd', 'adam', 'rmsprop' or 'auto' loss (str): One of 'logistic', 'squared_loss', 'absolute_loss', 'hinge_loss', 'eps_insensitive_squared_loss', 'eps_insensitive_absolute_loss', 'quantile_loss', 'huber_loss' or 'softmax_loss' or 'auto'. wd (float): L2 regularization parameter i.e. the weight decay parameter. Use 0 for no L2 regularization. l1 (float): L1 regularization parameter. Use 0 for no L1 regularization. momentum (float): Momentum parameter of sgd optimizer. learning_rate (float): The SGD learning rate beta_1 (float): Exponential decay rate for first moment estimates. Only applies for adam optimizer. beta_2 (float): Exponential decay rate for second moment estimates. Only applies for adam optimizer. bias_lr_mult (float): Allows different learning rate for the bias term. The actual learning rate for the bias is learning rate times bias_lr_mult. bias_wd_mult (float): Allows different regularization for the bias term. The actual L2 regularization weight for the bias is wd times bias_wd_mult. By default there is no regularization on the bias term. use_lr_scheduler (bool): If true, we use a scheduler for the learning rate. lr_scheduler_step (int): The number of steps between decreases of the learning rate. Only applies to learning rate scheduler. lr_scheduler_factor (float): Every lr_scheduler_step the learning rate will decrease by this quantity. Only applies for learning rate scheduler. lr_scheduler_minimum_lr (float): The learning rate will never decrease to a value lower than this. Only applies for learning rate scheduler. normalize_data (bool): Normalizes the features before training to have standard deviation of 1.0. normalize_label (bool): Normalizes the regression label to have a standard deviation of 1.0. If set for classification, it will be ignored. unbias_data (bool): If true, features are modified to have mean 0.0. unbias_label (bool): If true, labels are modified to have mean 0.0. num_point_for_scaler (int): The number of data points to use for calculating the normalizing and unbiasing terms. margin (float): the margin for hinge_loss. quantile (float): Quantile for quantile loss. For quantile q, the model will attempt to produce predictions such that true_label < prediction with probability q. loss_insensitivity (float): Parameter for epsilon insensitive loss type. During training and metric evaluation, any error smaller than this is considered to be zero. huber_delta (float): Parameter for Huber loss. During training and metric evaluation, compute L2 loss for errors smaller than delta and L1 loss for errors larger than delta. early_stopping_patience (int): the number of epochs to wait before ending training if no improvement is made. The improvement is training loss if validation data is not provided, or else it is the validation loss or the binary classification model selection criteria like accuracy, f1-score etc. To disable early stopping, set early_stopping_patience to a value larger than epochs. early_stopping_tolerance (float): Relative tolerance to measure an improvement in loss. If the ratio of the improvement in loss divided by the previous best loss is smaller than this value, early stopping will consider the improvement to be zero. num_classes (int): The number of classes for the response variable. Required when predictor_type is multiclass_classifier and ignored otherwise. The classes are assumed to be labeled 0, ..., num_classes - 1. accuracy_top_k (int): The value of k when computing the Top K Accuracy metric for multiclass classification. An example is scored as correct if the model assigns one of the top k scores to the true label. f_beta (float): The value of beta to use when calculating F score metrics for binary or multiclass classification. Also used if binary_classifier_model_selection_criteria is f_beta. balance_multiclass_weights (bool): Whether to use class weights which give each class equal importance in the loss function. Only used when predictor_type is multiclass_classifier. **kwargs: base class keyword argument values. .. tip:: You can find additional parameters for initializing this class at :class:`~sagemaker.estimator.amazon_estimator.AmazonAlgorithmEstimatorBase` and :class:`~sagemaker.estimator.EstimatorBase`. """ super(LinearLearner, self).__init__(role, instance_count, instance_type, **kwargs) self.predictor_type = predictor_type self.binary_classifier_model_selection_criteria = binary_classifier_model_selection_criteria self.target_recall = target_recall self.target_precision = target_precision self.positive_example_weight_mult = positive_example_weight_mult self.epochs = epochs self.use_bias = use_bias self.num_models = num_models self.num_calibration_samples = num_calibration_samples self.init_method = init_method self.init_scale = init_scale self.init_sigma = init_sigma self.init_bias = init_bias self.optimizer = optimizer self.loss = loss self.wd = wd self.l1 = l1 self.momentum = momentum self.learning_rate = learning_rate self.beta_1 = beta_1 self.beta_2 = beta_2 self.bias_lr_mult = bias_lr_mult self.bias_wd_mult = bias_wd_mult self.use_lr_scheduler = use_lr_scheduler self.lr_scheduler_step = lr_scheduler_step self.lr_scheduler_factor = lr_scheduler_factor self.lr_scheduler_minimum_lr = lr_scheduler_minimum_lr self.normalize_data = normalize_data self.normalize_label = normalize_label self.unbias_data = unbias_data self.unbias_label = unbias_label self.num_point_for_scaler = num_point_for_scaler self.margin = margin self.quantile = quantile self.loss_insensitivity = loss_insensitivity self.huber_delta = huber_delta self.early_stopping_patience = early_stopping_patience self.early_stopping_tolerance = early_stopping_tolerance self.num_classes = num_classes self.accuracy_top_k = accuracy_top_k self.f_beta = f_beta self.balance_multiclass_weights = balance_multiclass_weights if self.predictor_type == "multiclass_classifier" and ( num_classes is None or int(num_classes) < 3): raise ValueError( "For predictor_type 'multiclass_classifier', 'num_classes' should be set to a " "value greater than 2.") def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT, **kwargs): """Return a :class:`~sagemaker.amazon.LinearLearnerModel` referencing the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. **kwargs: Additional kwargs passed to the LinearLearnerModel constructor. """ return LinearLearnerModel( self.model_data, self.role, self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), **kwargs) def _prepare_for_training(self, records, mini_batch_size=None, job_name=None): """ Args: records: mini_batch_size: job_name: """ num_records = None if isinstance(records, list): for record in records: if record.channel == "train": num_records = record.num_records break if num_records is None: raise ValueError("Must provide train channel.") else: num_records = records.num_records # mini_batch_size can't be greater than number of records or training job fails default_mini_batch_size = min( self.DEFAULT_MINI_BATCH_SIZE, max(1, int(num_records / self.instance_count))) mini_batch_size = mini_batch_size or default_mini_batch_size super(LinearLearner, self)._prepare_for_training(records, mini_batch_size=mini_batch_size, job_name=job_name)
class PCA(AmazonAlgorithmEstimatorBase): """Placeholder docstring""" repo_name = "pca" repo_version = 1 DEFAULT_MINI_BATCH_SIZE = 500 num_components = hp("num_components", gt(0), "Value must be an integer greater than zero", int) algorithm_mode = hp( "algorithm_mode", isin("regular", "randomized"), 'Value must be one of "regular" and "randomized"', str, ) subtract_mean = hp(name="subtract_mean", validation_message="Value must be a boolean", data_type=bool) extra_components = hp( name="extra_components", validation_message= "Value must be an integer greater than or equal to 0, or -1.", data_type=int, ) def __init__(self, role, train_instance_count, train_instance_type, num_components, algorithm_mode=None, subtract_mean=None, extra_components=None, **kwargs): """A Principal Components Analysis (PCA) :class:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase`. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit_ndarray` or :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. The former allows a PCA model to be fit on a 2-dimensional numpy array. The latter requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, deploy returns a :class:`~sagemaker.amazon.pca.PCAPredictor` object that can be used to project input vectors to the learned lower-dimensional representation, using the trained PCA model hosted in the SageMaker Endpoint. PCA Estimators can be configured by setting hyperparameters. The available hyperparameters for PCA are documented below. For further information on the AWS PCA algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/pca.html This Estimator uses Amazon SageMaker PCA to perform training and host deployed models. To learn more about Amazon SageMaker PCA, please read: https://docs.aws.amazon.com/sagemaker/latest/dg/how-pca-works.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count (int): Number of Amazon EC2 instances to use for training. train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. num_components (int): The number of principal components. Must be greater than zero. algorithm_mode (str): Mode for computing the principal components. One of 'regular' or 'randomized'. subtract_mean (bool): Whether the data should be unbiased both during train and at inference. extra_components (int): As the value grows larger, the solution becomes more accurate but the runtime and memory consumption increase linearly. If this value is unset or set to -1, then a default value equal to the maximum of 10 and num_components will be used. Valid for randomized mode only. **kwargs: base class keyword argument values. """ super(PCA, self).__init__(role, train_instance_count, train_instance_type, **kwargs) self.num_components = num_components self.algorithm_mode = algorithm_mode self.subtract_mean = subtract_mean self.extra_components = extra_components def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT, **kwargs): """Return a :class:`~sagemaker.amazon.pca.PCAModel` referencing the latest s3 model data produced by this Estimator. Args: vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model. Default: use subnets and security groups from this Estimator. * 'Subnets' (list[str]): List of subnet ids. * 'SecurityGroupIds' (list[str]): List of security group ids. **kwargs: Additional kwargs passed to the PCAModel constructor. """ return PCAModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session, vpc_config=self.get_vpc_config(vpc_config_override), **kwargs) def _prepare_for_training(self, records, mini_batch_size=None, job_name=None): """Set hyperparameters needed for training. Args: records (:class:`~RecordSet`): The records to train this ``Estimator`` on. mini_batch_size (int or None): The size of each mini-batch to use when training. If ``None``, a default value will be used. job_name (str): Name of the training job to be created. If not specified, one is generated, using the base name given to the constructor if applicable. """ num_records = None if isinstance(records, list): for record in records: if record.channel == "train": num_records = record.num_records break if num_records is None: raise ValueError("Must provide train channel.") else: num_records = records.num_records # mini_batch_size is a required parameter default_mini_batch_size = min( self.DEFAULT_MINI_BATCH_SIZE, max(1, int(num_records / self.train_instance_count))) use_mini_batch_size = mini_batch_size or default_mini_batch_size super(PCA, self)._prepare_for_training(records=records, mini_batch_size=use_mini_batch_size, job_name=job_name)
class ImageClassification(AmazonS3AlgorithmEstimatorBase): repo = 'image-classification:latest' num_classes = hp('num_classes', (gt(1)), 'num_classes should be an integer greater-than 1', int) num_training_samples = hp( 'num_training_samples', (gt(1)), 'num_training_samples should be an integer greater-than 1', int) use_pretrained_model = hp( 'use_pretrained_model', (isin(0, 1), ), 'use_pretrained_model should be in the set, [0,1]', int) checkpoint_frequency = hp( 'checkpoint_frequency', (ge(1), ), 'checkpoint_frequency should be an integer greater-than 1', int) num_layers = hp( 'num_layers', (isin(18, 34, 50, 101, 152, 200, 20, 32, 44, 56, 110), ), 'num_layers should be in the set [18, 34, 50, 101, 152, 200, 20, 32, 44, 56, 110]', int) resize = hp('resize', (gt(1)), 'resize should be an integer greater-than 1', int) epochs = hp('epochs', (ge(1)), 'epochs should be an integer greater-than 1', int) learning_rate = hp( 'learning_rate', (gt(0)), 'learning_rate should be a floating point greater than 0', float) lr_scheduler_factor = hp( 'lr_scheduler_factor', (gt(0)), 'lr_schedule_factor should be a floating point greater than 0', float) lr_scheduler_step = hp('lr_scheduler_step', (), 'lr_scheduler_step should be a string input.', str) optimizer = hp( 'optimizer', (isin('sgd', 'adam', 'rmsprop', 'nag')), 'Should be one optimizer among the list sgd, adam, rmsprop, or nag.', str) momentum = hp('momentum', (ge(0), le(1)), 'momentum is expected in the range 0, 1', float) weight_decay = hp('weight_decay', (ge(0), le(1)), 'weight_decay in range 0 , 1 ', float) beta_1 = hp('beta_1', (ge(0), le(1)), 'beta_1 should be in range 0, 1', float) beta_2 = hp('beta_2', (ge(0), le(1)), 'beta_2 should be in the range 0, 1', float) eps = hp('eps', (gt(0), le(1)), 'eps should be in the range 0, 1', float) gamma = hp('gamma', (ge(0), le(1)), 'gamma should be in the range 0, 1', float) mini_batch_size = hp( 'mini_batch_size', (gt(0)), 'mini_batch_size should be an integer greater than 0', int) image_shape = hp('image_shape', (), 'image_shape is expected to be a string', str) augmentation_type = hp( 'augmentation_type', (isin('crop', 'crop_color', 'crop_color_transform')), 'augmentation type must be from one option offered', str) top_k = hp('top_k', (ge(1)), 'top_k should be greater than or equal to 1', int) kv_store = hp('kv_store', (isin('dist_sync', 'dist_async')), 'Can be dist_sync or dist_async', str) def __init__(self, role, train_instance_count, train_instance_type, num_classes, num_training_samples, resize=None, lr_scheduler_step=None, use_pretrained_model=0, checkpoint_frequency=1, num_layers=18, epochs=30, learning_rate=0.1, lr_schedule_factor=0.1, optimizer='sgd', momentum=0., weight_decay=0.0001, beta_1=0.9, beta_2=0.999, eps=1e-8, gamma=0.9, mini_batch_size=32, image_shape='3,224,224', augmentation_type=None, top_k=None, kv_store=None, **kwargs): """ An Image classification algorithm :class:`~sagemaker.amazon.AmazonAlgorithmEstimatorBase`. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonS3AlgorithmEstimatorBase.fit` After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, ``deploy`` returns a :class:`~sagemaker.amazon.kmeans.ImageClassificationPredictor` object that can be used to label assignment, using the trained model hosted in the SageMaker Endpoint. ImageClassification Estimators can be configured by setting hyperparameters. The available hyperparameters for ImageClassification are documented below. For further information on the AWS ImageClassification algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/IC-Hyperparameter.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count (int): Number of Amazon EC2 instances to use for training. train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. num_classes (int): Number of output classes. This parameter defines the dimensions of the network output and is typically set to the number of classes in the dataset. num_training_samples (int): Number of training examples in the input dataset. If there is a mismatch between this value and the number of samples in the training set, then the behavior of the lr_scheduler_step parameter is undefined and distributed training accuracy might be affected. use_pretrained_model (int): Flag to indicate whether to use pre-trained model for training. If set to `1`, then the pretrained model with the corresponding number of layers is loaded and used for training. Only the top FC layer are reinitialized with random weights. Otherwise, the network is trained from scratch. Default value: 0 checkpoint_frequency (int): Period to store model parameters (in number of epochs). Default value: 1 num_layers (int): Number of layers for the network. For data with large image size (for example, 224x224 - like ImageNet), we suggest selecting the number of layers from the set [18, 34, 50, 101, 152, 200]. For data with small image size (for example, 28x28 - like CFAR), we suggest selecting the number of layers from the set [20, 32, 44, 56, 110]. The number of layers in each set is based on the ResNet paper. For transfer learning, the number of layers defines the architecture of base network and hence can only be selected from the set [18, 34, 50, 101, 152, 200]. Default value: 152 resize (int): Resize the image before using it for training. The images are resized so that the shortest side is of this parameter. If the parameter is not set, then the training data is used as such without resizing. Note: This option is available only for inputs specified as application/x-image content-type in training and validation channels. epochs (int): Number of training epochs. Default value: 30 learning_rate (float): Initial learning rate. Float. Range in [0, 1]. Default value: 0.1 lr_scheduler_factor (flaot): The ratio to reduce learning rate used in conjunction with the `lr_scheduler_step` parameter, defined as `lr_new=lr_old * lr_scheduler_factor`. Valid values: Float. Range in [0, 1]. Default value: 0.1 lr_scheduler_step (str): The epochs at which to reduce the learning rate. As explained in the ``lr_scheduler_factor`` parameter, the learning rate is reduced by ``lr_scheduler_factor`` at these epochs. For example, if the value is set to "10, 20", then the learning rate is reduced by ``lr_scheduler_factor`` after 10th epoch and again by ``lr_scheduler_factor`` after 20th epoch. The epochs are delimited by ",". optimizer (str): The optimizer types. For more details of the parameters for the optimizers, please refer to MXNet's API. Valid values: One of sgd, adam, rmsprop, or nag. Default value: `sgd`. momentum (float): The momentum for sgd and nag, ignored for other optimizers. Valid values: Float. Range in [0, 1]. Default value: 0 weight_decay (float): The coefficient weight decay for sgd and nag, ignored for other optimizers. Range in [0, 1]. Default value: 0.0001 beta_1 (float): The beta1 for adam, in other words, exponential decay rate for the first moment estimates. Range in [0, 1]. Default value: 0.9 beta_2 (float): The beta2 for adam, in other words, exponential decay rate for the second moment estimates. Range in [0, 1]. Default value: 0.999 eps (float): The epsilon for adam and rmsprop. It is usually set to a small value to avoid division by 0. Range in [0, 1]. Default value: 1e-8 gamma (float): The gamma for rmsprop. A decay factor of moving average of the squared gradient. Range in [0, 1]. Default value: 0.9 mini_batch_size (int): The batch size for training. In a single-machine multi-GPU setting, each GPU handles mini_batch_size/num_gpu training samples. For the multi-machine training in dist_sync mode, the actual batch size is mini_batch_size*number of machines. See MXNet docs for more details. Default value: 32 image_shape (str): The input image dimensions, which is the same size as the input layer of the network. \ The format is defined as 'num_channels, height, width'. The image dimension can take on any value as the network can handle varied dimensions of the input. However, there may be memory constraints if a larger image dimension is used. Typical image dimensions for image classification are '3, 224, 224'. This is similar to the ImageNet dataset. Default value: ‘3, 224, 224’ augmentation_type (str): Data augmentation type. The input images can be augmented in multiple ways as specified below. 'crop' - Randomly crop the image and flip the image horizontally 'crop_color' - In addition to ‘crop’, three random values in the range [-36, 36], [-50, 50], and [-50, 50] are added to the corresponding Hue-Saturation-Lightness channels resptly. 'crop_color_transform': In addition to crop_color, random transformations, including rotation, shear, and aspect ratio variations are applied to the image. The maximum angle of rotation is 10 degrees, the maximum shear ratio is 0.1, and the maximum aspect changing ratio is 0.25. top_k (int): Report the top-k accuracy during training. This parameter has to be greater than 1, since the top-1 training accuracy is the same as the regular training accuracy that has already been reported. kv_store (str): Weight update synchronization mode during distributed training. The weight updates can be updated either synchronously or asynchronously across machines. Synchronous updates typically provide better accuracy than asynchronous updates but can be slower. See distributed training in MXNet for more details. This parameter is not applicable to single machine training. 'dist_sync' - The gradients are synchronized after every batch with all the workers. With dist_sync, batch-size now means the batch size used on each machine. So if there are n machines and we use batch size b, then dist_sync behaves like local with batch size n*b 'dist_async'- Performs asynchronous updates. The weights are updated whenever gradients are received from any machine and the weight updates are atomic. However, the order is not guaranteed. **kwargs: base class keyword argument values. """ super(ImageClassification, self).__init__(role, train_instance_count, train_instance_type, algorithm='image_classification', **kwargs) self.num_classes = num_classes self.num_training_samples = num_training_samples self.resize = resize self.lr_scheduler_step = lr_scheduler_step self.use_pretrained_model = use_pretrained_model self.checkpoint_frequency = checkpoint_frequency self.num_layers = num_layers self.epochs = epochs self.learning_rate = learning_rate self.lr_schedule_factor = lr_schedule_factor self.optimizer = optimizer self.momentum = momentum self.weight_decay = weight_decay self.beta_1 = beta_1 self.beta_2 = beta_2 self.eps = eps self.gamma = gamma self.mini_batch_size = mini_batch_size self.image_shape = image_shape self.augmentation_type = augmentation_type self.top_k = top_k self.kv_store = kv_store def create_model(self): """Return a :class:`~sagemaker.amazon.image_classification.ImageClassification` referencing the latest s3 model data produced by this Estimator.""" return ImageClassificationModel(self.model_data, self.role, self.sagemaker_session) def hyperparameters(self): """Return the SageMaker hyperparameters for training this ImageClassification Estimator""" hp = dict() hp.update(super(ImageClassification, self).hyperparameters()) return hp
class LinearLearner(AmazonAlgorithmEstimatorBase): repo_name = 'linear-learner' repo_version = 1 DEFAULT_MINI_BATCH_SIZE = 1000 binary_classifier_model_selection_criteria = hp( 'binary_classifier_model_selection_criteria', isin('accuracy', 'f1', 'precision_at_target_recall', 'recall_at_target_precision', 'cross_entropy_loss'), data_type=str) target_recall = hp('target_recall', (gt(0), lt(1)), "A float in (0,1)", float) target_precision = hp('target_precision', (gt(0), lt(1)), "A float in (0,1)", float) positive_example_weight_mult = hp('positive_example_weight_mult', gt(0), "A float greater than 0", float) epochs = hp('epochs', gt(0), "An integer greater-than 0", int) predictor_type = hp('predictor_type', isin('binary_classifier', 'regressor'), 'One of "binary_classifier" or "regressor"', str) use_bias = hp('use_bias', (), "Either True or False", bool) num_models = hp('num_models', gt(0), "An integer greater-than 0", int) num_calibration_samples = hp('num_calibration_samples', gt(0), "An integer greater-than 0", int) init_method = hp('init_method', isin('uniform', 'normal'), 'One of "uniform" or "normal"', str) init_scale = hp('init_scale', (gt(-1), lt(1)), 'A float in (-1, 1)', float) init_sigma = hp('init_sigma', (gt(0), lt(1)), 'A float in (0, 1)', float) init_bias = hp('init_bias', (), 'A number', float) optimizer = hp('optimizer', isin('sgd', 'adam', 'auto'), 'One of "sgd", "adam" or "auto', str) loss = hp('loss', isin('logistic', 'squared_loss', 'absolute_loss', 'auto'), '"logistic", "squared_loss", "absolute_loss" or"auto"', str) wd = hp('wd', (gt(0), lt(1)), 'A float in (0,1)', float) l1 = hp('l1', (gt(0), lt(1)), 'A float in (0,1)', float) momentum = hp('momentum', (gt(0), lt(1)), 'A float in (0,1)', float) learning_rate = hp('learning_rate', (gt(0), lt(1)), 'A float in (0,1)', float) beta_1 = hp('beta_1', (gt(0), lt(1)), 'A float in (0,1)', float) beta_2 = hp('beta_1', (gt(0), lt(1)), 'A float in (0,1)', float) bias_lr_mult = hp('bias_lr_mult', gt(0), 'A float greater-than 0', float) bias_wd_mult = hp('bias_wd_mult', gt(0), 'A float greater-than 0', float) use_lr_scheduler = hp('use_lr_scheduler', (), 'A boolean', bool) lr_scheduler_step = hp('lr_scheduler_step', gt(0), 'An integer greater-than 0', int) lr_scheduler_factor = hp('lr_scheduler_factor', (gt(0), lt(1)), 'A float in (0,1)', float) lr_scheduler_minimum_lr = hp('lr_scheduler_minimum_lr', gt(0), 'A float greater-than 0', float) normalize_data = hp('normalize_data', (), 'A boolean', bool) normalize_label = hp('normalize_label', (), 'A boolean', bool) unbias_data = hp('unbias_data', (), 'A boolean', bool) unbias_label = hp('unbias_label', (), 'A boolean', bool) num_point_for_scalar = hp('num_point_for_scalar', gt(0), 'An integer greater-than 0', int) def __init__(self, role, train_instance_count, train_instance_type, predictor_type='binary_classifier', binary_classifier_model_selection_criteria=None, target_recall=None, target_precision=None, positive_example_weight_mult=None, epochs=None, use_bias=None, num_models=None, num_calibration_samples=None, init_method=None, init_scale=None, init_sigma=None, init_bias=None, optimizer=None, loss=None, wd=None, l1=None, momentum=None, learning_rate=None, beta_1=None, beta_2=None, bias_lr_mult=None, bias_wd_mult=None, use_lr_scheduler=None, lr_scheduler_step=None, lr_scheduler_factor=None, lr_scheduler_minimum_lr=None, normalize_data=None, normalize_label=None, unbias_data=None, unbias_label=None, num_point_for_scalar=None, **kwargs): """An :class:`Estimator` for binary classification and regression. Amazon SageMaker Linear Learner provides a solution for both classification and regression problems, allowing for exploring different training objectives simultaneously and choosing the best solution from a validation set. It allows the user to explore a large number of models and choose the best, which optimizes either continuous objectives such as mean square error, cross entropy loss, absolute error, etc., or discrete objectives suited for classification such as F1 measure, precision@recall, accuracy. The implementation provides a significant speedup over naive hyperparameter optimization techniques and an added convenience, when compared with solutions providing a solution only to continuous objectives. This Estimator may be fit via calls to :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit_ndarray` or :meth:`~sagemaker.amazon.amazon_estimator.AmazonAlgorithmEstimatorBase.fit`. The former allows a LinearLearner model to be fit on a 2-dimensional numpy array. The latter requires Amazon :class:`~sagemaker.amazon.record_pb2.Record` protobuf serialized data to be stored in S3. To learn more about the Amazon protobuf Record class and how to prepare bulk data in this format, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-training.html After this Estimator is fit, model data is stored in S3. The model may be deployed to an Amazon SageMaker Endpoint by invoking :meth:`~sagemaker.amazon.estimator.EstimatorBase.deploy`. As well as deploying an Endpoint, ``deploy`` returns a :class:`~sagemaker.amazon.linear_learner.LinearLearnerPredictor` object that can be used to make class or regression predictions, using the trained model. LinearLearner Estimators can be configured by setting hyperparameters. The available hyperparameters for LinearLearner are documented below. For further information on the AWS LinearLearner algorithm, please consult AWS technical documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/linear-learner.html Args: role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if accessing AWS resource. train_instance_count (int): Number of Amazon EC2 instances to use for training. train_instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. predictor_type (str): The type of predictor to learn. Either "binary_classifier" or "regressor". binary_classifier_model_selection_criteria (str): One of 'accuracy', 'f1', 'precision_at_target_recall', 'recall_at_target_precision', 'cross_entropy_loss' target_recall (float): Target recall. Only applicable if binary_classifier_model_selection_criteria is precision_at_target_recall. target_precision (float): Target precision. Only applicable if binary_classifier_model_selection_criteria is recall_at_target_precision. positive_example_weight_mult (float): The importance weight of positive examples is multiplied by this constant. Useful for skewed datasets. Only applies for classification tasks. epochs (int): The maximum number of passes to make over the training data. use_bias (bool): Whether to include a bias field num_models (int): Number of models to train in parallel. If not set, the number of parallel models to train will be decided by the algorithm itself. One model will be trained according to the given training parameter (regularization, optimizer, loss) and the rest by close by parameters. num_calibration_samples (int): Number of observations to use from validation dataset for doing model calibration (finding the best threshold). init_method (str): Function to use to set the initial model weights. One of "uniform" or "normal" init_scale (float): For "uniform" init, the range of values. init_sigma (float): For "normal" init, the standard-deviation. init_bias (float): Initial weight for bias term optimizer (str): One of 'sgd', 'adam' or 'auto' loss (str): One of 'logistic', 'squared_loss', 'absolute_loss' or 'auto' wd (float): L2 regularization parameter i.e. the weight decay parameter. Use 0 for no L2 regularization. l1 (float): L1 regularization parameter. Use 0 for no L1 regularization. momentum (float): Momentum parameter of sgd optimizer. learning_rate (float): The SGD learning rate beta_1 (float): Exponential decay rate for first moment estimates. Only applies for adam optimizer. beta_2 (float): Exponential decay rate for second moment estimates. Only applies for adam optimizer. bias_lr_mult (float): Allows different learning rate for the bias term. The actual learning rate for the bias is learning rate times bias_lr_mult. bias_wd_mult (float): Allows different regularization for the bias term. The actual L2 regularization weight for the bias is wd times bias_wd_mult. By default there is no regularization on the bias term. use_lr_scheduler (bool): If true, we use a scheduler for the learning rate. lr_scheduler_step (int): The number of steps between decreases of the learning rate. Only applies to learning rate scheduler. lr_scheduler_factor (float): Every lr_scheduler_step the learning rate will decrease by this quantity. Only applies for learning rate scheduler. lr_scheduler_minimum_lr (float): The learning rate will never decrease to a value lower than this. lr_scheduler_minimum_lr (float): Only applies for learning rate scheduler. normalize_data (bool): Normalizes the features before training to have standard deviation of 1.0. normalize_label (bool): Normalizes the regression label to have a standard deviation of 1.0. If set for classification, it will be ignored. unbias_data (bool): If true, features are modified to have mean 0.0. ubias_label (bool): If true, labels are modified to have mean 0.0. num_point_for_scaler (int): The number of data points to use for calculating the normalizing and unbiasing terms. **kwargs: base class keyword argument values. """ super(LinearLearner, self).__init__(role, train_instance_count, train_instance_type, **kwargs) self.predictor_type = predictor_type self.binary_classifier_model_selection_criteria = binary_classifier_model_selection_criteria self.target_recall = target_recall self.target_precision = target_precision self.positive_example_weight_mult = positive_example_weight_mult self.epochs = epochs self.use_bias = use_bias self.num_models = num_models self.num_calibration_samples = num_calibration_samples self.init_method = init_method self.init_scale = init_scale self.init_sigma = init_sigma self.init_bias = init_bias self.optimizer = optimizer self.loss = loss self.wd = wd self.l1 = l1 self.momentum = momentum self.learning_rate = learning_rate self.beta_1 = beta_1 self.beta_2 = beta_2 self.bias_lr_mult = bias_lr_mult self.bias_wd_mult = bias_wd_mult self.use_lr_scheduler = use_lr_scheduler self.lr_scheduler_step = lr_scheduler_step self.lr_scheduler_factor = lr_scheduler_factor self.lr_scheduler_minimum_lr = lr_scheduler_minimum_lr self.normalize_data = normalize_data self.normalize_label = normalize_label self.unbias_data = unbias_data self.ubias_label = unbias_label self.num_point_for_scaler = num_point_for_scalar def create_model(self): """Return a :class:`~sagemaker.amazon.kmeans.LinearLearnerModel` referencing the latest s3 model data produced by this Estimator.""" return LinearLearnerModel(self, self.model_data, self.role, self.sagemaker_session) def fit(self, records, mini_batch_size=None, **kwargs): # mini_batch_size can't be greater than number of records or training job fails default_mini_batch_size = min( self.DEFAULT_MINI_BATCH_SIZE, max(1, int(records.num_records / self.train_instance_count))) use_mini_batch_size = mini_batch_size or default_mini_batch_size super(LinearLearner, self).fit(records, use_mini_batch_size, **kwargs)