Пример #1
0
 def _test_logits(
     self, mode, hidden_units, logits_dimension, inputs, expected_logits):
   """Tests that the expected logits are passed to mock head."""
   with ops.Graph().as_default():
     training_util.create_global_step()
     head = _mock_head(
         self,
         hidden_units=hidden_units,
         logits_dimension=logits_dimension,
         expected_logits=expected_logits)
     estimator_spec = dnn._dnn_model_fn(
         features={'age': constant_op.constant(inputs)},
         labels=constant_op.constant([[1]]),
         mode=mode,
         head=head,
         hidden_units=hidden_units,
         feature_columns=[
             feature_column.numeric_column('age',
                                           shape=np.array(inputs).shape[1:])],
         optimizer=_mock_optimizer(self, hidden_units))
     with monitored_session.MonitoredTrainingSession(
         checkpoint_dir=self._model_dir) as sess:
       if mode == model_fn.ModeKeys.TRAIN:
         sess.run(estimator_spec.train_op)
       elif mode == model_fn.ModeKeys.EVAL:
         sess.run(estimator_spec.loss)
       elif mode == model_fn.ModeKeys.PREDICT:
         sess.run(estimator_spec.predictions)
       else:
         self.fail('Invalid mode: {}'.format(mode))
Пример #2
0
 def _test_logits(self, mode, hidden_units, logits_dimension, inputs,
                  expected_logits):
     """Tests that the expected logits are passed to mock head."""
     with ops.Graph().as_default():
         training_util.create_global_step()
         head = _mock_head(self,
                           hidden_units=hidden_units,
                           logits_dimension=logits_dimension,
                           expected_logits=expected_logits)
         estimator_spec = dnn._dnn_model_fn(
             features={'age': constant_op.constant(inputs)},
             labels=constant_op.constant([[1]]),
             mode=mode,
             head=head,
             hidden_units=hidden_units,
             feature_columns=[
                 feature_column.numeric_column(
                     'age', shape=np.array(inputs).shape[1:])
             ],
             optimizer=_mock_optimizer(self, hidden_units))
         with monitored_session.MonitoredTrainingSession(
                 checkpoint_dir=self._model_dir) as sess:
             if mode == model_fn.ModeKeys.TRAIN:
                 sess.run(estimator_spec.train_op)
             elif mode == model_fn.ModeKeys.EVAL:
                 sess.run(estimator_spec.loss)
             elif mode == model_fn.ModeKeys.PREDICT:
                 sess.run(estimator_spec.predictions)
             else:
                 self.fail('Invalid mode: {}'.format(mode))
Пример #3
0
    def test_multi_feature_column_multi_dim_logits(self):
        """Tests multiple feature columns and multi-dimensional logits.

    All numbers are the same as test_multi_dim_input_multi_dim_logits. The only
    difference is that the input consists of two 1D feature columns, instead of
    one 2D feature column.
    """
        base_global_step = 100
        _create_checkpoint((
            ([[.6, .5], [-.6, -.5]], [.1, -.1]),
            ([[1., .8], [-.8, -1.]], [.2, -.2]),
            ([[-1., 1., .5], [-1., 1., .5]], [.3, -.3, .0]),
        ), base_global_step, self._model_dir)
        hidden_units = (2, 2)
        logits_dimension = 3
        inputs = ([[10.]], [[8.]])
        expected_logits = [[-0.48, 0.48, 0.39]]

        for mode in [
                model_fn.ModeKeys.TRAIN, model_fn.ModeKeys.EVAL,
                model_fn.ModeKeys.PREDICT
        ]:
            with ops.Graph().as_default():
                training_util.create_global_step()
                head = _mock_head(self,
                                  hidden_units=hidden_units,
                                  logits_dimension=logits_dimension,
                                  expected_logits=expected_logits)
                estimator_spec = dnn._dnn_model_fn(
                    features={
                        'age': constant_op.constant(inputs[0]),
                        'height': constant_op.constant(inputs[1])
                    },
                    labels=constant_op.constant([[1]]),
                    mode=mode,
                    head=head,
                    hidden_units=hidden_units,
                    feature_columns=[
                        feature_column.numeric_column('age'),
                        feature_column.numeric_column('height')
                    ],
                    optimizer=_mock_optimizer(self, hidden_units))
                with monitored_session.MonitoredTrainingSession(
                        checkpoint_dir=self._model_dir) as sess:
                    if mode == model_fn.ModeKeys.TRAIN:
                        sess.run(estimator_spec.train_op)
                    elif mode == model_fn.ModeKeys.EVAL:
                        sess.run(estimator_spec.loss)
                    elif mode == model_fn.ModeKeys.PREDICT:
                        sess.run(estimator_spec.predictions)
                    else:
                        self.fail('Invalid mode: {}'.format(mode))
