Example #1
0
    def append_new_subnetwork(self, ensemble_name, ensemble_spec,
                              subnetwork_builder, iteration_number,
                              iteration_step, summary, features, mode, labels):
        del ensemble_name
        del ensemble_spec
        del subnetwork_builder
        del iteration_number
        del iteration_step
        del summary

        logits = [[.5]]

        estimator_spec = self._head.create_estimator_spec(features=features,
                                                          mode=mode,
                                                          labels=labels,
                                                          logits=logits)
        return _EnsembleSpec(name="test",
                             ensemble=None,
                             architecture=None,
                             predictions=estimator_spec.predictions,
                             loss=None,
                             adanet_loss=.1,
                             subnetwork_train_op=None,
                             ensemble_train_op=None,
                             eval_metric_ops=None,
                             export_outputs=estimator_spec.export_outputs)
Example #2
0
def dummy_ensemble_spec(name,
                        random_seed=42,
                        num_subnetworks=1,
                        bias=0.,
                        loss=None,
                        adanet_loss=None,
                        eval_metric_ops=None,
                        dict_predictions=False,
                        export_output_key=None,
                        train_op=None):
  """Creates a dummy `_EnsembleSpec` instance.

  Args:
    name: _EnsembleSpec's name.
    random_seed: A scalar random seed.
    num_subnetworks: The number of fake subnetworks in this ensemble.
    bias: Bias value.
    loss: Float loss to return. When None, it's picked from a random
      distribution.
    adanet_loss: Float AdaNet loss to return. When None, it's picked from a
      random distribution.
    eval_metric_ops: Optional dictionary of metric ops.
    dict_predictions: Boolean whether to return predictions as a dictionary of
      `Tensor` or just a single float `Tensor`.
    export_output_key: An `ExportOutputKeys` for faking export outputs.
    train_op: A train op.

  Returns:
    A dummy `_EnsembleSpec` instance.
  """

  if loss is None:
    loss = dummy_tensor([], random_seed)

  if adanet_loss is None:
    adanet_loss = dummy_tensor([], random_seed * 2)
  else:
    adanet_loss = tf.convert_to_tensor(adanet_loss)

  logits = dummy_tensor([], random_seed * 3)
  if dict_predictions:
    predictions = {
        "logits": logits,
        "classes": tf.cast(tf.abs(logits), dtype=tf.int64)
    }
  else:
    predictions = logits
  weighted_subnetworks = [
      WeightedSubnetwork(
          name=name,
          iteration_number=1,
          logits=dummy_tensor([2, 1], random_seed * 4),
          weight=dummy_tensor([2, 1], random_seed * 4),
          subnetwork=Subnetwork(
              last_layer=dummy_tensor([1, 2], random_seed * 4),
              logits=dummy_tensor([2, 1], random_seed * 4),
              complexity=1.,
              persisted_tensors={}))
  ]

  export_outputs = _dummy_export_outputs(export_output_key, logits, predictions)
  bias = tf.constant(bias)
  return _EnsembleSpec(
      name=name,
      ensemble=Ensemble(
          weighted_subnetworks=weighted_subnetworks * num_subnetworks,
          bias=bias,
          logits=logits,
      ),
      predictions=predictions,
      loss=loss,
      adanet_loss=adanet_loss,
      eval_metric_ops=eval_metric_ops,
      subnetwork_train_op=train_op,
      ensemble_train_op=train_op,
      export_outputs=export_outputs)