Example #1
0
 def test_dict(self):
     a = {"b": np.ones(10), "a": np.ones(20)}
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["a", "b"], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertTrue(tf.is_tensor(vals["a"]))
     self.assertTrue(tf.is_tensor(vals["b"]))
 def test_dict(self):
     a = {'b': np.ones(10), 'a': np.ones(20)}
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(['a', 'b'], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertTrue(tf.is_tensor(vals['a']))
     self.assertTrue(tf.is_tensor(vals['b']))
 def test_list(self):
     a = [np.ones(10), np.ones(20)]
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["input_1", "input_2"], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertTrue(tf.is_tensor(vals[0]))
     self.assertTrue(tf.is_tensor(vals[1]))
Example #4
0
 def test_dict_eager(self):
     if not tf.executing_eagerly():
         self.skipTest('Run in eager mode only.')
     with testing_utils.use_keras_tensors_scope(False):
         a = {'b': np.ones(10), 'a': np.ones(20)}
         model_inputs = training_utils_v1.ModelInputs(a)
         self.assertEqual(['a', 'b'], model_inputs.get_input_names())
         vals = model_inputs.get_symbolic_inputs()
         self.assertTrue(tf_utils.is_symbolic_tensor(vals['a']))
         self.assertTrue(tf_utils.is_symbolic_tensor(vals['b']))
     with testing_utils.use_keras_tensors_scope(True):
         a = {'b': np.ones(10), 'a': np.ones(20)}
         model_inputs = training_utils_v1.ModelInputs(a)
         self.assertEqual(['a', 'b'], model_inputs.get_input_names())
         vals = model_inputs.get_symbolic_inputs()
         self.assertIsInstance(vals['a'], keras_tensor.KerasTensor)
         self.assertIsInstance(vals['b'], keras_tensor.KerasTensor)
Example #5
0
 def test_dict_eager(self):
     if not tf.executing_eagerly():
         self.skipTest("Run in eager mode only.")
     a = {"b": np.ones(10), "a": np.ones(20)}
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["a", "b"], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertIsInstance(vals["a"], keras_tensor.KerasTensor)
     self.assertIsInstance(vals["b"], keras_tensor.KerasTensor)
 def test_list_eager(self):
     if not tf.executing_eagerly():
         self.skipTest("Run in eager mode only.")
     a = [np.ones(10), np.ones(20)]
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["input_1", "input_2"], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
     self.assertIsInstance(vals[1], keras_tensor.KerasTensor)
Example #7
0
 def test_single_thing(self):
     a = np.ones(10)
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["input_1"], model_inputs.get_input_names())
     vals = model_inputs.get_symbolic_inputs()
     self.assertTrue(tf.is_tensor(vals))
     vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
     self.assertEqual(1, len(vals))
     self.assertTrue(tf.is_tensor(vals[0]))
     self.assertEqual(backend.floatx(), vals[0].dtype)
Example #8
0
def _prepare_feed_values(model, inputs, targets, sample_weights, mode):
    """Prepare feed values to the model execution function.

    Args:
      model: Model to prepare feed values for.
      inputs: List or dict of model inputs.
      targets: Optional list of model targets.
      sample_weights: Optional list of sample weight arrays.
      mode: One of ModeKeys.TRAIN/ModeKeys.TEST/ModeKeys.PREDICT.

    Returns:
      Feed values for the model in the given mode.
    """
    if model._distribution_strategy:
        if isinstance(inputs, (tf.compat.v1.data.Dataset, tf.data.Dataset)):
            inputs = distributed_training_utils_v1.get_iterator(
                inputs, model._distribution_strategy)

        def get_distributed_inputs():
            return distributed_training_utils_v1._prepare_feed_values(
                model, inputs, targets, sample_weights, mode)

        # In the eager case, we want to call the input method per step, so
        # return a lambda from here that can be called. Note that this is
        # applicable only in Distribution Strategy case as it follows the same
        # code path for both eager and graph modes.
        # TODO(priyag,omalleyt): Either we should move the training DS with
        # IteratorBase to use training_generator code path, or figure out how to
        # set a symbolic Iterator out of a Dataset when in eager mode.
        if tf.executing_eagerly():
            return get_distributed_inputs
        else:
            return get_distributed_inputs()

    if isinstance(
            inputs,
        (
            tf.compat.v1.data.Dataset,
            tf.data.Dataset,
            tf.compat.v1.data.Iterator,
        ),
    ):
        inputs, targets, sample_weights = model._standardize_user_data(
            inputs, extract_tensors_from_dataset=True)

    inputs = training_utils_v1.ModelInputs(inputs).as_list()
    targets = list(targets or [])
    sample_weights = list(sample_weights or [])
    ins = inputs + targets + sample_weights
    if mode == ModeKeys.TRAIN and not isinstance(
            backend.symbolic_learning_phase(), int):
        ins += [True]  # Add learning phase value.
    return ins
