예제 #1
0
def _centered_bias_step(targets, loss_fn, num_label_columns):
    centered_bias = ops.get_collection("centered_bias")
    batch_size = array_ops.shape(targets)[0]
    logits = array_ops.reshape(array_ops.tile(centered_bias[0], [batch_size]),
                               [batch_size, num_label_columns])
    loss = loss_fn(logits, targets)
    return train.AdagradOptimizer(0.1).minimize(loss, var_list=centered_bias)
예제 #2
0
def _configure_optimizer(learning_rate, opt_type='adam'):
    if opt_type == 'adadelta':
        optimizer = training.AdadeltaOptimizer(learning_rate,
                                               rho=FLAGS.adadelta_rho,
                                               epsilon=FLAGS.opt_epsilon)
    elif opt_type == 'adagrad':
        optimizer = training.AdagradOptimizer(
            learning_rate,
            initial_accumulator_value=FLAGS.adagrad_initial_accumulator_value)
    elif opt_type == 'adam':
        optimizer = training.AdamOptimizer(learning_rate, )
    elif opt_type == 'ftrl':
        optimizer = training.FtrlOptimizer(
            learning_rate,
            learning_rate_power=FLAGS.ftrl_learning_rate_power,
            initial_accumulator_value=FLAGS.ftrl_initial_accumulator_value,
            l1_regularization_strength=FLAGS.ftrl_l1,
            l2_regularization_strength=FLAGS.ftrl_l2)
    elif opt_type == 'momentum':
        optimizer = training.MomentumOptimizer(learning_rate,
                                               momentum=FLAGS.momentum,
                                               name='Momentum')
    elif opt_type == 'rmsprop':
        optimizer = training.RMSPropOptimizer(learning_rate,
                                              decay=FLAGS.rmsprop_decay,
                                              momentum=FLAGS.rmsprop_momentum,
                                              epsilon=FLAGS.opt_epsilon)
    elif opt_type == 'sgd':
        optimizer = training.GradientDescentOptimizer(learning_rate)
    else:
        raise ValueError('Optimizer [%s] was not recognized', FLAGS.optimizer)
    return optimizer
예제 #3
0
 def _centered_bias_step(self, targets, weight_tensor):
   centered_bias = ops.get_collection(self._centered_bias_weight_collection)
   batch_size = array_ops.shape(targets)[0]
   logits = array_ops.reshape(
       array_ops.tile(centered_bias[0], [batch_size]),
       [batch_size, self._num_label_columns()])
   loss = self._loss(logits, targets, weight_tensor)
   # Learn central bias by an optimizer. 0.1 is a convervative lr for a single
   # variable.
   return training.AdagradOptimizer(0.1).minimize(loss, var_list=centered_bias)
예제 #4
0
def _centered_bias_step(logits_dimension, weight_collection, labels, loss_fn):
    """Creates and returns training op for centered bias."""
    centered_bias = ops.get_collection(weight_collection)
    batch_size = array_ops.shape(labels)[0]
    logits = array_ops.reshape(array_ops.tile(centered_bias[0], [batch_size]),
                               [batch_size, logits_dimension])
    with ops.name_scope(None, "centered_bias", (labels, logits)):
        centered_bias_loss = math_ops.reduce_mean(loss_fn(logits, labels),
                                                  name="training_loss")
    # Learn central bias by an optimizer. 0.1 is a convervative lr for a
    # single variable.
    return training.AdagradOptimizer(0.1).minimize(centered_bias_loss,
                                                   var_list=centered_bias)
예제 #5
0
 def _centered_bias_step(self, targets, features):
     centered_bias = ops.get_collection(
         self._centered_bias_weight_collection)
     batch_size = array_ops.shape(targets)[0]
     logits = array_ops.reshape(
         array_ops.tile(centered_bias[0], [batch_size]),
         [batch_size, self._target_column.num_label_columns])
     with ops.name_scope(None, "centered_bias", (targets, features)):
         training_loss = self._target_column.training_loss(
             logits, targets, features)
     # Learn central bias by an optimizer. 0.1 is a convervative lr for a
     # single variable.
     return training.AdagradOptimizer(0.1).minimize(training_loss,
                                                    var_list=centered_bias)
예제 #6
0
def _centered_bias_step(centered_bias, logits_dimension, labels, loss_fn):
    """Creates and returns training op for centered bias."""
    if (logits_dimension is None) or (logits_dimension < 1):
        raise ValueError("Invalid logits_dimension %s." % logits_dimension)
    batch_size = array_ops.shape(labels)[0]
    logits = array_ops.reshape(array_ops.tile(centered_bias, (batch_size, )),
                               (batch_size, logits_dimension))
    with ops.name_scope(None, "centered_bias", (labels, logits)):
        centered_bias_loss = math_ops.reduce_mean(loss_fn(logits, labels),
                                                  name="training_loss")
    # Learn central bias by an optimizer. 0.1 is a convervative lr for a
    # single variable.
    return training.AdagradOptimizer(0.1).minimize(centered_bias_loss,
                                                   var_list=(centered_bias, ),
                                                   name="centered_bias_step")
