1313from sagemaker .amazon .amazon_estimator import AmazonAlgorithmEstimatorBase , registry
1414from sagemaker .amazon .common import numpy_to_record_serializer , record_deserializer
1515from sagemaker .amazon .hyperparameter import Hyperparameter as hp # noqa
16- from sagemaker .amazon .validation import isin , gt , lt
16+ from sagemaker .amazon .validation import isin , gt , lt , ge
1717from sagemaker .predictor import RealTimePredictor
1818from sagemaker .model import Model
1919from sagemaker .session import Session
2020
2121
2222class LinearLearner (AmazonAlgorithmEstimatorBase ):
23-
2423 repo_name = 'linear-learner'
2524 repo_version = 1
2625
@@ -32,27 +31,30 @@ class LinearLearner(AmazonAlgorithmEstimatorBase):
3231 data_type = str )
3332 target_recall = hp ('target_recall' , (gt (0 ), lt (1 )), "A float in (0,1)" , float )
3433 target_precision = hp ('target_precision' , (gt (0 ), lt (1 )), "A float in (0,1)" , float )
34+ positive_example_weight_mult = hp ('positive_example_weight_mult' , (),
35+ "A float greater than 0 or 'auto' or 'balanced'" , str )
3536 epochs = hp ('epochs' , gt (0 ), "An integer greater-than 0" , int )
3637 predictor_type = hp ('predictor_type' , isin ('binary_classifier' , 'regressor' ),
3738 'One of "binary_classifier" or "regressor"' , str )
3839 use_bias = hp ('use_bias' , (), "Either True or False" , bool )
3940 num_models = hp ('num_models' , gt (0 ), "An integer greater-than 0" , int )
4041 num_calibration_samples = hp ('num_calibration_samples' , gt (0 ), "An integer greater-than 0" , int )
4142 init_method = hp ('init_method' , isin ('uniform' , 'normal' ), 'One of "uniform" or "normal"' , str )
42- init_scale = hp ('init_scale' , ( gt (- 1 ), lt ( 1 )), 'A float in (-1, 1) ' , float )
43- init_sigma = hp ('init_sigma' , ( gt (0 ), lt ( 1 )), 'A float in (0, 1) ' , float )
43+ init_scale = hp ('init_scale' , gt (0 ), 'A float greater-than 0 ' , float )
44+ init_sigma = hp ('init_sigma' , gt (0 ), 'A float greater-than 0 ' , float )
4445 init_bias = hp ('init_bias' , (), 'A number' , float )
4546 optimizer = hp ('optimizer' , isin ('sgd' , 'adam' , 'auto' ), 'One of "sgd", "adam" or "auto' , str )
4647 loss = hp ('loss' , isin ('logistic' , 'squared_loss' , 'absolute_loss' , 'auto' ),
47- '"logistic", "squared_loss", "absolute_loss" or"auto"' , str )
48- wd = hp ('wd' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
49- l1 = hp ('l1' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
50- momentum = hp ('momentum' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
51- learning_rate = hp ('learning_rate' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
52- beta_1 = hp ('beta_1' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
53- beta_2 = hp ('beta_2' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
48+ '"logistic", "squared_loss", "absolute_loss", "hinge_loss", "eps_insensitive_squared_loss", '
49+ '"eps_insensitive_absolute_loss", "quantile_loss", "huber_loss" or "auto"' , str )
50+ wd = hp ('wd' , ge (0 ), 'A float greater-than or equal to 0' , float )
51+ l1 = hp ('l1' , ge (0 ), 'A float greater-than or equal to 0' , float )
52+ momentum = hp ('momentum' , (ge (0 ), lt (1 )), 'A float in [0,1)' , float )
53+ learning_rate = hp ('learning_rate' , gt (0 ), 'A float greater-than or equal to 0' , float )
54+ beta_1 = hp ('beta_1' , (ge (0 ), lt (1 )), 'A float in [0,1)' , float )
55+ beta_2 = hp ('beta_2' , (ge (0 ), lt (1 )), 'A float in [0,1)' , float )
5456 bias_lr_mult = hp ('bias_lr_mult' , gt (0 ), 'A float greater-than 0' , float )
55- bias_wd_mult = hp ('bias_wd_mult' , gt (0 ), 'A float greater-than 0' , float )
57+ bias_wd_mult = hp ('bias_wd_mult' , ge (0 ), 'A float greater-than or equal to 0' , float )
5658 use_lr_scheduler = hp ('use_lr_scheduler' , (), 'A boolean' , bool )
5759 lr_scheduler_step = hp ('lr_scheduler_step' , gt (0 ), 'An integer greater-than 0' , int )
5860 lr_scheduler_factor = hp ('lr_scheduler_factor' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
@@ -62,15 +64,23 @@ class LinearLearner(AmazonAlgorithmEstimatorBase):
6264 unbias_data = hp ('unbias_data' , (), 'A boolean' , bool )
6365 unbias_label = hp ('unbias_label' , (), 'A boolean' , bool )
6466 num_point_for_scaler = hp ('num_point_for_scaler' , gt (0 ), 'An integer greater-than 0' , int )
67+ margin = hp ('margin' , ge (0 ), 'A float greater-than or equal to 0' , float )
68+ quantile = hp ('quantile' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
69+ loss_insensitivity = hp ('loss_insensitivity' , gt (0 ), 'A float greater-than 0' , float )
70+ huber_delta = hp ('huber_delta' , ge (0 ), 'A float greater-than or equal to 0' , float )
71+ early_stopping_patience = hp ('early_stopping_patience' , gt (0 ), 'An integer greater-than 0' , int )
72+ early_stopping_tolerance = hp ('early_stopping_tolerance' , gt (0 ), 'A float greater-than 0' , float )
6573
6674 def __init__ (self , role , train_instance_count , train_instance_type , predictor_type ,
6775 binary_classifier_model_selection_criteria = None , target_recall = None , target_precision = None ,
68- epochs = None , use_bias = None , num_models = None ,
76+ positive_example_weight_mult = None , epochs = None , use_bias = None , num_models = None ,
6977 num_calibration_samples = None , init_method = None , init_scale = None , init_sigma = None , init_bias = None ,
7078 optimizer = None , loss = None , wd = None , l1 = None , momentum = None , learning_rate = None , beta_1 = None ,
7179 beta_2 = None , bias_lr_mult = None , bias_wd_mult = None , use_lr_scheduler = None , lr_scheduler_step = None ,
7280 lr_scheduler_factor = None , lr_scheduler_minimum_lr = None , normalize_data = None ,
73- normalize_label = None , unbias_data = None , unbias_label = None , num_point_for_scaler = None , ** kwargs ):
81+ normalize_label = None , unbias_data = None , unbias_label = None , num_point_for_scaler = None , margin = None ,
82+ quantile = None , loss_insensitivity = None , huber_delta = None , early_stopping_patience = None ,
83+ early_stopping_tolerance = None , ** kwargs ):
7484 """An :class:`Estimator` for binary classification and regression.
7585
7686 Amazon SageMaker Linear Learner provides a solution for both classification and regression problems, allowing
@@ -113,6 +123,8 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
113123 precision_at_target_recall.
114124 target_precision (float): Target precision. Only applicable if binary_classifier_model_selection_criteria
115125 is recall_at_target_precision.
126+ positive_example_weight_mult (float): The importance weight of positive examples is multiplied by this
127+ constant. Useful for skewed datasets. Only applies for classification tasks.
116128 epochs (int): The maximum number of passes to make over the training data.
117129 use_bias (bool): Whether to include a bias field
118130 num_models (int): Number of models to train in parallel. If not set, the number of parallel models to
@@ -125,7 +137,8 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
125137 init_sigma (float): For "normal" init, the standard-deviation.
126138 init_bias (float): Initial weight for bias term
127139 optimizer (str): One of 'sgd', 'adam' or 'auto'
128- loss (str): One of 'logistic', 'squared_loss', 'absolute_loss' or 'auto'
140+ loss (str): One of 'logistic', 'squared_loss', 'absolute_loss', 'hinge_loss',
141+ 'eps_insensitive_squared_loss', 'eps_insensitive_absolute_loss', 'quantile_loss', 'huber_loss' or 'auto'
129142 wd (float): L2 regularization parameter i.e. the weight decay parameter. Use 0 for no L2 regularization.
130143 l1 (float): L1 regularization parameter. Use 0 for no L1 regularization.
131144 momentum (float): Momentum parameter of sgd optimizer.
@@ -150,13 +163,28 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
150163 ubias_label (bool): If true, labels are modified to have mean 0.0.
151164 num_point_for_scaler (int): The number of data points to use for calculating the normalizing and
152165 unbiasing terms.
166+ margin (float): the margin for hinge_loss.
167+ quantile (float): Quantile for quantile loss. For quantile q, the model will attempt to produce
168+ predictions such that true_label < prediction with probability q.
169+ loss_insensitivity (float): Parameter for epsilon insensitive loss type. During training and metric
170+ evaluation, any error smaller than this is considered to be zero.
171+ huber_delta (float): Parameter for Huber loss. During training and metric evaluation, compute L2 loss for
172+ errors smaller than delta and L1 loss for errors larger than delta.
173+ early_stopping_patience (int): the number of epochs to wait before ending training if no improvement is
174+ made. The improvement is training loss if validation data is not provided, or else it is the validation
175+ loss or the binary classification model selection criteria like accuracy, f1-score etc. To disable early
176+ stopping, set early_stopping_patience to a value larger than epochs.
177+ early_stopping_tolerance (float): Relative tolerance to measure an improvement in loss. If the ratio of
178+ the improvement in loss divided by the previous best loss is smaller than this value, early stopping will
179+ consider the improvement to be zero.
153180 **kwargs: base class keyword argument values.
154181 """
155182 super (LinearLearner , self ).__init__ (role , train_instance_count , train_instance_type , ** kwargs )
156183 self .predictor_type = predictor_type
157184 self .binary_classifier_model_selection_criteria = binary_classifier_model_selection_criteria
158185 self .target_recall = target_recall
159186 self .target_precision = target_precision
187+ self .positive_example_weight_mult = positive_example_weight_mult
160188 self .epochs = epochs
161189 self .use_bias = use_bias
162190 self .num_models = num_models
@@ -184,6 +212,12 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
184212 self .unbias_data = unbias_data
185213 self .unbias_label = unbias_label
186214 self .num_point_for_scaler = num_point_for_scaler
215+ self .margin = margin
216+ self .quantile = quantile
217+ self .loss_insensitivity = loss_insensitivity
218+ self .huber_delta = huber_delta
219+ self .early_stopping_patience = early_stopping_patience
220+ self .early_stopping_tolerance = early_stopping_tolerance
187221
188222 def create_model (self ):
189223 """Return a :class:`~sagemaker.amazon.kmeans.LinearLearnerModel` referencing the latest
0 commit comments