コード例 #1
0
ファイル: training_utils_test.py プロジェクト: MFChunga/poo
 def test_single_thing_eager(self):
     with testing_utils.use_keras_tensors_scope(False):
         with context.eager_mode():
             a = np.ones(10, dtype=np.int32)
             model_inputs = training_utils.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(dtypes.int32, vals[0].dtype)
     with testing_utils.use_keras_tensors_scope(True):
         with context.eager_mode():
             a = np.ones(10, dtype=np.int32)
             model_inputs = training_utils.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(dtypes.int32, vals[0].dtype)
コード例 #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),
                    dtypes.float32, (1, 2, 3),
                    dtypes.int64,
                    assign_variant_dtype=assign_variant_dtype)
                x = input_layer_lib.Input(type_spec=spec)

                def lambda_fn(tensors):
                    return (math_ops.cast(tensors.x, dtypes.float64) +
                            math_ops.cast(tensors.y, dtypes.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(
                    array_ops.ones((1, 2, 3)) * 2.0, array_ops.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))
コード例 #3
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=tensor_spec.TensorSpec(
                                       (2, 7, 32), dtypes.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(batch_size=4,
                                   type_spec=tensor_spec.TensorSpec(
                                       (7, 32), dtypes.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(dtype=dtypes.int64,
                                   type_spec=tensor_spec.TensorSpec(
                                       (7, 32), dtypes.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(sparse=True,
                                   type_spec=tensor_spec.TensorSpec(
                                       (7, 32), dtypes.float32))
         with self.assertRaisesRegexp(
                 ValueError, 'all other args except `name` must be None'):
             input_layer_lib.Input(ragged=True,
                                   type_spec=tensor_spec.TensorSpec(
                                       (7, 32), dtypes.float32))
コード例 #4
0
ファイル: combinations.py プロジェクト: csjs0820/projectall
    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 []
コード例 #5
0
 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=dtypes.int32, sparse=True)
       float_values = math_ops.cast(int_values, dtypes.float32)
       _ = keras.Model(int_values, float_values)
コード例 #6
0
ファイル: training_utils_test.py プロジェクト: MFChunga/poo
 def test_dict_eager(self):
     with testing_utils.use_keras_tensors_scope(False):
         with context.eager_mode():
             a = {'b': np.ones(10), 'a': np.ones(20)}
             model_inputs = training_utils.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):
         with context.eager_mode():
             a = {'b': np.ones(10), 'a': np.ones(20)}
             model_inputs = training_utils.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)
コード例 #7
0
 def test_dict_eager(self):
     if not context.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)
コード例 #8
0
 def test_ragged_op_layer(self):
     with testing_utils.use_keras_tensors_scope(False):
         with self.assertRaisesRegex(ValueError,
                                     'Keras automatic op wrapping'):
             int_values = keras.Input(shape=(None, ),
                                      dtype=dtypes.int32,
                                      ragged=True)
             float_values = math_ops.cast(int_values, dtypes.float32)
             _ = keras.Model(int_values, float_values)
コード例 #9
0
 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 func_graph.FuncGraph('test').as_default():
       placeholder = inp._to_placeholder()
       self.assertEqual(placeholder.ragged_rank, ragged_rank)
       self.assertAllEqual(placeholder.shape, [batch_size] + list(shape))
コード例 #10
0
ファイル: training_utils_test.py プロジェクト: MFChunga/poo
 def test_list_eager(self):
     with testing_utils.use_keras_tensors_scope(False):
         with context.eager_mode():
             a = [np.ones(10), np.ones(20)]
             model_inputs = training_utils.ModelInputs(a)
             self.assertEqual(['input_1', 'input_2'],
                              model_inputs.get_input_names())
             vals = model_inputs.get_symbolic_inputs()
             self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
             self.assertTrue(tf_utils.is_symbolic_tensor(vals[1]))
     with testing_utils.use_keras_tensors_scope(True):
         with context.eager_mode():
             a = [np.ones(10), np.ones(20)]
             model_inputs = training_utils.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)
コード例 #11
0
    def testInputTensorArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            x = input_layer_lib.Input(tensor=array_ops.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(array_ops.ones(x.shape)),
                                array_ops.ones(x.shape) * 2.0)
コード例 #12
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=dtypes.int32, ragged=True)
       float_values = math_ops.cast(int_values, dtypes.float32)
       _ = keras.Model(int_values, float_values)