예제 #7
0
 def setUp(self):
   super(GroupwiseRankingEstimatorTest, self).setUp()
   ops.reset_default_graph()
   self._model_dir = test.get_temp_dir()
   gfile.MakeDirs(self._model_dir)
   model_fn = model.make_groupwise_ranking_fn(
       _group_score_fn,
       group_size=2,
       transform_fn=feature.make_identity_transform_fn(['context', 'weight']),
       ranking_head=head.create_ranking_head(
           loss_fn=losses.make_loss_fn(
               losses.RankingLossKey.PAIRWISE_HINGE_LOSS,
               weights_feature_name='weight'),
           optimizer=training.AdagradOptimizer(learning_rate=0.1)))
   self._estimator = estimator.Estimator(model_fn, self._model_dir)
예제 #8
0
    def __init__(self,
                 periodicities,
                 input_window_size,
                 output_window_size,
                 num_features,
                 exogenous_feature_columns=None,
                 num_time_buckets=10,
                 loss=ar_model.ARModel.NORMAL_LIKELIHOOD_LOSS,
                 hidden_layer_sizes=None,
                 anomaly_prior_probability=None,
                 anomaly_distribution=None,
                 optimizer=None,
                 model_dir=None,
                 config=None):
        """Initialize the Estimator.

    Args:
      periodicities: periodicities of the input data, in the same units as the
        time feature. Note this can be a single value or a list of values for
        multiple periodicities.
      input_window_size: Number of past time steps of data to look at when doing
        the regression.
      output_window_size: Number of future time steps to predict. Note that
        setting it to > 1 empirically seems to give a better fit.
      num_features: The dimensionality of the time series (one for univariate,
        more than one for multivariate).
      exogenous_feature_columns: A list of `tf.feature_column`s (for example
        `tf.feature_column.embedding_column`) corresponding to exogenous
        features which provide extra information to the model but are not part
        of the series to be predicted. Passed to
        `tf.compat.v1.feature_column.input_layer`.
      num_time_buckets: Number of buckets into which to divide (time %
        periodicity) for generating time based features.
      loss: Loss function to use for training. Currently supported values are
        SQUARED_LOSS and NORMAL_LIKELIHOOD_LOSS. Note that for
        NORMAL_LIKELIHOOD_LOSS, we train the covariance term as well. For
        SQUARED_LOSS, the evaluation loss is reported based on un-scaled
        observations and predictions, while the training loss is computed on
        normalized data.
      hidden_layer_sizes: list of sizes of hidden layers.
      anomaly_prior_probability: If specified, constructs a mixture model under
        which anomalies (modeled with `anomaly_distribution`) have this prior
        probability. See `AnomalyMixtureARModel`.
      anomaly_distribution: May not be specified unless
        anomaly_prior_probability is specified and is not None. Controls the
        distribution of anomalies under the mixture model. Currently either
        `ar_model.AnomalyMixtureARModel.GAUSSIAN_ANOMALY` or
        `ar_model.AnomalyMixtureARModel.CAUCHY_ANOMALY`. See
        `AnomalyMixtureARModel`. Defaults to `GAUSSIAN_ANOMALY`.
      optimizer: The optimization algorithm to use when training, inheriting
        from tf.train.Optimizer. Defaults to Adagrad with step size 0.1.
      model_dir: See `Estimator`.
      config: See `Estimator`.

    Raises:
      ValueError: For invalid combinations of arguments.
    """
        if optimizer is None:
            optimizer = train.AdagradOptimizer(0.1)
        if anomaly_prior_probability is None and anomaly_distribution is not None:
            raise ValueError("anomaly_prior_probability is required if "
                             "anomaly_distribution is specified.")
        if anomaly_prior_probability is None:
            if anomaly_distribution is None:
                anomaly_distribution = ar_model.AnomalyMixtureARModel.GAUSSIAN_ANOMALY
            model = ar_model.ARModel(
                periodicities=periodicities,
                num_features=num_features,
                prediction_model_factory=functools.partial(
                    ar_model.FlatPredictionModel,
                    hidden_layer_sizes=hidden_layer_sizes),
                exogenous_feature_columns=exogenous_feature_columns,
                num_time_buckets=num_time_buckets,
                input_window_size=input_window_size,
                output_window_size=output_window_size,
                loss=loss)
        else:
            if loss != ar_model.ARModel.NORMAL_LIKELIHOOD_LOSS:
                raise ValueError(
                    "AnomalyMixtureARModel only supports "
                    "ar_model.ARModel.NORMAL_LIKELIHOOD_LOSS for its loss argument."
                )
            model = ar_model.AnomalyMixtureARModel(
                periodicities=periodicities,
                input_window_size=input_window_size,
                output_window_size=output_window_size,
                num_features=num_features,
                prediction_model_factory=functools.partial(
                    ar_model.FlatPredictionModel,
                    hidden_layer_sizes=hidden_layer_sizes),
                exogenous_feature_columns=exogenous_feature_columns,
                num_time_buckets=num_time_buckets,
                anomaly_prior_probability=anomaly_prior_probability,
                anomaly_distribution=anomaly_distribution)
        state_manager = state_management.FilteringOnlyStateManager()
        super(ARRegressor, self).__init__(model=model,
                                          state_manager=state_manager,
                                          optimizer=optimizer,
                                          model_dir=model_dir,
                                          config=config)
예제 #9
0
def _get_default_optimizer():
  """Default optimizer for DNN models."""
  return train.AdagradOptimizer(_DEFAULT_LEARNING_RATE)