Example #9
0
 def test_single_thing_eager(self):
     if not tf.executing_eagerly():
         self.skipTest("Run in eager mode only.")
     a = np.ones(10, dtype=np.int32)
     model_inputs = training_utils_v1.ModelInputs(a)
     self.assertEqual(["input_1"], model_inputs.get_input_names())
     val = model_inputs.get_symbolic_inputs()
     self.assertIsInstance(val, keras_tensor.KerasTensor)
     vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
     self.assertEqual(1, len(vals))
     self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
     self.assertEqual(tf.int32, vals[0].dtype)
Example #10
0
 def test_single_thing_eager(self):
     if not tf.executing_eagerly():
         self.skipTest('Run in eager mode only.')
     with testing_utils.use_keras_tensors_scope(False):
         a = np.ones(10, dtype=np.int32)
         model_inputs = training_utils_v1.ModelInputs(a)
         self.assertEqual(['input_1'], model_inputs.get_input_names())
         val = model_inputs.get_symbolic_inputs()
         self.assertTrue(tf_utils.is_symbolic_tensor(val))
         vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
         self.assertEqual(1, len(vals))
         self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
         self.assertEqual(tf.int32, vals[0].dtype)
     with testing_utils.use_keras_tensors_scope(True):
         a = np.ones(10, dtype=np.int32)
         model_inputs = training_utils_v1.ModelInputs(a)
         self.assertEqual(['input_1'], model_inputs.get_input_names())
         val = model_inputs.get_symbolic_inputs()
         self.assertIsInstance(val, keras_tensor.KerasTensor)
         vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
         self.assertEqual(1, len(vals))
         self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
         self.assertEqual(tf.int32, vals[0].dtype)
Example #11
0
def _prepare_feed_values(model, inputs, targets, sample_weights, mode):
    """Prepare feed values to the model execution function.

  Args:
    model: Model to prepare feed values for.
    inputs: List or dict of model inputs.
    targets: Optional list of model targets.
    sample_weights: Optional list of sample weight arrays.
    mode: One of ModeKeys.TRAIN/ModeKeys.TEST/ModeKeys.PREDICT.

  Returns:
    Feed values for the model in the given mode.
  """
    strategy = model._distribution_strategy
    inputs, targets, sample_weights = _get_input_from_iterator(inputs, model)
    if backend.is_tpu_strategy(strategy):
        if sample_weights is not None:
            raise ValueError('TPUStrategy does not support sample weights.')

    # When the inputs are dict, then we want to flatten it in the same order as
    # the input layers, such that the data are fed into the input layers in the
    # correct order.
    if isinstance(inputs, dict):
        inputs = [inputs[key] for key in model._feed_input_names]
    if is_distributing_by_cloning(model):
        inputs = flatten_per_replica_values(strategy, inputs)
        targets = flatten_per_replica_values(strategy, targets)
        # Expand 1-dimensional inputs.
        # TODO(b/124535720): Remove once this standarize data logic is shared with
        # main flow.
        inputs, targets = tf.nest.map_structure(
            training_utils_v1.standardize_single_array, (inputs, targets))
    else:
        inputs = training_utils_v1.ModelInputs(inputs).as_list()

    if mode == ModeKeys.PREDICT:
        sample_weights = []
        targets = []
    elif sample_weights is not None and is_distributing_by_cloning(model):
        if tf.executing_eagerly() and not model._compile_distribution:
            raise NotImplementedError(
                '`sample_weight` is not supported when using '
                'tf.distribute.Strategy in eager mode and '
                'cloning=True.')
        sample_weights = flatten_per_replica_values(strategy, sample_weights)

    ins = [inputs, targets, sample_weights]
    return tuple(ins)