13
13
from sagemaker .amazon .amazon_estimator import AmazonAlgorithmEstimatorBase , registry
14
14
from sagemaker .amazon .common import numpy_to_record_serializer , record_deserializer
15
15
from 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
17
17
from sagemaker .predictor import RealTimePredictor
18
18
from sagemaker .model import Model
19
19
from sagemaker .session import Session
20
20
21
21
22
22
class LinearLearner (AmazonAlgorithmEstimatorBase ):
23
-
24
23
repo_name = 'linear-learner'
25
24
repo_version = 1
26
25
@@ -32,27 +31,30 @@ class LinearLearner(AmazonAlgorithmEstimatorBase):
32
31
data_type = str )
33
32
target_recall = hp ('target_recall' , (gt (0 ), lt (1 )), "A float in (0,1)" , float )
34
33
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 )
35
36
epochs = hp ('epochs' , gt (0 ), "An integer greater-than 0" , int )
36
37
predictor_type = hp ('predictor_type' , isin ('binary_classifier' , 'regressor' ),
37
38
'One of "binary_classifier" or "regressor"' , str )
38
39
use_bias = hp ('use_bias' , (), "Either True or False" , bool )
39
40
num_models = hp ('num_models' , gt (0 ), "An integer greater-than 0" , int )
40
41
num_calibration_samples = hp ('num_calibration_samples' , gt (0 ), "An integer greater-than 0" , int )
41
42
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 )
44
45
init_bias = hp ('init_bias' , (), 'A number' , float )
45
46
optimizer = hp ('optimizer' , isin ('sgd' , 'adam' , 'auto' ), 'One of "sgd", "adam" or "auto' , str )
46
47
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 )
54
56
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 )
56
58
use_lr_scheduler = hp ('use_lr_scheduler' , (), 'A boolean' , bool )
57
59
lr_scheduler_step = hp ('lr_scheduler_step' , gt (0 ), 'An integer greater-than 0' , int )
58
60
lr_scheduler_factor = hp ('lr_scheduler_factor' , (gt (0 ), lt (1 )), 'A float in (0,1)' , float )
@@ -62,15 +64,23 @@ class LinearLearner(AmazonAlgorithmEstimatorBase):
62
64
unbias_data = hp ('unbias_data' , (), 'A boolean' , bool )
63
65
unbias_label = hp ('unbias_label' , (), 'A boolean' , bool )
64
66
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 )
65
73
66
74
def __init__ (self , role , train_instance_count , train_instance_type , predictor_type ,
67
75
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 ,
69
77
num_calibration_samples = None , init_method = None , init_scale = None , init_sigma = None , init_bias = None ,
70
78
optimizer = None , loss = None , wd = None , l1 = None , momentum = None , learning_rate = None , beta_1 = None ,
71
79
beta_2 = None , bias_lr_mult = None , bias_wd_mult = None , use_lr_scheduler = None , lr_scheduler_step = None ,
72
80
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 ):
74
84
"""An :class:`Estimator` for binary classification and regression.
75
85
76
86
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
113
123
precision_at_target_recall.
114
124
target_precision (float): Target precision. Only applicable if binary_classifier_model_selection_criteria
115
125
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.
116
128
epochs (int): The maximum number of passes to make over the training data.
117
129
use_bias (bool): Whether to include a bias field
118
130
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
125
137
init_sigma (float): For "normal" init, the standard-deviation.
126
138
init_bias (float): Initial weight for bias term
127
139
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'
129
142
wd (float): L2 regularization parameter i.e. the weight decay parameter. Use 0 for no L2 regularization.
130
143
l1 (float): L1 regularization parameter. Use 0 for no L1 regularization.
131
144
momentum (float): Momentum parameter of sgd optimizer.
@@ -150,13 +163,28 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
150
163
ubias_label (bool): If true, labels are modified to have mean 0.0.
151
164
num_point_for_scaler (int): The number of data points to use for calculating the normalizing and
152
165
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.
153
180
**kwargs: base class keyword argument values.
154
181
"""
155
182
super (LinearLearner , self ).__init__ (role , train_instance_count , train_instance_type , ** kwargs )
156
183
self .predictor_type = predictor_type
157
184
self .binary_classifier_model_selection_criteria = binary_classifier_model_selection_criteria
158
185
self .target_recall = target_recall
159
186
self .target_precision = target_precision
187
+ self .positive_example_weight_mult = positive_example_weight_mult
160
188
self .epochs = epochs
161
189
self .use_bias = use_bias
162
190
self .num_models = num_models
@@ -184,6 +212,12 @@ def __init__(self, role, train_instance_count, train_instance_type, predictor_ty
184
212
self .unbias_data = unbias_data
185
213
self .unbias_label = unbias_label
186
214
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
187
221
188
222
def create_model (self ):
189
223
"""Return a :class:`~sagemaker.amazon.kmeans.LinearLearnerModel` referencing the latest
0 commit comments