コード例 #13
0
ファイル: base_layer_utils_test.py プロジェクト: MFChunga/poo
  def test_ragged_op_layer_keras_tensors(self):
    with testing_utils.use_keras_tensors_scope(True):
      int_values = keras.Input(shape=(None,), dtype=dtypes.int32, ragged=True)
      float_values = math_ops.cast(int_values, dtypes.float32)
      model = keras.Model(int_values, float_values)
      model.compile(loss='mse')

      input_data = ragged_factory_ops.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, ragged_tensor.RaggedTensor)
      self.assertAllClose(expected, output)
コード例 #14
0
ファイル: base_layer_utils_test.py プロジェクト: MFChunga/poo
  def test_sparse_op_layer_keras_tensors(self):
    with testing_utils.use_keras_tensors_scope(True):
      int_values = keras.Input(shape=(None,), dtype=dtypes.int32, sparse=True)
      float_values = math_ops.cast(int_values, dtypes.float32)
      _ = keras.Model(int_values, float_values)
      model = keras.Model(int_values, float_values)
      model.compile(loss='mse')

      input_data = sparse_ops.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, sparse_tensor.SparseTensor)
      self.assertAllClose(expected, sparse_ops.sparse_tensor_to_dense(output))
コード例 #15
0
    def testCompositeInputTensorArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            rt = ragged_tensor.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 = ragged_tensor.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)
コード例 #16
0
    def test_sparse_output_and_dense_layer(self):
        with testing_utils.use_keras_tensors_scope(False):
            input_array = constant_op.constant([[1, 2, 3], [3, 3, 0]])

            max_tokens = 4

            input_data = keras.Input(shape=(None, ), dtype=dtypes.int32)
            encoding_layer = get_layer_class()(
                max_tokens=max_tokens,
                output_mode=category_encoding.COUNT,
                sparse=True)
            int_data = encoding_layer(input_data)
            dense_layer = keras.layers.Dense(units=1)
            output_data = dense_layer(int_data)

            model = keras.Model(inputs=input_data, outputs=output_data)
            _ = model.predict(input_array, steps=1)
コード例 #17
0
  def test_repr(self):
    kt = keras_tensor.KerasTensor(
        type_spec=tensor_spec.TensorSpec(shape=(1, 2, 3), dtype=dtypes.float32))
    expected_repr = "<KerasTensor: shape=(1, 2, 3) dtype=float32>"
    self.assertEqual(expected_repr, str(kt))
    self.assertEqual(expected_repr, repr(kt))

    kt = keras_tensor.KerasTensor(
        type_spec=tensor_spec.TensorSpec(shape=(2,), dtype=dtypes.int32),
        inferred_shape_value=[2, 3])
    expected_repr = (
        "<KerasTensor: shape=(2,) dtype=int32 inferred_value='[2, 3]'>")
    self.assertEqual(expected_repr, str(kt))
    self.assertEqual(expected_repr, repr(kt))

    kt = keras_tensor.KerasTensor(
        type_spec=sparse_tensor.SparseTensorSpec(
            shape=(1, 2, 3), dtype=dtypes.float32))
    expected_repr = (
        "<KerasTensor: type_spec=SparseTensorSpec("
        "TensorShape([1, 2, 3]), tf.float32)>")
    self.assertEqual(expected_repr, str(kt))
    self.assertEqual(expected_repr, repr(kt))

    with testing_utils.use_keras_tensors_scope(True):
      inp = layers.Input(shape=(3, 5))
      kt = layers.Dense(10)(inp)
      expected_repr = (
          "<KerasTensor: shape=(None, 3, 10) dtype=float32 (Symbolic value 0 "
          "from symbolic call 0 of layer 'dense')>")
      self.assertEqual(expected_repr, str(kt))
      self.assertEqual(expected_repr, repr(kt))

      kt = array_ops.reshape(kt, shape=(3, 5, 2))
      expected_repr = ("<KerasTensor: shape=(3, 5, 2) dtype=float32 (Symbolic "
                       "value 0 from symbolic call 0 of layer 'tf.reshape')>")
      self.assertEqual(expected_repr, str(kt))
      self.assertEqual(expected_repr, repr(kt))

      kts = array_ops.unstack(kt)
      for i in range(3):
        expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 "
                         "(Symbolic value %s from symbolic call 0 "
                         "of layer 'tf.unstack')>" % i)
        self.assertEqual(expected_repr, str(kts[i]))
        self.assertEqual(expected_repr, repr(kts[i]))