Пример #4
0
 def _model_fn(features, labels, mode, config):
   return dnn_lib._dnn_model_fn(  # pylint: disable=protected-access
       features=features,
       labels=labels,
       mode=mode,
       head=head,
       hidden_units=hidden_units,
       feature_columns=tuple(feature_columns or []),
       optimizer=optimizer,
       activation_fn=activation_fn,
       dropout=dropout,
       input_layer_partitioner=input_layer_partitioner,
       config=config)
Пример #5
0
 def _model_fn(features, labels, mode, config):
     return dnn_lib._dnn_model_fn(  # pylint: disable=protected-access
         features=features,
         labels=labels,
         mode=mode,
         head=head,
         hidden_units=hidden_units,
         feature_columns=tuple(feature_columns or []),
         optimizer=optimizer,
         activation_fn=activation_fn,
         dropout=dropout,
         input_layer_partitioner=input_layer_partitioner,
         config=config)
Пример #6
0
  def test_multi_feature_column_multi_dim_logits(self):
    """Tests multiple feature columns and multi-dimensional logits.

    All numbers are the same as test_multi_dim_input_multi_dim_logits. The only
    difference is that the input consists of two 1D feature columns, instead of
    one 2D feature column.
    """
    base_global_step = 100
    _create_checkpoint((
        ([[.6, .5], [-.6, -.5]], [.1, -.1]),
        ([[1., .8], [-.8, -1.]], [.2, -.2]),
        ([[-1., 1., .5], [-1., 1., .5]], [.3, -.3, .0]),
    ), base_global_step, self._model_dir)
    hidden_units = (2, 2)
    logits_dimension = 3
    inputs = ([[10.]], [[8.]])
    expected_logits = [[-0.48, 0.48, 0.39]]

    for mode in [model_fn.ModeKeys.TRAIN,
                 model_fn.ModeKeys.EVAL,
                 model_fn.ModeKeys.PREDICT]:
      with ops.Graph().as_default():
        training_util.create_global_step()
        head = _mock_head(
            self,
            hidden_units=hidden_units,
            logits_dimension=logits_dimension,
            expected_logits=expected_logits)
        estimator_spec = dnn._dnn_model_fn(
            features={'age': constant_op.constant(inputs[0]),
                      'height': constant_op.constant(inputs[1])},
            labels=constant_op.constant([[1]]),
            mode=mode,
            head=head,
            hidden_units=hidden_units,
            feature_columns=[
                feature_column.numeric_column('age'),
                feature_column.numeric_column('height')],
            optimizer=_mock_optimizer(self, hidden_units))
        with monitored_session.MonitoredTrainingSession(
            checkpoint_dir=self._model_dir) as sess:
          if mode == model_fn.ModeKeys.TRAIN:
            sess.run(estimator_spec.train_op)
          elif mode == model_fn.ModeKeys.EVAL:
            sess.run(estimator_spec.loss)
          elif mode == model_fn.ModeKeys.PREDICT:
            sess.run(estimator_spec.predictions)
          else:
            self.fail('Invalid mode: {}'.format(mode))
Пример #7
0
    def _model_fn(features, labels, mode, params):
        """Build TF graph based on canned DNN classifier."""

        key_column = features.pop(constants.KEY_COLUMN, None)
        if key_column is None:
            raise ValueError('Key is missing from features.')

        spec = _dnn_model_fn(features=features,
                             labels=labels,
                             mode=mode,
                             **params)

        predictions = spec.predictions
        if predictions:
            predictions[
                constants.KEY_COLUMN] = tf.convert_to_tensor_or_sparse_tensor(
                    key_column)
            spec = spec._replace(predictions=predictions)
            spec = spec._replace(
                export_outputs={
                    'classes': tf.estimator.export.PredictOutput(predictions)
                })
        return spec