Exemple #1
0
 def testNoMixingArgsWithTypeSpecArg(self):
     with testing_utils.use_keras_tensors_scope(True):
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(shape=(4, 7),
                                   type_spec=tf.TensorSpec((2, 7, 32),
                                                           tf.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(batch_size=4,
                                   type_spec=tf.TensorSpec((7, 32),
                                                           tf.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(dtype=tf.int64,
                                   type_spec=tf.TensorSpec((7, 32),
                                                           tf.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(sparse=True,
                                   type_spec=tf.TensorSpec((7, 32),
                                                           tf.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(ragged=True,
                                   type_spec=tf.TensorSpec((7, 32),
                                                           tf.float32))
Exemple #2
0
    def testCompositeTypeSpecArgWithoutDtype(self):
        with testing_utils.use_keras_tensors_scope(True):
            for assign_variant_dtype in [False, True]:
                # Create a Keras Input
                spec = TwoTensorsSpecNoOneDtype(
                    (1, 2, 3),
                    tf.float32, (1, 2, 3),
                    tf.int64,
                    assign_variant_dtype=assign_variant_dtype)
                x = input_layer_lib.Input(type_spec=spec)

                def lambda_fn(tensors):
                    return (tf.cast(tensors.x, tf.float64) +
                            tf.cast(tensors.y, tf.float64))

                # Verify you can construct and use a model w/ this input
                model = functional.Functional(x, core.Lambda(lambda_fn)(x))

                # And that the model works
                two_tensors = TwoTensors(
                    tf.ones((1, 2, 3)) * 2.0, tf.ones(1, 2, 3))
                self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))

                # Test serialization / deserialization
                model = functional.Functional.from_config(model.get_config())
                self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
                model = model_config.model_from_json(model.to_json())
                self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
Exemple #3
0
  def context_managers(self, kwargs):
    use_keras_tensors = kwargs.pop('use_keras_tensors', None)

    if use_keras_tensors is not None:
      return [testing_utils.use_keras_tensors_scope(use_keras_tensors)]
    else:
      return []
Exemple #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)
 def test_sparse_op_layer(self):
   with testing_utils.use_keras_tensors_scope(False):
     with self.assertRaisesRegex(
         ValueError, "(?ms)Keras automatic op wrapping"
         r".*Sparse ops encountered: \[\<tf\.Operation 'Cast' type=Cast\>\]"):
       int_values = keras.Input(shape=(None,), dtype=tf.int32, sparse=True)
       float_values = tf.cast(int_values, tf.float32)
       _ = keras.Model(int_values, float_values)
 def test_to_placeholder(self, shape, batch_size, ragged_rank):
     with testing_utils.use_keras_tensors_scope(True):
         inp = layers.Input(shape=shape, batch_size=batch_size, ragged=True)
         self.assertEqual(inp.ragged_rank, ragged_rank)
         self.assertAllEqual(inp.shape, [batch_size] + list(shape))
         with tf.__internal__.FuncGraph('test').as_default():
             placeholder = inp._to_placeholder()
             self.assertEqual(placeholder.ragged_rank, ragged_rank)
             self.assertAllEqual(placeholder.shape,
                                 [batch_size] + list(shape))
Exemple #7
0
    def testInputTensorArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            x = input_layer_lib.Input(tensor=tf.zeros((7, 32)))
            self.assertAllEqual(x.shape.as_list(), [7, 32])

            # Verify you can construct and use a model w/ this input
            model = functional.Functional(x, x * 2.0)
            self.assertAllEqual(model(tf.ones(x.shape)),
                                tf.ones(x.shape) * 2.0)
 def test_ragged_op_layer(self):
   with testing_utils.use_keras_tensors_scope(False):
     with self.assertRaisesRegex(
         ValueError, '(?ms)Keras automatic op wrapping'
         '.*Ragged tensors encountered: '
         r'\[tf.RaggedTensor\(values=Tensor\("Cast:0", shape=\((\?|None),\), '
         r'dtype=float32\), row_splits=Tensor\("Placeholder_1:0", '
         r'shape=\((\?|None),\), dtype=int64\)\)\]'):
       int_values = keras.Input(shape=(None,), dtype=tf.int32, ragged=True)
       float_values = tf.cast(int_values, tf.float32)
       _ = keras.Model(int_values, float_values)
    def test_ragged_op_layer_keras_tensors(self):
        with testing_utils.use_keras_tensors_scope(True):
            int_values = keras.Input(shape=(None, ),
                                     dtype=tf.int32,
                                     ragged=True)
            float_values = tf.cast(int_values, tf.float32)
            model = keras.Model(int_values, float_values)
            model.compile(loss='mse')

            input_data = tf.ragged.constant([[1, 2], [3, 4]], dtype=np.int32)
            expected = [[1.0, 2.0], [3.0, 4.0]]
            output = model.predict(input_data)
            self.assertIsInstance(output, tf.RaggedTensor)
            self.assertAllClose(expected, output)
Exemple #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)
Exemple #11
0
    def testCompositeInputTensorArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            rt = tf.RaggedTensor.from_row_splits(
                values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
            x = input_layer_lib.Input(tensor=rt)

            # Verify you can construct and use a model w/ this input
            model = functional.Functional(x, x * 2)

            # And that the model works
            rt = tf.RaggedTensor.from_row_splits(
                values=[3, 21, 4, 1, 53, 9, 2, 6],
                row_splits=[0, 4, 4, 7, 8, 8])
            self.assertAllEqual(model(rt), rt * 2)
Exemple #12
0
    def test_sparse_op_layer_keras_tensors(self):
        with testing_utils.use_keras_tensors_scope(True):
            int_values = keras.Input(shape=(None, ),
                                     dtype=tf.int32,
                                     sparse=True)
            float_values = tf.cast(int_values, tf.float32)
            _ = keras.Model(int_values, float_values)
            model = keras.Model(int_values, float_values)
            model.compile(loss='mse')

            input_data = tf.sparse.from_dense(
                np.array([[1, 2], [3, 4]], dtype=np.int32))
            expected = [[1.0, 2.0], [3.0, 4.0]]
            output = model.predict(input_data)
            self.assertIsInstance(output, tf.SparseTensor)
            self.assertAllClose(expected, tf.sparse.to_dense(output))
Exemple #13
0
    def testTypeSpecArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            x = input_layer_lib.Input(
                type_spec=tf.TensorSpec((7, 32), tf.float32))
            self.assertAllEqual(x.shape.as_list(), [7, 32])

            # Verify you can construct and use a model w/ this input
            model = functional.Functional(x, x * 2.0)
            self.assertAllEqual(model(tf.ones(x.shape)),
                                tf.ones(x.shape) * 2.0)

            # Test serialization / deserialization
            model = functional.Functional.from_config(model.get_config())
            self.assertAllEqual(model(tf.ones(x.shape)),
                                tf.ones(x.shape) * 2.0)

            model = model_config.model_from_json(model.to_json())
            self.assertAllEqual(model(tf.ones(x.shape)),
                                tf.ones(x.shape) * 2.0)
Exemple #14
0
    def testInputTensorArgInTFFunction(self):
        with testing_utils.use_keras_tensors_scope(True):
            # We use a mutable model container instead of a model python variable,
            # because python 2.7 does not have `nonlocal`
            model_container = {}

            @tf.function
            def run_model(inp):
                if not model_container:
                    # Create a Keras Input
                    x = input_layer_lib.Input(tensor=tf.zeros((10, 16)))
                    self.assertAllEqual(x.shape.as_list(), [10, 16])

                    # Verify you can construct and use a model w/ this input
                    model_container['model'] = functional.Functional(
                        x, x * 3.0)
                return model_container['model'](inp)

            self.assertAllEqual(run_model(tf.ones((10, 16))),
                                tf.ones((10, 16)) * 3.0)
Exemple #15
0
    def testCompositeTypeSpecArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            rt = tf.RaggedTensor.from_row_splits(
                values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
            x = input_layer_lib.Input(type_spec=rt._type_spec)

            # Verify you can construct and use a model w/ this input
            model = functional.Functional(x, x * 2)

            # And that the model works
            rt = tf.RaggedTensor.from_row_splits(
                values=[3, 21, 4, 1, 53, 9, 2, 6],
                row_splits=[0, 4, 4, 7, 8, 8])
            self.assertAllEqual(model(rt), rt * 2)

            # Test serialization / deserialization
            model = functional.Functional.from_config(model.get_config())
            self.assertAllEqual(model(rt), rt * 2)
            model = model_config.model_from_json(model.to_json())
            self.assertAllEqual(model(rt), rt * 2)
Exemple #16
0
    def testCompositeTypeSpecArgInTFFunction(self):
        with testing_utils.use_keras_tensors_scope(True):
            # We use a mutable model container instead of a model pysthon variable,
            # because python 2.7 does not have `nonlocal`
            model_container = {}

            @tf.function
            def run_model(inp):
                if not model_container:
                    # Create a Keras Input
                    rt = tf.RaggedTensor.from_row_splits(
                        values=[3, 1, 4, 1, 5, 9, 2, 6],
                        row_splits=[0, 4, 4, 7, 8, 8])
                    x = input_layer_lib.Input(type_spec=rt._type_spec)

                    # Verify you can construct and use a model w/ this input
                    model_container['model'] = functional.Functional(x, x * 3)
                return model_container['model'](inp)

            # And verify the model works
            rt = tf.RaggedTensor.from_row_splits(
                values=[3, 21, 4, 1, 53, 9, 2, 6],
                row_splits=[0, 4, 4, 7, 8, 8])
            self.assertAllEqual(run_model(rt), rt * 3)
Exemple #17
0
def _v2_function_and_kerastensors_test(f, test_or_class, *args, **kwargs):
    with tf.__internal__.eager_context.eager_mode():
        with testing_utils.run_eagerly_scope(False):
            with testing_utils.use_keras_tensors_scope(True):
                f(test_or_class, *args, **kwargs)