コード例 #18
0
    def testTypeSpecArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            x = input_layer_lib.Input(
                type_spec=tensor_spec.TensorSpec((7, 32), dtypes.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(array_ops.ones(x.shape)),
                                array_ops.ones(x.shape) * 2.0)

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

            model = model_config.model_from_json(model.to_json())
            self.assertAllEqual(model(array_ops.ones(x.shape)),
                                array_ops.ones(x.shape) * 2.0)
コード例 #19
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 = {}

            @def_function.function
            def run_model(inp):
                if not model_container:
                    # Create a Keras Input
                    x = input_layer_lib.Input(tensor=array_ops.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(array_ops.ones((10, 16))),
                                array_ops.ones((10, 16)) * 3.0)
コード例 #20
0
    def testCompositeTypeSpecArg(self):
        with testing_utils.use_keras_tensors_scope(True):
            # Create a Keras Input
            rt = ragged_tensor.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 = ragged_tensor.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)
コード例 #21
0
    def testCompositeInputTensorArgInTFFunction(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 = {}

            @def_function.function
            def run_model(inp):
                if not model_container:
                    # Create a Keras Input
                    rt = ragged_tensor.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_container['model'] = functional.Functional(x, x * 3)
                return model_container['model'](inp)

            # And verify the model works
            rt = ragged_tensor.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)
コード例 #22
0
def _v2_function_and_kerastensors_test(f, test_or_class, *args, **kwargs):
    with context.eager_mode():
        with testing_utils.run_eagerly_scope(False):
            with testing_utils.use_keras_tensors_scope(True):
                f(test_or_class, *args, **kwargs)
コード例 #23
0
    def test_repr_and_string(self):
        kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec(
            shape=(1, 2, 3), dtype=dtypes.float32))
        expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(1, 2, 3), "
                        "dtype=tf.float32, name=None))")
        expected_repr = "<KerasTensor: shape=(1, 2, 3) dtype=float32>"
        self.assertEqual(expected_str, str(kt))
        self.assertEqual(expected_repr, repr(kt))

        kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec(
            shape=(2, ), dtype=dtypes.int32),
                                      inferred_value=[2, 3])
        expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(2,), "
                        "dtype=tf.int32, name=None), inferred_value=[2, 3])")
        expected_repr = (
            "<KerasTensor: shape=(2,) dtype=int32 inferred_value=[2, 3]>")
        self.assertEqual(expected_str, str(kt))
        self.assertEqual(expected_repr, repr(kt))

        kt = keras_tensor.KerasTensor(type_spec=sparse_tensor.SparseTensorSpec(
            shape=(1, 2, 3), dtype=dtypes.float32))
        expected_str = ("KerasTensor(type_spec=SparseTensorSpec("
                        "TensorShape([1, 2, 3]), tf.float32))")
        expected_repr = ("<KerasTensor: type_spec=SparseTensorSpec("
                         "TensorShape([1, 2, 3]), tf.float32)>")
        self.assertEqual(expected_str, str(kt))
        self.assertEqual(expected_repr, repr(kt))

        with testing_utils.use_keras_tensors_scope(True):
            inp = layers.Input(shape=(3, 5))
            kt = layers.Dense(10)(inp)
            expected_str = (
                "KerasTensor(type_spec=TensorSpec(shape=(None, 3, 10), "
                "dtype=tf.float32, name=None), name='dense/BiasAdd:0', "
                "description=\"Symbolic value 0 from symbolic call 0 "
                "of layer 'dense'\")")
            expected_repr = (
                "<KerasTensor: shape=(None, 3, 10) dtype=float32 (Symbolic value 0 "
                "from symbolic call 0 of layer 'dense')>")
            self.assertEqual(expected_str, str(kt))
            self.assertEqual(expected_repr, repr(kt))

            kt = array_ops.reshape(kt, shape=(3, 5, 2))
            expected_str = (
                "KerasTensor(type_spec=TensorSpec(shape=(3, 5, 2), dtype=tf.float32, "
                "name=None), name='tf.reshape/Reshape:0', description=\"Symbolic "
                "value 0 from symbolic call 0 of layer 'tf.reshape'\")")
            expected_repr = (
                "<KerasTensor: shape=(3, 5, 2) dtype=float32 (Symbolic "
                "value 0 from symbolic call 0 of layer 'tf.reshape')>")
            self.assertEqual(expected_str, str(kt))
            self.assertEqual(expected_repr, repr(kt))

            kts = array_ops.unstack(kt)
            for i in range(3):
                expected_str = (
                    "KerasTensor(type_spec=TensorSpec(shape=(5, 2), dtype=tf.float32, "
                    "name=None), name='tf.unstack/unstack:%s', description=\"Symbolic "
                    "value %s from symbolic call 0 of layer 'tf.unstack'\")"
                ) % (i, i)
                expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 "
                                 "(Symbolic value %s from symbolic call 0 "
                                 "of layer 'tf.unstack')>") % i
                self.assertEqual(expected_str, str(kts[i]))
                self.assertEqual(expected_repr, repr(kts[i]))