def _map_fn(i):
     return sparse_tensor.SparseTensorValue(indices=[[0, 0], [1, 1]],
                                            values=(i * [1, -1]),
                                            dense_shape=[2, 2])
Ejemplo n.º 2
0
 def _sparse(i):
     return sparse_tensor.SparseTensorValue(
         indices=array_ops.expand_dims(
             math_ops.range(i, dtype=dtypes.int64), 1),
         values=array_ops.fill([math_ops.cast(i, dtypes.int32)], i),
         dense_shape=[i])
Ejemplo n.º 3
0
class StructureTest(test_base.DatasetTestBase, parameterized.TestCase,
                    test_util.TensorFlowTestCase):

    # pylint: disable=g-long-lambda,protected-access
    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant(37.0), tensor_spec.TensorSpec,
         [dtypes.float32], [[]]),
        ("TensorArray", lambda: tensor_array_ops.TensorArray(
            dtype=dtypes.float32, element_shape=(3, ), size=0),
         tensor_array_ops.TensorArraySpec, [dtypes.variant], [[]]),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[3, 4]], values=[-1], dense_shape=[4, 5]),
         sparse_tensor.SparseTensorSpec, [dtypes.variant], [None]),
        ("RaggedTensor",
         lambda: ragged_factory_ops.constant([[1, 2], [], [4]]),
         ragged_tensor.RaggedTensorSpec, [dtypes.variant], [None]),
        ("Nested_0", lambda:
         (constant_op.constant(37.0), constant_op.constant([1, 2, 3])), tuple,
         [dtypes.float32, dtypes.int32], [[], [3]]),
        ("Nested_1", lambda: {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }, dict, [dtypes.float32, dtypes.int32], [[], [3]]),
        ("Nested_2", lambda: {
            "a":
            constant_op.constant(37.0),
            "b":
            (sparse_tensor.
             SparseTensor(indices=[[0, 0]], values=[1], dense_shape=[1, 1]),
             sparse_tensor.SparseTensor(
                 indices=[[3, 4]], values=[-1], dense_shape=[4, 5]))
        }, dict, [dtypes.float32, dtypes.variant, dtypes.variant], [[], None,
                                                                    None]),
    )
    def testFlatStructure(self, value_fn, expected_structure, expected_types,
                          expected_shapes):
        value = value_fn()
        s = structure.type_spec_from_value(value)
        self.assertIsInstance(s, expected_structure)
        flat_types = structure.get_flat_tensor_types(s)
        self.assertEqual(expected_types, flat_types)
        flat_shapes = structure.get_flat_tensor_shapes(s)
        self.assertLen(flat_shapes, len(expected_shapes))
        for expected, actual in zip(expected_shapes, flat_shapes):
            if expected is None:
                self.assertEqual(actual.ndims, None)
            else:
                self.assertEqual(actual.as_list(), expected)

    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant(37.0), lambda: [
            constant_op.constant(38.0),
            array_ops.placeholder(dtypes.float32),
            variables.Variable(100.0), 42.0,
            np.array(42.0, dtype=np.float32)
        ],
         lambda: [constant_op.constant([1.0, 2.0]),
                  constant_op.constant(37)]),
        ("TensorArray", lambda: tensor_array_ops.TensorArray(
            dtype=dtypes.float32, element_shape=(3, ), size=0), lambda: [
                tensor_array_ops.TensorArray(
                    dtype=dtypes.float32, element_shape=(3, ), size=0),
                tensor_array_ops.TensorArray(
                    dtype=dtypes.float32, element_shape=(3, ), size=10)
            ], lambda: [
                tensor_array_ops.TensorArray(
                    dtype=dtypes.int32, element_shape=(3, ), size=0),
                tensor_array_ops.TensorArray(
                    dtype=dtypes.float32, element_shape=(), size=0)
            ]),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[3, 4]], values=[-1], dense_shape=[4, 5]), lambda: [
                sparse_tensor.SparseTensor(indices=[[1, 1], [3, 4]],
                                           values=[10, -1],
                                           dense_shape=[4, 5]),
                sparse_tensor.SparseTensorValue(indices=[[1, 1], [3, 4]],
                                                values=[10, -1],
                                                dense_shape=[4, 5]),
                array_ops.sparse_placeholder(dtype=dtypes.int32),
                array_ops.sparse_placeholder(dtype=dtypes.int32,
                                             shape=[None, None])
            ], lambda: [
                constant_op.constant(37, shape=[4, 5]),
                sparse_tensor.SparseTensor(
                    indices=[[3, 4]], values=[-1], dense_shape=[5, 6]),
                array_ops.sparse_placeholder(dtype=dtypes.int32,
                                             shape=[None, None, None]),
                sparse_tensor.SparseTensor(
                    indices=[[3, 4]], values=[-1.0], dense_shape=[4, 5])
            ]),
        ("RaggedTensor",
         lambda: ragged_factory_ops.constant([[1, 2], [], [3]]), lambda: [
             ragged_factory_ops.constant([[1, 2], [3, 4], []]),
             ragged_factory_ops.constant([[1], [2, 3, 4], [5]]),
         ], lambda: [
             ragged_factory_ops.constant(1),
             ragged_factory_ops.constant([1, 2]),
             ragged_factory_ops.constant([[1], [2]]),
             ragged_factory_ops.constant([["a", "b"]]),
         ]),
        ("Nested", lambda: {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }, lambda: [{
            "a": constant_op.constant(15.0),
            "b": constant_op.constant([4, 5, 6])
        }], lambda: [{
            "a": constant_op.constant(15.0),
            "b": constant_op.constant([4, 5, 6, 7])
        }, {
            "a": constant_op.constant(15),
            "b": constant_op.constant([4, 5, 6])
        }, {
            "a":
            constant_op.constant(15),
            "b":
            sparse_tensor.SparseTensor(
                indices=[[0], [1], [2]], values=[4, 5, 6], dense_shape=[3])
        }, (constant_op.constant(15.0), constant_op.constant([4, 5, 6]))]),
    )
    @test_util.run_deprecated_v1
    def testIsCompatibleWithStructure(self, original_value_fn,
                                      compatible_values_fn,
                                      incompatible_values_fn):
        original_value = original_value_fn()
        compatible_values = compatible_values_fn()
        incompatible_values = incompatible_values_fn()
        s = structure.type_spec_from_value(original_value)
        for compatible_value in compatible_values:
            self.assertTrue(
                structure.are_compatible(
                    s, structure.type_spec_from_value(compatible_value)))
        for incompatible_value in incompatible_values:
            self.assertFalse(
                structure.are_compatible(
                    s, structure.type_spec_from_value(incompatible_value)))

    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant(37.0),
         lambda: constant_op.constant(42.0),
         lambda: constant_op.constant([5])),
        ("TensorArray", lambda: tensor_array_ops.TensorArray(
            dtype=dtypes.float32, element_shape=(3, ), size=0),
         lambda: tensor_array_ops.TensorArray(
             dtype=dtypes.float32, element_shape=(3, ), size=0),
         lambda: tensor_array_ops.TensorArray(
             dtype=dtypes.int32, element_shape=(), size=0)),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[3, 4]], values=[-1], dense_shape=[4, 5]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[1, 2]], values=[42], dense_shape=[4, 5]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[3]], values=[-1], dense_shape=[5]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[3, 4]], values=[1.0], dense_shape=[4, 5])),
        ("RaggedTensor",
         lambda: ragged_factory_ops.constant([[[1, 2]], [[3]]]),
         lambda: ragged_factory_ops.constant([[[5]], [[8], [3, 2]]]), lambda:
         ragged_factory_ops.constant([[[1]], [[2], [3]]], ragged_rank=1),
         lambda: ragged_factory_ops.constant([[[1.0, 2.0]], [[3.0]]]),
         lambda: ragged_factory_ops.constant([[[1]], [[2]], [[3]]])),
        ("Nested", lambda: {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }, lambda: {
            "a": constant_op.constant(42.0),
            "b": constant_op.constant([4, 5, 6])
        }, lambda: {
            "a": constant_op.constant([1, 2, 3]),
            "b": constant_op.constant(37.0)
        }),
    )  # pyformat: disable
    def testStructureFromValueEquality(self, value1_fn, value2_fn,
                                       *not_equal_value_fns):
        # pylint: disable=g-generic-assert
        s1 = structure.type_spec_from_value(value1_fn())
        s2 = structure.type_spec_from_value(value2_fn())
        self.assertEqual(s1, s1)  # check __eq__ operator.
        self.assertEqual(s1, s2)  # check __eq__ operator.
        self.assertFalse(s1 != s1)  # check __ne__ operator.
        self.assertFalse(s1 != s2)  # check __ne__ operator.
        for c1, c2 in zip(nest.flatten(s1), nest.flatten(s2)):
            self.assertEqual(hash(c1), hash(c1))
            self.assertEqual(hash(c1), hash(c2))
        for value_fn in not_equal_value_fns:
            s3 = structure.type_spec_from_value(value_fn())
            self.assertNotEqual(s1, s3)  # check __ne__ operator.
            self.assertNotEqual(s2, s3)  # check __ne__ operator.
            self.assertFalse(s1 == s3)  # check __eq_ operator.
            self.assertFalse(s2 == s3)  # check __eq_ operator.

    @parameterized.named_parameters(
        ("RaggedTensor_RaggedRank",
         ragged_tensor.RaggedTensorSpec(None, dtypes.int32, 1),
         ragged_tensor.RaggedTensorSpec(None, dtypes.int32, 2)),
        ("RaggedTensor_Shape",
         ragged_tensor.RaggedTensorSpec([3, None], dtypes.int32, 1),
         ragged_tensor.RaggedTensorSpec([5, None], dtypes.int32, 1)),
        ("RaggedTensor_DType",
         ragged_tensor.RaggedTensorSpec(None, dtypes.int32, 1),
         ragged_tensor.RaggedTensorSpec(None, dtypes.float32, 1)),
    )
    def testRaggedStructureInequality(self, s1, s2):
        # pylint: disable=g-generic-assert
        self.assertNotEqual(s1, s2)  # check __ne__ operator.
        self.assertFalse(s1 == s2)  # check __eq__ operator.

    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant(37.0),
         lambda: constant_op.constant(42.0),
         lambda: constant_op.constant([5])),
        ("TensorArray", lambda: tensor_array_ops.TensorArray(
            dtype=dtypes.float32, element_shape=(3, ), size=0),
         lambda: tensor_array_ops.TensorArray(
             dtype=dtypes.float32, element_shape=(3, ), size=0),
         lambda: tensor_array_ops.TensorArray(
             dtype=dtypes.int32, element_shape=(), size=0)),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[3, 4]], values=[-1], dense_shape=[4, 5]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[1, 2]], values=[42], dense_shape=[4, 5]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[3]], values=[-1], dense_shape=[5])),
        ("Nested", lambda: {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }, lambda: {
            "a": constant_op.constant(42.0),
            "b": constant_op.constant([4, 5, 6])
        }, lambda: {
            "a": constant_op.constant([1, 2, 3]),
            "b": constant_op.constant(37.0)
        }),
    )
    def testHash(self, value1_fn, value2_fn, value3_fn):
        s1 = structure.type_spec_from_value(value1_fn())
        s2 = structure.type_spec_from_value(value2_fn())
        s3 = structure.type_spec_from_value(value3_fn())
        for c1, c2, c3 in zip(nest.flatten(s1), nest.flatten(s2),
                              nest.flatten(s3)):
            self.assertEqual(hash(c1), hash(c1))
            self.assertEqual(hash(c1), hash(c2))
            self.assertNotEqual(hash(c1), hash(c3))
            self.assertNotEqual(hash(c2), hash(c3))

    @parameterized.named_parameters(
        (
            "Tensor",
            lambda: constant_op.constant(37.0),
        ),
        (
            "SparseTensor",
            lambda: sparse_tensor.SparseTensor(
                indices=[[3, 4]], values=[-1], dense_shape=[4, 5]),
        ),
        ("TensorArray", lambda: tensor_array_ops.TensorArray(
            dtype=dtypes.float32, element_shape=(), size=1).write(0, 7)),
        (
            "RaggedTensor",
            lambda: ragged_factory_ops.constant([[1, 2], [], [3]]),
        ),
        (
            "Nested_0",
            lambda: {
                "a": constant_op.constant(37.0),
                "b": constant_op.constant([1, 2, 3])
            },
        ),
        (
            "Nested_1",
            lambda: {
                "a":
                constant_op.constant(37.0),
                "b": (sparse_tensor.SparseTensor(
                    indices=[[0, 0]], values=[1], dense_shape=[1, 1]),
                      sparse_tensor.SparseTensor(
                          indices=[[3, 4]], values=[-1], dense_shape=[4, 5]))
            },
        ),
    )
    def testRoundTripConversion(self, value_fn):
        value = value_fn()
        s = structure.type_spec_from_value(value)

        def maybe_stack_ta(v):
            if isinstance(v, tensor_array_ops.TensorArray):
                return v.stack()
            else:
                return v

        before = self.evaluate(maybe_stack_ta(value))
        after = self.evaluate(
            maybe_stack_ta(
                structure.from_tensor_list(s,
                                           structure.to_tensor_list(s,
                                                                    value))))

        flat_before = nest.flatten(before)
        flat_after = nest.flatten(after)
        for b, a in zip(flat_before, flat_after):
            if isinstance(b, sparse_tensor.SparseTensorValue):
                self.assertAllEqual(b.indices, a.indices)
                self.assertAllEqual(b.values, a.values)
                self.assertAllEqual(b.dense_shape, a.dense_shape)
            elif isinstance(b, (ragged_tensor.RaggedTensor,
                                ragged_tensor_value.RaggedTensorValue)):
                self.assertAllEqual(b, a)
            else:
                self.assertAllEqual(b, a)

    # pylint: enable=g-long-lambda

    def preserveStaticShape(self):
        rt = ragged_factory_ops.constant([[1, 2], [], [3]])
        rt_s = structure.type_spec_from_value(rt)
        rt_after = structure.from_tensor_list(
            rt_s, structure.to_tensor_list(rt_s, rt))
        self.assertEqual(rt_after.row_splits.shape.as_list(),
                         rt.row_splits.shape.as_list())
        self.assertEqual(rt_after.values.shape.as_list(), [None])

        st = sparse_tensor.SparseTensor(indices=[[3, 4]],
                                        values=[-1],
                                        dense_shape=[4, 5])
        st_s = structure.type_spec_from_value(st)
        st_after = structure.from_tensor_list(
            st_s, structure.to_tensor_list(st_s, st))
        self.assertEqual(st_after.indices.shape.as_list(), [None, 2])
        self.assertEqual(st_after.values.shape.as_list(), [None])
        self.assertEqual(st_after.dense_shape.shape.as_list(),
                         st.dense_shape.shape.as_list())

    def testIncompatibleStructure(self):
        # Define three mutually incompatible values/structures, and assert that:
        # 1. Using one structure to flatten a value with an incompatible structure
        #    fails.
        # 2. Using one structure to restructre a flattened value with an
        #    incompatible structure fails.
        value_tensor = constant_op.constant(42.0)
        s_tensor = structure.type_spec_from_value(value_tensor)
        flat_tensor = structure.to_tensor_list(s_tensor, value_tensor)

        value_sparse_tensor = sparse_tensor.SparseTensor(indices=[[0, 0]],
                                                         values=[1],
                                                         dense_shape=[1, 1])
        s_sparse_tensor = structure.type_spec_from_value(value_sparse_tensor)
        flat_sparse_tensor = structure.to_tensor_list(s_sparse_tensor,
                                                      value_sparse_tensor)

        value_nest = {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }
        s_nest = structure.type_spec_from_value(value_nest)
        flat_nest = structure.to_tensor_list(s_nest, value_nest)

        with self.assertRaisesRegexp(
                ValueError,
                r"SparseTensor.* is not convertible to a tensor with "
                r"dtype.*float32.* and shape \(\)"):
            structure.to_tensor_list(s_tensor, value_sparse_tensor)
        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_tensor, value_nest)

        with self.assertRaisesRegexp(
                TypeError, "Neither a SparseTensor nor SparseTensorValue"):
            structure.to_tensor_list(s_sparse_tensor, value_tensor)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_sparse_tensor, value_nest)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_nest, value_tensor)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_nest, value_sparse_tensor)

        with self.assertRaisesRegexp(ValueError, r"Incompatible input:"):
            structure.from_tensor_list(s_tensor, flat_sparse_tensor)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 1 tensors but got 2."):
            structure.from_tensor_list(s_tensor, flat_nest)

        with self.assertRaisesRegexp(ValueError, "Incompatible input: "):
            structure.from_tensor_list(s_sparse_tensor, flat_tensor)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 1 tensors but got 2."):
            structure.from_tensor_list(s_sparse_tensor, flat_nest)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 2 tensors but got 1."):
            structure.from_tensor_list(s_nest, flat_tensor)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 2 tensors but got 1."):
            structure.from_tensor_list(s_nest, flat_sparse_tensor)

    def testIncompatibleNestedStructure(self):
        # Define three mutually incompatible nested values/structures, and assert
        # that:
        # 1. Using one structure to flatten a value with an incompatible structure
        #    fails.
        # 2. Using one structure to restructure a flattened value with an
        #    incompatible structure fails.

        value_0 = {
            "a": constant_op.constant(37.0),
            "b": constant_op.constant([1, 2, 3])
        }
        s_0 = structure.type_spec_from_value(value_0)
        flat_s_0 = structure.to_tensor_list(s_0, value_0)

        # `value_1` has compatible nested structure with `value_0`, but different
        # classes.
        value_1 = {
            "a":
            constant_op.constant(37.0),
            "b":
            sparse_tensor.SparseTensor(indices=[[0, 0]],
                                       values=[1],
                                       dense_shape=[1, 1])
        }
        s_1 = structure.type_spec_from_value(value_1)
        flat_s_1 = structure.to_tensor_list(s_1, value_1)

        # `value_2` has incompatible nested structure with `value_0` and `value_1`.
        value_2 = {
            "a":
            constant_op.constant(37.0),
            "b": (sparse_tensor.SparseTensor(indices=[[0, 0]],
                                             values=[1],
                                             dense_shape=[1, 1]),
                  sparse_tensor.SparseTensor(indices=[[3, 4]],
                                             values=[-1],
                                             dense_shape=[4, 5]))
        }
        s_2 = structure.type_spec_from_value(value_2)
        flat_s_2 = structure.to_tensor_list(s_2, value_2)

        with self.assertRaisesRegexp(
                ValueError,
                r"SparseTensor.* is not convertible to a tensor with "
                r"dtype.*int32.* and shape \(3,\)"):
            structure.to_tensor_list(s_0, value_1)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_0, value_2)

        with self.assertRaisesRegexp(
                TypeError, "Neither a SparseTensor nor SparseTensorValue"):
            structure.to_tensor_list(s_1, value_0)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_1, value_2)

        # NOTE(mrry): The repr of the dictionaries is not sorted, so the regexp
        # needs to account for "a" coming before or after "b". It might be worth
        # adding a deterministic repr for these error messages (among other
        # improvements).
        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_2, value_0)

        with self.assertRaisesRegexp(
                ValueError,
                "The two structures don't have the same nested structure."):
            structure.to_tensor_list(s_2, value_1)

        with self.assertRaisesRegexp(ValueError, r"Incompatible input:"):
            structure.from_tensor_list(s_0, flat_s_1)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 2 tensors but got 3."):
            structure.from_tensor_list(s_0, flat_s_2)

        with self.assertRaisesRegexp(ValueError, "Incompatible input: "):
            structure.from_tensor_list(s_1, flat_s_0)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 2 tensors but got 3."):
            structure.from_tensor_list(s_1, flat_s_2)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 3 tensors but got 2."):
            structure.from_tensor_list(s_2, flat_s_0)

        with self.assertRaisesRegexp(ValueError,
                                     "Expected 3 tensors but got 2."):
            structure.from_tensor_list(s_2, flat_s_1)

    @parameterized.named_parameters(
        ("Tensor", dtypes.float32, tensor_shape.scalar(), ops.Tensor,
         tensor_spec.TensorSpec([], dtypes.float32)),
        ("SparseTensor", dtypes.int32, tensor_shape.matrix(
            2, 2), sparse_tensor.SparseTensor,
         sparse_tensor.SparseTensorSpec([2, 2], dtypes.int32)),
        ("TensorArray_0", dtypes.int32,
         tensor_shape.as_shape([None, True, 2, 2
                                ]), tensor_array_ops.TensorArray,
         tensor_array_ops.TensorArraySpec(
             [2, 2], dtypes.int32, dynamic_size=None, infer_shape=True)),
        ("TensorArray_1", dtypes.int32,
         tensor_shape.as_shape([True, None, 2, 2
                                ]), tensor_array_ops.TensorArray,
         tensor_array_ops.TensorArraySpec(
             [2, 2], dtypes.int32, dynamic_size=True, infer_shape=None)),
        ("TensorArray_2", dtypes.int32,
         tensor_shape.as_shape([True, False, 2, 2
                                ]), tensor_array_ops.TensorArray,
         tensor_array_ops.TensorArraySpec(
             [2, 2], dtypes.int32, dynamic_size=True, infer_shape=False)),
        ("RaggedTensor", dtypes.int32, tensor_shape.matrix(2, None),
         ragged_tensor.RaggedTensorSpec([2, None], dtypes.int32, 1),
         ragged_tensor.RaggedTensorSpec([2, None], dtypes.int32, 1)),
        ("Nested", {
            "a": dtypes.float32,
            "b": (dtypes.int32, dtypes.string)
        }, {
            "a": tensor_shape.scalar(),
            "b": (tensor_shape.matrix(2, 2), tensor_shape.scalar())
        }, {
            "a": ops.Tensor,
            "b": (sparse_tensor.SparseTensor, ops.Tensor)
        }, {
            "a":
            tensor_spec.TensorSpec([], dtypes.float32),
            "b": (sparse_tensor.SparseTensorSpec([2, 2], dtypes.int32),
                  tensor_spec.TensorSpec([], dtypes.string))
        }),
    )
    def testConvertLegacyStructure(self, output_types, output_shapes,
                                   output_classes, expected_structure):
        actual_structure = structure.convert_legacy_structure(
            output_types, output_shapes, output_classes)
        self.assertEqual(actual_structure, expected_structure)

    def testNestedNestedStructure(self):
        s = (tensor_spec.TensorSpec([], dtypes.int64),
             (tensor_spec.TensorSpec([], dtypes.float32),
              tensor_spec.TensorSpec([], dtypes.string)))

        int64_t = constant_op.constant(37, dtype=dtypes.int64)
        float32_t = constant_op.constant(42.0)
        string_t = constant_op.constant("Foo")

        nested_tensors = (int64_t, (float32_t, string_t))

        tensor_list = structure.to_tensor_list(s, nested_tensors)
        for expected, actual in zip([int64_t, float32_t, string_t],
                                    tensor_list):
            self.assertIs(expected, actual)

        (actual_int64_t,
         (actual_float32_t,
          actual_string_t)) = structure.from_tensor_list(s, tensor_list)
        self.assertIs(int64_t, actual_int64_t)
        self.assertIs(float32_t, actual_float32_t)
        self.assertIs(string_t, actual_string_t)

        (actual_int64_t,
         (actual_float32_t,
          actual_string_t)) = (structure.from_compatible_tensor_list(
              s, tensor_list))
        self.assertIs(int64_t, actual_int64_t)
        self.assertIs(float32_t, actual_float32_t)
        self.assertIs(string_t, actual_string_t)

    @parameterized.named_parameters(
        ("Tensor", tensor_spec.TensorSpec([], dtypes.float32), 32,
         tensor_spec.TensorSpec([32], dtypes.float32)),
        ("TensorUnknown", tensor_spec.TensorSpec([], dtypes.float32), None,
         tensor_spec.TensorSpec([None], dtypes.float32)),
        ("SparseTensor", sparse_tensor.SparseTensorSpec([None],
                                                        dtypes.float32), 32,
         sparse_tensor.SparseTensorSpec([32, None], dtypes.float32)),
        ("SparseTensorUnknown",
         sparse_tensor.SparseTensorSpec([4], dtypes.float32), None,
         sparse_tensor.SparseTensorSpec([None, 4], dtypes.float32)),
        ("RaggedTensor",
         ragged_tensor.RaggedTensorSpec([2, None], dtypes.float32, 1), 32,
         ragged_tensor.RaggedTensorSpec([32, 2, None], dtypes.float32, 2)),
        ("RaggedTensorUnknown",
         ragged_tensor.RaggedTensorSpec([4, None], dtypes.float32, 1), None,
         ragged_tensor.RaggedTensorSpec([None, 4, None], dtypes.float32, 2)),
        ("Nested", {
            "a":
            tensor_spec.TensorSpec([], dtypes.float32),
            "b": (sparse_tensor.SparseTensorSpec([2, 2], dtypes.int32),
                  tensor_spec.TensorSpec([], dtypes.string))
        }, 128, {
            "a":
            tensor_spec.TensorSpec([128], dtypes.float32),
            "b": (sparse_tensor.SparseTensorSpec([128, 2, 2], dtypes.int32),
                  tensor_spec.TensorSpec([128], dtypes.string))
        }),
    )
    def testBatch(self, element_structure, batch_size,
                  expected_batched_structure):
        batched_structure = nest.map_structure(
            lambda component_spec: component_spec._batch(batch_size),
            element_structure)
        self.assertEqual(batched_structure, expected_batched_structure)

    @parameterized.named_parameters(
        ("Tensor", tensor_spec.TensorSpec(
            [32], dtypes.float32), tensor_spec.TensorSpec([], dtypes.float32)),
        ("TensorUnknown", tensor_spec.TensorSpec([None], dtypes.float32),
         tensor_spec.TensorSpec([], dtypes.float32)),
        ("SparseTensor",
         sparse_tensor.SparseTensorSpec([32, None], dtypes.float32),
         sparse_tensor.SparseTensorSpec([None], dtypes.float32)),
        ("SparseTensorUnknown",
         sparse_tensor.SparseTensorSpec([None, 4], dtypes.float32),
         sparse_tensor.SparseTensorSpec([4], dtypes.float32)),
        ("RaggedTensor",
         ragged_tensor.RaggedTensorSpec([32, None, None], dtypes.float32, 2),
         ragged_tensor.RaggedTensorSpec([None, None], dtypes.float32, 1)),
        ("RaggedTensorUnknown",
         ragged_tensor.RaggedTensorSpec([None, None, None], dtypes.float32, 2),
         ragged_tensor.RaggedTensorSpec([None, None], dtypes.float32, 1)),
        ("Nested", {
            "a":
            tensor_spec.TensorSpec([128], dtypes.float32),
            "b": (sparse_tensor.SparseTensorSpec([128, 2, 2], dtypes.int32),
                  tensor_spec.TensorSpec([None], dtypes.string))
        }, {
            "a":
            tensor_spec.TensorSpec([], dtypes.float32),
            "b": (sparse_tensor.SparseTensorSpec([2, 2], dtypes.int32),
                  tensor_spec.TensorSpec([], dtypes.string))
        }),
    )
    def testUnbatch(self, element_structure, expected_unbatched_structure):
        unbatched_structure = nest.map_structure(
            lambda component_spec: component_spec._unbatch(),
            element_structure)
        self.assertEqual(unbatched_structure, expected_unbatched_structure)

    # pylint: disable=g-long-lambda
    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant([[1.0, 2.0], [3.0, 4.0]]),
         lambda: constant_op.constant([1.0, 2.0])),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[0, 0], [1, 1]], values=[13, 27], dense_shape=[2, 2]),
         lambda: sparse_tensor.SparseTensor(
             indices=[[0]], values=[13], dense_shape=[2])),
        ("RaggedTensor", lambda: ragged_factory_ops.constant([[[1]], [[2]]]),
         lambda: ragged_factory_ops.constant([[1]])),
        ("Nest", lambda:
         (constant_op.constant([[1.0, 2.0], [3.0, 4.0]]),
          sparse_tensor.SparseTensor(
              indices=[[0, 0], [1, 1]], values=[13, 27], dense_shape=[2, 2])),
         lambda: (constant_op.constant([1.0, 2.0]),
                  sparse_tensor.SparseTensor(
                      indices=[[0]], values=[13], dense_shape=[2]))),
    )
    def testToBatchedTensorList(self, value_fn, element_0_fn):
        batched_value = value_fn()
        s = structure.type_spec_from_value(batched_value)
        batched_tensor_list = structure.to_batched_tensor_list(
            s, batched_value)

        # The batch dimension is 2 for all of the test cases.
        # NOTE(mrry): `tf.shape()` does not currently work for the DT_VARIANT
        # tensors in which we store sparse tensors.
        for t in batched_tensor_list:
            if t.dtype != dtypes.variant:
                self.assertEqual(2, self.evaluate(array_ops.shape(t)[0]))

        # Test that the 0th element from the unbatched tensor is equal to the
        # expected value.
        expected_element_0 = self.evaluate(element_0_fn())
        unbatched_s = nest.map_structure(
            lambda component_spec: component_spec._unbatch(), s)
        actual_element_0 = structure.from_tensor_list(
            unbatched_s, [t[0] for t in batched_tensor_list])

        for expected, actual in zip(nest.flatten(expected_element_0),
                                    nest.flatten(actual_element_0)):
            self.assertValuesEqual(expected, actual)
Ejemplo n.º 4
0
 def dense_to_sparse_scalar(tensor):
     indices = [[]]
     values = array_ops.expand_dims(tensor, 0)
     shape = []
     return sparse_tensor.SparseTensorValue(indices, values, shape)
Ejemplo n.º 5
0
    def _test_dense_features(self, trainable=True):
        # Inputs.
        vocabulary_size = 3
        sparse_input_a = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            indices=((0, 0), (1, 0), (1, 4)),
            values=(2, 0, 1),
            dense_shape=(2, 5))
        sparse_input_b = sparse_tensor.SparseTensorValue(
            # example 0, ids [0]
            # example 1, ids []
            indices=((0, 0), ),
            values=(0, ),
            dense_shape=(2, 5))
        sparse_input_c = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            indices=((0, 1), (1, 1), (1, 3)),
            values=(2, 0, 1),
            dense_shape=(2, 5))
        sparse_input_d = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids []
            indices=((0, 1), ),
            values=(2, ),
            dense_shape=(2, 5))

        # Embedding variable.
        embedding_dimension = 2
        embedding_values = (
            (1., 2.),  # id 0
            (3., 5.),  # id 1
            (7., 11.)  # id 2
        )

        def _initializer(shape, dtype, partition_info=None):
            self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
            self.assertEqual(dtypes.float32, dtype)
            self.assertIsNone(partition_info)
            return embedding_values

        # Expected lookup result, using combiner='mean'.
        expected_lookups = (
            # example 0:
            # A ids [2], embedding = [7, 11]
            # B ids [0], embedding = [1, 2]
            # C ids [2], embedding = [7, 11]
            # D ids [2], embedding = [7, 11]
            (7., 11., 1., 2., 7., 11., 7., 11.),
            # example 1:
            # A ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
            # B ids [], embedding = [0, 0]
            # C ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
            # D ids [], embedding = [0, 0]
            (2., 3.5, 0., 0., 2., 3.5, 0., 0.),
        )

        # Build columns.
        categorical_column_a = fc.categorical_column_with_identity(
            key='aaa', num_buckets=vocabulary_size)
        categorical_column_b = fc.categorical_column_with_identity(
            key='bbb', num_buckets=vocabulary_size)
        categorical_column_c = fc.categorical_column_with_identity(
            key='ccc', num_buckets=vocabulary_size)
        categorical_column_d = fc.categorical_column_with_identity(
            key='ddd', num_buckets=vocabulary_size)

        embedding_column_a, embedding_column_b = fc.shared_embedding_columns_v2(
            [categorical_column_a, categorical_column_b],
            dimension=embedding_dimension,
            initializer=_initializer,
            trainable=trainable)
        embedding_column_c, embedding_column_d = fc.shared_embedding_columns_v2(
            [categorical_column_c, categorical_column_d],
            dimension=embedding_dimension,
            initializer=_initializer,
            trainable=trainable)

        features = {
            'aaa': sparse_input_a,
            'bbb': sparse_input_b,
            'ccc': sparse_input_c,
            'ddd': sparse_input_d
        }

        # Provide sparse input and get dense result.
        dense_features = df.DenseFeatures(
            feature_columns=(embedding_column_b, embedding_column_a,
                             embedding_column_c, embedding_column_d))(features)

        # Assert expected embedding variable and lookups.
        global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
        self.assertCountEqual(
            ['aaa_bbb_shared_embedding:0', 'ccc_ddd_shared_embedding:0'],
            tuple([v.name for v in global_vars]))
        for v in global_vars:
            self.assertIsInstance(v, variables_lib.Variable)
        trainable_vars = ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)
        if trainable:
            self.assertCountEqual(
                ['aaa_bbb_shared_embedding:0', 'ccc_ddd_shared_embedding:0'],
                tuple([v.name for v in trainable_vars]))
        else:
            self.assertCountEqual([], tuple([v.name for v in trainable_vars]))
        shared_embedding_vars = global_vars

        self.evaluate(variables_lib.global_variables_initializer())
        self.evaluate(lookup_ops.tables_initializer())

        self.assertAllEqual(embedding_values,
                            self.evaluate(shared_embedding_vars[0]))
        self.assertAllEqual(expected_lookups, self.evaluate(dense_features))
Ejemplo n.º 6
0
 def make_tensor(i):
     return sparse_tensor.SparseTensorValue(indices=[[0, 0]],
                                            values=(i * [1]),
                                            dense_shape=[2, 2])
Ejemplo n.º 7
0
 def _sparse(i):
     return sparse_tensor.SparseTensorValue(
         indices=np.array([[0, 0]]),
         values=(i * np.array([1], dtype=np.int64)),
         dense_shape=np.array([1, 1]))
class StringSplitOpTest(test.TestCase, parameterized.TestCase):

  def testStringSplit(self):
    strings = ["pigs on the wing", "animals"]

    with self.cached_session():
      tokens = string_ops.string_split(strings)
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0]])
      self.assertAllEqual(values, [b"pigs", b"on", b"the", b"wing", b"animals"])
      self.assertAllEqual(shape, [2, 4])

  @test_util.run_deprecated_v1
  def testStringSplitEmptyDelimiter(self):
    strings = ["hello", "hola", b"\xF0\x9F\x98\x8E"]  # Last string is U+1F60E

    with self.cached_session():
      tokens = string_ops.string_split(strings, delimiter="")
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4],
                                    [1, 0], [1, 1], [1, 2], [1, 3], [2, 0],
                                    [2, 1], [2, 2], [2, 3]])
      expected = np.array(
          [
              "h", "e", "l", "l", "o", "h", "o", "l", "a", b"\xf0", b"\x9f",
              b"\x98", b"\x8e"
          ],
          dtype="|S1")
      self.assertAllEqual(values.tolist(), expected)
      self.assertAllEqual(shape, [3, 5])

  def testStringSplitEmptyToken(self):
    strings = ["", " a", "b ", " c", " ", " d ", "  e", "f  ", "  g  ", "  "]

    with self.cached_session():
      tokens = string_ops.string_split(strings)
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(
          indices,
          [[1, 0], [2, 0], [3, 0], [5, 0], [6, 0], [7, 0], [8, 0]])
      self.assertAllEqual(values, [b"a", b"b", b"c", b"d", b"e", b"f", b"g"])
      self.assertAllEqual(shape, [10, 1])

  def testStringSplitOnSetEmptyToken(self):
    strings = ["", " a", "b ", " c", " ", " d ", ". e", "f .", " .g. ", " ."]

    with self.cached_session():
      tokens = string_ops.string_split(strings, delimiter=" .")
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(
          indices,
          [[1, 0], [2, 0], [3, 0], [5, 0], [6, 0], [7, 0], [8, 0]])
      self.assertAllEqual(values, [b"a", b"b", b"c", b"d", b"e", b"f", b"g"])
      self.assertAllEqual(shape, [10, 1])

  @test_util.run_deprecated_v1
  def testStringSplitWithDelimiter(self):
    strings = ["hello|world", "hello world"]

    with self.cached_session():
      self.assertRaises(
          ValueError, string_ops.string_split, strings, delimiter=["|", ""])

      self.assertRaises(
          ValueError, string_ops.string_split, strings, delimiter=["a"])

      tokens = string_ops.string_split(strings, delimiter="|")
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(indices, [[0, 0], [0, 1], [1, 0]])
      self.assertAllEqual(values, [b"hello", b"world", b"hello world"])
      self.assertAllEqual(shape, [2, 2])

      tokens = string_ops.string_split(strings, delimiter="| ")
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(indices, [[0, 0], [0, 1], [1, 0], [1, 1]])
      self.assertAllEqual(values, [b"hello", b"world", b"hello", b"world"])
      self.assertAllEqual(shape, [2, 2])

  @test_util.run_deprecated_v1
  def testStringSplitWithDelimiterTensor(self):
    strings = ["hello|world", "hello world"]

    with self.cached_session() as sess:
      delimiter = array_ops.placeholder(dtypes.string)

      tokens = string_ops.string_split(strings, delimiter=delimiter)

      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(tokens, feed_dict={delimiter: ["a", "b"]})
      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(tokens, feed_dict={delimiter: ["a"]})
      indices, values, shape = sess.run(tokens, feed_dict={delimiter: "|"})

      self.assertAllEqual(indices, [[0, 0], [0, 1], [1, 0]])
      self.assertAllEqual(values, [b"hello", b"world", b"hello world"])
      self.assertAllEqual(shape, [2, 2])

  @test_util.run_deprecated_v1
  def testStringSplitWithDelimitersTensor(self):
    strings = ["hello.cruel,world", "hello cruel world"]

    with self.cached_session() as sess:
      delimiter = array_ops.placeholder(dtypes.string)

      tokens = string_ops.string_split(strings, delimiter=delimiter)

      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(tokens, feed_dict={delimiter: ["a", "b"]})
      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(tokens, feed_dict={delimiter: ["a"]})
      indices, values, shape = sess.run(tokens, feed_dict={delimiter: ".,"})

      self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [1, 0]])
      self.assertAllEqual(values,
                          [b"hello", b"cruel", b"world", b"hello cruel world"])
      self.assertAllEqual(shape, [2, 3])

  def testStringSplitWithNoSkipEmpty(self):
    strings = ["#a", "b#", "#c#"]

    with self.cached_session():
      tokens = string_ops.string_split(strings, "#", skip_empty=False)
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(indices, [[0, 0], [0, 1],
                                    [1, 0], [1, 1],
                                    [2, 0], [2, 1], [2, 2]])
      self.assertAllEqual(values, [b"", b"a", b"b", b"", b"", b"c", b""])
      self.assertAllEqual(shape, [3, 3])

    with self.cached_session():
      tokens = string_ops.string_split(strings, "#")
      indices, values, shape = self.evaluate(tokens)
      self.assertAllEqual(values, [b"a", b"b", b"c"])
      self.assertAllEqual(indices, [[0, 0], [1, 0], [2, 0]])
      self.assertAllEqual(shape, [3, 1])

  @parameterized.named_parameters([
      dict(
          testcase_name="RaggedResultType",
          source=[b"pigs on the wing", b"animals"],
          result_type="RaggedTensor",
          expected=[[b"pigs", b"on", b"the", b"wing"], [b"animals"]]),
      dict(
          testcase_name="SparseResultType",
          source=[b"pigs on the wing", b"animals"],
          result_type="SparseTensor",
          expected=sparse_tensor.SparseTensorValue(
              [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0]],
              [b"pigs", b"on", b"the", b"wing", b"animals"], [2, 4])),
      dict(
          testcase_name="DefaultResultType",
          source=[b"pigs on the wing", b"animals"],
          expected=sparse_tensor.SparseTensorValue(
              [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0]],
              [b"pigs", b"on", b"the", b"wing", b"animals"], [2, 4])),
      dict(
          testcase_name="BadResultType",
          source=[b"pigs on the wing", b"animals"],
          result_type="BouncyTensor",
          error="result_type must be .*"),
      dict(
          testcase_name="WithSepAndAndSkipEmpty",
          source=[b"+hello+++this+is+a+test"],
          sep="+",
          skip_empty=False,
          result_type="RaggedTensor",
          expected=[[b"", b"hello", b"", b"", b"this", b"is", b"a", b"test"]]),
      dict(
          testcase_name="WithDelimiter",
          source=[b"hello world"],
          delimiter="l",
          result_type="RaggedTensor",
          expected=[[b"he", b"o wor", b"d"]]),
  ])
  def testRaggedStringSplitWrapper(self,
                                   source,
                                   sep=None,
                                   skip_empty=True,
                                   delimiter=None,
                                   result_type="SparseTensor",
                                   expected=None,
                                   error=None):
    if error is not None:
      with self.assertRaisesRegex(ValueError, error):
        ragged_string_ops.string_split(source, sep, skip_empty, delimiter,
                                       result_type)
    if expected is not None:
      result = ragged_string_ops.string_split(source, sep, skip_empty,
                                              delimiter, result_type)
      if isinstance(expected, sparse_tensor.SparseTensorValue):
        self.assertAllEqual(result.indices, expected.indices)
        self.assertAllEqual(result.values, expected.values)
        self.assertAllEqual(result.dense_shape, expected.dense_shape)
      else:
        self.assertAllEqual(result, expected)
Ejemplo n.º 9
0
    def test_feature_layer_cpu(self):
        # Inputs.
        vocabulary_size = 3
        input_a = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            indices=((0, 0), (1, 0), (1, 1)),
            values=(2, 0, 1),
            dense_shape=(2, 2))
        input_b = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            # example 2, ids []
            indices=((0, 0), (1, 0), (1, 1)),
            values=(2, 0, 1),
            dense_shape=(3, 2))
        input_features = {'aaa': input_a, 'bbb': input_b}

        # Embedding variable.
        embedding_dimension = 2
        embedding_values = (
            (1., 2.),  # id 0
            (3., 5.),  # id 1
            (7., 11.)  # id 2
        )

        def _initializer(shape, dtype, partition_info):
            self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
            self.assertEqual(dtypes.float32, dtype)
            self.assertIsNone(partition_info)
            return embedding_values

        # Expected lookup result, using combiner='mean'.
        expected_lookups_a = (
            # example 0:
            (7., 11.),  # ids [2], embedding = [7, 11]
            # example 1:
            (2., 3.5
             ),  # ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
        )
        expected_lookups_b = (
            # example 0:
            (
                (7., 11.),
                (0., 0.),
            ),  # ids [2], embedding = [[7, 11], [0, 0]]
            # example 1:
            (
                (1., 2.),
                (3., 5.),
            ),  # ids [0, 1], embedding = [[1, 2], [3, 5]]
            # example 2:
            (
                (0., 0.),
                (0., 0.),
            ),  # ids [], embedding = [[0, 0], [0, 0]]
        )

        # Build columns.
        categorical_column_a = fc_lib.categorical_column_with_identity(
            key='aaa', num_buckets=vocabulary_size)
        categorical_column_b = fc_lib.sequence_categorical_column_with_identity(
            key='bbb', num_buckets=vocabulary_size)
        embedding_column_a, embedding_column_b = tpu_fc.shared_embedding_columns_v2(
            [categorical_column_a, categorical_column_b],
            dimension=embedding_dimension,
            initializer=_initializer,
            max_sequence_lengths=[0, 2])

        # Provide sparse input and get dense result.
        dense_features = fc_lib.DenseFeatures([embedding_column_a])
        sequence_features = fc_lib.SequenceFeatures([embedding_column_b])
        embedding_lookup_a = dense_features(input_features)
        embedding_lookup_b = sequence_features(input_features)

        # Assert expected embedding variable and lookups.
        global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
        self.assertItemsEqual(('aaa_bbb_shared_embedding:0', ),
                              tuple([v.name for v in global_vars]))
        embedding_var = global_vars[0]
        with _initialized_session():
            self.assertAllEqual(embedding_values, embedding_var.eval())
            self.assertAllEqual(expected_lookups_a, embedding_lookup_a.eval())
            self.assertAllEqual(expected_lookups_b,
                                embedding_lookup_b[0].eval())
Ejemplo n.º 10
0
def _make_sparse_tensor(indices, values, dense_shape, dtype=np.int32):
    return sparse_tensor.SparseTensorValue(np.array(indices, np.int64),
                                           np.array(values, dtype),
                                           np.array(dense_shape, np.int64))
Ejemplo n.º 11
0
class OptionalTest(test_base.DatasetTestBase, parameterized.TestCase):
    def testFromValue(self):
        opt = optional_ops.Optional.from_value(constant_op.constant(37.0))
        self.assertTrue(self.evaluate(opt.has_value()))
        self.assertEqual(37.0, self.evaluate(opt.get_value()))

    def testFromStructuredValue(self):
        opt = optional_ops.Optional.from_value({
            "a":
            constant_op.constant(37.0),
            "b": (constant_op.constant(["Foo"]), constant_op.constant("Bar"))
        })
        self.assertTrue(self.evaluate(opt.has_value()))
        self.assertEqual({
            "a": 37.0,
            "b": ([b"Foo"], b"Bar")
        }, self.evaluate(opt.get_value()))

    def testFromSparseTensor(self):
        st_0 = sparse_tensor.SparseTensorValue(indices=np.array([[0]]),
                                               values=np.array([0],
                                                               dtype=np.int64),
                                               dense_shape=np.array([1]))
        st_1 = sparse_tensor.SparseTensorValue(
            indices=np.array([[0, 0], [1, 1]]),
            values=np.array([-1., 1.], dtype=np.float32),
            dense_shape=np.array([2, 2]))
        opt = optional_ops.Optional.from_value((st_0, st_1))
        self.assertTrue(self.evaluate(opt.has_value()))
        val_0, val_1 = opt.get_value()
        for expected, actual in [(st_0, val_0), (st_1, val_1)]:
            self.assertAllEqual(expected.indices,
                                self.evaluate(actual.indices))
            self.assertAllEqual(expected.values, self.evaluate(actual.values))
            self.assertAllEqual(expected.dense_shape,
                                self.evaluate(actual.dense_shape))

    def testFromNone(self):
        value_structure = structure.TensorStructure(dtypes.float32, [])
        opt = optional_ops.Optional.none_from_structure(value_structure)
        self.assertTrue(
            opt.value_structure.is_compatible_with(value_structure))
        self.assertFalse(
            opt.value_structure.is_compatible_with(
                structure.TensorStructure(dtypes.float32, [1])))
        self.assertFalse(
            opt.value_structure.is_compatible_with(
                structure.TensorStructure(dtypes.int32, [])))
        self.assertFalse(self.evaluate(opt.has_value()))
        with self.assertRaises(errors.InvalidArgumentError):
            self.evaluate(opt.get_value())

    def testAddN(self):
        devices = ["/cpu:0"]
        if test_util.is_gpu_available():
            devices.append("/gpu:0")
        for device in devices:
            with ops.device(device):
                # With value
                opt1 = optional_ops.Optional.from_value((1.0, 2.0))
                opt2 = optional_ops.Optional.from_value((3.0, 4.0))

                add_tensor = math_ops.add_n(
                    [opt1._variant_tensor, opt2._variant_tensor])
                add_opt = optional_ops._OptionalImpl(add_tensor,
                                                     opt1.value_structure)
                self.assertAllEqual(self.evaluate(add_opt.get_value()),
                                    (4.0, 6.0))

                # Without value
                opt_none1 = optional_ops.Optional.none_from_structure(
                    opt1.value_structure)
                opt_none2 = optional_ops.Optional.none_from_structure(
                    opt2.value_structure)
                add_tensor = math_ops.add_n(
                    [opt_none1._variant_tensor, opt_none2._variant_tensor])
                add_opt = optional_ops._OptionalImpl(add_tensor,
                                                     opt_none1.value_structure)
                self.assertFalse(self.evaluate(add_opt.has_value()))

    def testNestedAddN(self):
        devices = ["/cpu:0"]
        if test_util.is_gpu_available():
            devices.append("/gpu:0")
        for device in devices:
            with ops.device(device):
                opt1 = optional_ops.Optional.from_value([1, 2.0])
                opt2 = optional_ops.Optional.from_value([3, 4.0])
                opt3 = optional_ops.Optional.from_value(
                    (5.0, opt1._variant_tensor))
                opt4 = optional_ops.Optional.from_value(
                    (6.0, opt2._variant_tensor))

                add_tensor = math_ops.add_n(
                    [opt3._variant_tensor, opt4._variant_tensor])
                add_opt = optional_ops._OptionalImpl(add_tensor,
                                                     opt3.value_structure)
                self.assertEqual(self.evaluate(add_opt.get_value()[0]), 11.0)

                inner_add_opt = optional_ops._OptionalImpl(
                    add_opt.get_value()[1], opt1.value_structure)
                self.assertAllEqual(inner_add_opt.get_value(), [4, 6.0])

    def testZerosLike(self):
        devices = ["/cpu:0"]
        if test_util.is_gpu_available():
            devices.append("/gpu:0")
        for device in devices:
            with ops.device(device):
                # With value
                opt = optional_ops.Optional.from_value((1.0, 2.0))
                zeros_tensor = array_ops.zeros_like(opt._variant_tensor)
                zeros_opt = optional_ops._OptionalImpl(zeros_tensor,
                                                       opt.value_structure)
                self.assertAllEqual(self.evaluate(zeros_opt.get_value()),
                                    (0.0, 0.0))

                # Without value
                opt_none = optional_ops.Optional.none_from_structure(
                    opt.value_structure)
                zeros_tensor = array_ops.zeros_like(opt_none._variant_tensor)
                zeros_opt = optional_ops._OptionalImpl(
                    zeros_tensor, opt_none.value_structure)
                self.assertFalse(self.evaluate(zeros_opt.has_value()))

    def testNestedZerosLike(self):
        devices = ["/cpu:0"]
        if test_util.is_gpu_available():
            devices.append("/gpu:0")
        for device in devices:
            with ops.device(device):
                opt1 = optional_ops.Optional.from_value(1.0)
                opt2 = optional_ops.Optional.from_value(opt1._variant_tensor)

                zeros_tensor = array_ops.zeros_like(opt2._variant_tensor)
                zeros_opt = optional_ops._OptionalImpl(zeros_tensor,
                                                       opt2.value_structure)
                inner_zeros_opt = optional_ops._OptionalImpl(
                    zeros_opt.get_value(), opt1.value_structure)
                self.assertEqual(self.evaluate(inner_zeros_opt.get_value()),
                                 0.0)

    def testCopyToGPU(self):
        if not test_util.is_gpu_available():
            self.skipTest("No GPU available")

        with ops.device("/cpu:0"):
            optional_with_value = optional_ops.Optional.from_value(
                (constant_op.constant(37.0), constant_op.constant("Foo"),
                 constant_op.constant(42)))
            optional_none = optional_ops.Optional.none_from_structure(
                structure.TensorStructure(dtypes.float32, []))

        with ops.device("/gpu:0"):
            gpu_optional_with_value = optional_ops._OptionalImpl(
                array_ops.identity(optional_with_value._variant_tensor),
                optional_with_value.value_structure)
            gpu_optional_none = optional_ops._OptionalImpl(
                array_ops.identity(optional_none._variant_tensor),
                optional_none.value_structure)

            gpu_optional_with_value_has_value = gpu_optional_with_value.has_value(
            )
            gpu_optional_with_value_values = gpu_optional_with_value.get_value(
            )

            gpu_optional_none_has_value = gpu_optional_none.has_value()

        self.assertTrue(self.evaluate(gpu_optional_with_value_has_value))
        self.assertEqual((37.0, b"Foo", 42),
                         self.evaluate(gpu_optional_with_value_values))
        self.assertFalse(self.evaluate(gpu_optional_none_has_value))

    def testNestedCopyToGPU(self):
        if not test_util.is_gpu_available():
            self.skipTest("No GPU available")

        with ops.device("/cpu:0"):
            optional_with_value = optional_ops.Optional.from_value(
                (constant_op.constant(37.0), constant_op.constant("Foo"),
                 constant_op.constant(42)))
            optional_none = optional_ops.Optional.none_from_structure(
                structure.TensorStructure(dtypes.float32, []))
            nested_optional = optional_ops.Optional.from_value(
                (optional_with_value._variant_tensor,
                 optional_none._variant_tensor, 1.0))

        with ops.device("/gpu:0"):
            gpu_nested_optional = optional_ops._OptionalImpl(
                array_ops.identity(nested_optional._variant_tensor),
                nested_optional.value_structure)

            gpu_nested_optional_has_value = gpu_nested_optional.has_value()
            gpu_nested_optional_values = gpu_nested_optional.get_value()

        self.assertTrue(self.evaluate(gpu_nested_optional_has_value))

        inner_with_value = optional_ops._OptionalImpl(
            gpu_nested_optional_values[0], optional_with_value.value_structure)

        inner_none = optional_ops._OptionalImpl(gpu_nested_optional_values[1],
                                                optional_none.value_structure)

        self.assertEqual((37.0, b"Foo", 42),
                         self.evaluate(inner_with_value.get_value()))
        self.assertFalse(self.evaluate(inner_none.has_value()))
        self.assertEqual(1.0, self.evaluate(gpu_nested_optional_values[2]))

    def _assertElementValueEqual(self, expected, actual):
        if isinstance(expected, dict):
            self.assertItemsEqual(list(expected.keys()), list(actual.keys()))
            for k in expected.keys():
                self._assertElementValueEqual(expected[k], actual[k])
        elif isinstance(expected, sparse_tensor.SparseTensorValue):
            self.assertAllEqual(expected.indices, actual.indices)
            self.assertAllEqual(expected.values, actual.values)
            self.assertAllEqual(expected.dense_shape, actual.dense_shape)
        else:
            self.assertAllEqual(expected, actual)

    # pylint: disable=g-long-lambda
    @parameterized.named_parameters(
        ("Tensor", lambda: constant_op.constant(37.0),
         structure.TensorStructure(dtypes.float32, [])),
        ("SparseTensor", lambda: sparse_tensor.SparseTensor(
            indices=[[0, 1]],
            values=constant_op.constant([0], dtype=dtypes.int32),
            dense_shape=[10, 10]),
         structure.SparseTensorStructure(dtypes.int32, [10, 10])),
        ("Nest", lambda: {
            "a": constant_op.constant(37.0),
            "b": (constant_op.constant(["Foo"]), constant_op.constant("Bar"))
        },
         structure.NestedStructure({
             "a":
             structure.TensorStructure(dtypes.float32, []),
             "b": (structure.TensorStructure(dtypes.string, [1]),
                   structure.TensorStructure(dtypes.string, []))
         })),
        ("Optional", lambda: optional_ops.Optional.from_value(37.0),
         optional_ops.OptionalStructure(
             structure.TensorStructure(dtypes.float32, []))),
    )
    def testOptionalStructure(self, tf_value_fn, expected_value_structure):
        tf_value = tf_value_fn()
        opt = optional_ops.Optional.from_value(tf_value)

        self.assertTrue(
            expected_value_structure.is_compatible_with(opt.value_structure))
        self.assertTrue(
            opt.value_structure.is_compatible_with(expected_value_structure))

        opt_structure = structure.Structure.from_value(opt)
        self.assertIsInstance(opt_structure, optional_ops.OptionalStructure)
        self.assertTrue(opt_structure.is_compatible_with(opt_structure))
        self.assertTrue(
            opt_structure._value_structure.is_compatible_with(
                expected_value_structure))
        self.assertEqual([dtypes.variant], opt_structure._flat_types)
        self.assertEqual([tensor_shape.scalar()], opt_structure._flat_shapes)

        # All OptionalStructure objects are not compatible with a non-optional
        # value.
        non_optional_structure = structure.Structure.from_value(
            constant_op.constant(42.0))
        self.assertFalse(
            opt_structure.is_compatible_with(non_optional_structure))

        # Assert that the optional survives a round-trip via _from_tensor_list()
        # and _to_tensor_list().
        round_trip_opt = opt_structure._from_tensor_list(
            opt_structure._to_tensor_list(opt))
        if isinstance(tf_value, optional_ops.Optional):
            self._assertElementValueEqual(
                self.evaluate(tf_value.get_value()),
                self.evaluate(round_trip_opt.get_value().get_value()))
        else:
            self._assertElementValueEqual(
                self.evaluate(tf_value),
                self.evaluate(round_trip_opt.get_value()))

    @parameterized.named_parameters(
        ("Tensor", np.array([1, 2, 3], dtype=np.int32),
         lambda: constant_op.constant([4, 5, 6], dtype=dtypes.int32), True),
        ("SparseTensor",
         sparse_tensor.SparseTensorValue(indices=[[0, 0], [1, 1]],
                                         values=np.array([-1., 1.],
                                                         dtype=np.float32),
                                         dense_shape=[2, 2]),
         lambda: sparse_tensor.SparseTensor(indices=[[0, 1], [1, 0]],
                                            values=[37.0, 42.0],
                                            dense_shape=[2, 2]), False),
        ("Nest", {
            "a":
            np.array([1, 2, 3], dtype=np.int32),
            "b":
            sparse_tensor.SparseTensorValue(indices=[[0, 0], [1, 1]],
                                            values=np.array([-1., 1.],
                                                            dtype=np.float32),
                                            dense_shape=[2, 2])
        }, lambda: {
            "a":
            constant_op.constant([4, 5, 6], dtype=dtypes.int32),
            "b":
            sparse_tensor.SparseTensor(indices=[[0, 1], [1, 0]],
                                       values=[37.0, 42.0],
                                       dense_shape=[2, 2])
        }, False),
    )
    def testIteratorGetNextAsOptional(self, np_value, tf_value_fn,
                                      works_on_gpu):
        if not works_on_gpu and test.is_gpu_available():
            self.skipTest("Test case not yet supported on GPU.")
        ds = dataset_ops.Dataset.from_tensors(np_value).repeat(3)

        if context.executing_eagerly():
            iterator = dataset_ops.make_one_shot_iterator(ds)
            # For each element of the dataset, assert that the optional evaluates to
            # the expected value.
            for _ in range(3):
                next_elem = iterator_ops.get_next_as_optional(iterator)
                self.assertIsInstance(next_elem, optional_ops.Optional)
                self.assertTrue(
                    next_elem.value_structure.is_compatible_with(
                        structure.Structure.from_value(tf_value_fn())))
                self.assertTrue(next_elem.has_value())
                self._assertElementValueEqual(np_value, next_elem.get_value())
            # After exhausting the iterator, `next_elem.has_value()` will evaluate to
            # false, and attempting to get the value will fail.
            for _ in range(2):
                next_elem = iterator_ops.get_next_as_optional(iterator)
                self.assertFalse(self.evaluate(next_elem.has_value()))
                with self.assertRaises(errors.InvalidArgumentError):
                    self.evaluate(next_elem.get_value())
        else:
            iterator = dataset_ops.make_initializable_iterator(ds)
            next_elem = iterator_ops.get_next_as_optional(iterator)
            self.assertIsInstance(next_elem, optional_ops.Optional)
            self.assertTrue(
                next_elem.value_structure.is_compatible_with(
                    structure.Structure.from_value(tf_value_fn())))
            # Before initializing the iterator, evaluating the optional fails with
            # a FailedPreconditionError. This is only relevant in graph mode.
            elem_has_value_t = next_elem.has_value()
            elem_value_t = next_elem.get_value()
            with self.assertRaises(errors.FailedPreconditionError):
                self.evaluate(elem_has_value_t)
            with self.assertRaises(errors.FailedPreconditionError):
                self.evaluate(elem_value_t)
            # Now we initialize the iterator.
            self.evaluate(iterator.initializer)
            # For each element of the dataset, assert that the optional evaluates to
            # the expected value.
            for _ in range(3):
                elem_has_value, elem_value = self.evaluate(
                    [elem_has_value_t, elem_value_t])
                self.assertTrue(elem_has_value)
                self._assertElementValueEqual(np_value, elem_value)

            # After exhausting the iterator, `next_elem.has_value()` will evaluate to
            # false, and attempting to get the value will fail.
            for _ in range(2):
                self.assertFalse(self.evaluate(elem_has_value_t))
                with self.assertRaises(errors.InvalidArgumentError):
                    self.evaluate(elem_value_t)

    def testFunctionBoundaries(self):
        @def_function.function
        def get_optional():
            x = constant_op.constant(1.0)
            opt = optional_ops.Optional.from_value(x)
            # TODO(skyewm): support returning Optionals from functions?
            return opt._variant_tensor

        # TODO(skyewm): support Optional arguments?
        @def_function.function
        def consume_optional(opt_tensor):
            value_structure = structure.TensorStructure(dtypes.float32, [])
            opt = optional_ops._OptionalImpl(opt_tensor, value_structure)
            return opt.get_value()

        opt_tensor = get_optional()
        val = consume_optional(opt_tensor)
        self.assertEqual(self.evaluate(val), 1.0)

    def testLimitedRetracing(self):
        trace_count = [0]

        @def_function.function
        def f(opt):
            trace_count[0] += 1
            return opt.get_value()

        opt1 = optional_ops.Optional.from_value(constant_op.constant(37.0))
        opt2 = optional_ops.Optional.from_value(constant_op.constant(42.0))

        for _ in range(10):
            self.assertEqual(self.evaluate(f(opt1)), 37.0)
            self.assertEqual(self.evaluate(f(opt2)), 42.0)
            self.assertEqual(trace_count[0], 1)
Ejemplo n.º 12
0
  def testFromTensorSlicesMixed(self):
    """Test a dataset that represents the slices from a tuple of tensors."""
    components = (np.tile(np.array([[1], [2], [3]]), 20),
                  np.tile(np.array([[12], [13], [14]]), 22),
                  np.array([37.0, 38.0, 39.0]),
                  sparse_tensor.SparseTensorValue(
                      indices=np.array([[0, 0], [1, 0], [2, 0]]),
                      values=np.array([0, 0, 0]),
                      dense_shape=np.array([3, 1])),
                  sparse_tensor.SparseTensorValue(
                      indices=np.array([[0, 0], [1, 1], [2, 2]]),
                      values=np.array([1, 2, 3]),
                      dense_shape=np.array([3, 3])))

    iterator = (
        dataset_ops.Dataset.from_tensor_slices(components)
        .make_initializable_iterator())
    init_op = iterator.initializer
    get_next = iterator.get_next()

    self.assertEqual([
        tensor_shape.TensorShape(c.dense_shape[1:])
        if sparse_tensor.is_sparse(c) else c.shape[1:] for c in components
    ], [shape for shape in iterator.output_shapes])

    with self.cached_session() as sess:
      self.evaluate(init_op)
      expected = [
          (sparse_tensor.SparseTensorValue(
              indices=np.array([[0]]),
              values=np.array([0]),
              dense_shape=np.array([1])),
           sparse_tensor.SparseTensorValue(
               indices=np.array([[0]]),
               values=np.array([1]),
               dense_shape=np.array([3]))),
          (sparse_tensor.SparseTensorValue(
              indices=np.array([[0]]),
              values=np.array([0]),
              dense_shape=np.array([1])),
           sparse_tensor.SparseTensorValue(
               indices=np.array([[1]]),
               values=np.array([2]),
               dense_shape=np.array([3]))),
          (sparse_tensor.SparseTensorValue(
              indices=np.array([[0]]),
              values=np.array([0]),
              dense_shape=np.array([1])),
           sparse_tensor.SparseTensorValue(
               indices=np.array([[2]]),
               values=np.array([3]),
               dense_shape=np.array([3]))),
      ]
      for i in range(3):
        results = self.evaluate(get_next)
        for component, result_component in zip(
            (list(zip(*components[:3]))[i] + expected[i]), results):
          if sparse_tensor.is_sparse(component):
            self.assertSparseValuesEqual(component, result_component)
          else:
            self.assertAllEqual(component, result_component)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)
Ejemplo n.º 13
0
    def test_empty_row(self):
        # Inputs.
        vocabulary_size = 3
        input_sparse_tensor = sparse_tensor.SparseTensorValue(
            # example 0, ids []
            # example 1, ids [0, 1, 3]
            indices=((1, 0), (1, 1), (1, 4)),
            values=(0, 1, 3),
            dense_shape=(2, 5))
        input_features = {'inp': input_sparse_tensor}

        # Embedding variable.
        embedding_dimension = 2
        embedding_values = (
            (1., 2.),  # id 0
            (3., 5.),  # id 1
            (7., 11.),  # id 2
            (13., 17.)  # id 3
        )

        def _initializer(shape, dtype, partition_info=None):
            self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
            self.assertEqual(dtypes.float32, dtype)
            self.assertIsNone(partition_info)
            return embedding_values

        # Build columns.
        categorical_column_input = fc_lib.categorical_column_with_identity(
            key='inp', num_buckets=vocabulary_size)

        # Set tensor_core_shape to be [None, 20] to ensure some padding and
        # dynamic batch size.
        embedding_column = tpu_fc.embedding_column_v2(
            categorical_column_input,
            dimension=embedding_dimension,
            initializer=_initializer,
            combiner='mean',
            embedding_lookup_device='tpu_tensor_core',
            tensor_core_shape=[None, 3])

        # Run in TPUContexts so that we hit the intended densification case.
        context = tpu._TPUInferenceContext('tpu_inference')
        context.Enter()
        with tpu_function.tpu_shard_context(1):
            dense_features = fc_lib.DenseFeatures(embedding_column)
            expected_lookups = (
                # example 0:
                (0., 0.),  # ids [], embedding = [0, 0]
                # example 1:
                (2., 3.5
                 ),  # ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
            )

            embedding_lookup = dense_features(input_features)

            # Assert expected embedding variable and lookups.
            global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
            self.assertCountEqual(
                ('dense_features/inp_embedding/embedding_weights:0', ),
                tuple([v.name for v in global_vars]))

            embedding_var = global_vars[0]
            with _initialized_session():
                self.assertAllEqual(embedding_values, embedding_var)
                eval_res = embedding_lookup.eval()
                self.assertAllEqual(expected_lookups, eval_res)
            context.Exit()
Ejemplo n.º 14
0
 def dense_to_sparse_non_scalar(tensor):
     indices = array_ops.where(
         array_ops.ones_like(tensor, dtype=dtypes.bool))
     values = array_ops.gather_nd(tensor, indices)
     shape = array_ops.shape(tensor, out_type=dtypes.int64)
     return sparse_tensor.SparseTensorValue(indices, values, shape)
 def make_sparse_fn(i):
   return sparse_tensor.SparseTensorValue(
       indices=np.array([[0, 0]]),
       values=(i * np.array([1])),
       dense_shape=np.array([1, 1]))
Ejemplo n.º 16
0
    def test_feature_layer_cpu(self):
        # Inputs.
        vocabulary_size = 3
        sparse_input = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            # example 2, ids []
            # example 3, ids [1]
            indices=((0, 0), (1, 0), (1, 1), (3, 0)),
            values=(2, 0, 1, 1),
            dense_shape=(4, 2))

        # Embedding variable.
        embedding_dimension = 2
        embedding_values = (
            (1., 2.),  # id 0
            (3., 5.),  # id 1
            (7., 11.)  # id 2
        )

        def _initializer(shape, dtype, partition_info):
            self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
            self.assertEqual(dtypes.float32, dtype)
            self.assertIsNone(partition_info)
            return embedding_values

        # Expected lookup result, using combiner='mean'.
        expected_lookups = (
            # example 0, ids [2], embedding = [7, 11]
            (7., 11.),
            # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
            (2., 3.5),
            # example 2, ids [], embedding = [0, 0]
            (0., 0.),
            # example 3, ids [1], embedding = [3, 5]
            (3., 5.),
        )
        expected_lookups_sequence = (
            # example 0, ids [2], embedding = [[7, 11], [0, 0]]
            (
                (7., 11.),
                (0., 0.),
            ),
            # example 1, ids [0, 1], embedding = [[1, 2], [3. 5]]
            (
                (1., 2.),
                (3., 5.),
            ),
            # example 2, ids [], embedding = [0, 0]
            (
                (0., 0.),
                (0., 0.),
            ),
            # example 3, ids [1], embedding = [3, 5]
            (
                (3., 5.),
                (0., 0.),
            ),
        )

        # Build columns.
        categorical_column = fc_lib.categorical_column_with_identity(
            key='aaa', num_buckets=vocabulary_size)
        sequence_categorical_column = (
            fc_lib.sequence_categorical_column_with_identity(
                key='bbb', num_buckets=vocabulary_size))
        embedding_column = tpu_fc.embedding_column_v2(
            categorical_column,
            dimension=embedding_dimension,
            initializer=_initializer)
        sequence_embedding_column = tpu_fc.embedding_column_v2(
            sequence_categorical_column,
            dimension=embedding_dimension,
            initializer=_initializer,
            max_sequence_length=2)

        # Provide sparse input and get dense result.
        features = {'aaa': sparse_input, 'bbb': sparse_input}
        dense_features = fc_lib.DenseFeatures([embedding_column])
        sequence_features = fc_lib.SequenceFeatures(
            [sequence_embedding_column])
        embedding_lookup = dense_features(features)
        sequence_embedding_lookup = sequence_features(features)

        # Assert expected embedding variable and lookups.
        global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
        self.assertItemsEqual((
            'dense_features/aaa_embedding/embedding_weights:0',
            'sequence_features/bbb_embedding/embedding_weights:0',
        ), tuple([v.name for v in global_vars]))
        with _initialized_session():
            self.assertAllEqual(embedding_values, global_vars[0].eval())
            self.assertAllEqual(expected_lookups, embedding_lookup.eval())
            self.assertAllEqual(expected_lookups_sequence,
                                sequence_embedding_lookup[0].eval())
Ejemplo n.º 17
0
 def _SparseTensorValue_2x3x4(self):
     ind = np.array([[0, 0, 1], [0, 1, 0], [0, 1, 2], [1, 0, 3], [1, 1, 1],
                     [1, 1, 3], [1, 2, 2]])
     val = np.array([1, 10, 12, 103, 111, 113, 122])
     shape = np.array([2, 3, 4])
     return sparse_tensor.SparseTensorValue(ind, val, shape)
 def _SparseTensorValue_1x1x1(self):
     ind = np.array([[0, 0, 0]]).astype(np.int64)
     val = np.array([0]).astype(np.int32)
     shape = np.array([3, 4, 5]).astype(np.int64)
     return sparse_tensor_lib.SparseTensorValue(ind, val, shape)
Ejemplo n.º 19
0
 def _SparseTensorValue_2x5x6(self):
   return sparse_tensor.SparseTensorValue(self._IND_2_5_6, self._VAL_2_5_6,
                                          self._SHP_2_5_6)
Ejemplo n.º 20
0
    def test_dense_features_not_trainable(self):
        # Inputs.
        vocabulary_size = 3
        sparse_input = sparse_tensor.SparseTensorValue(
            # example 0, ids [2]
            # example 1, ids [0, 1]
            # example 2, ids []
            # example 3, ids [1]
            indices=((0, 0), (1, 0), (1, 4), (3, 0)),
            values=(2, 0, 1, 1),
            dense_shape=(4, 5))

        # Embedding variable.
        embedding_dimension = 2
        embedding_values = (
            (1., 2.),  # id 0
            (3., 5.),  # id 1
            (7., 11.)  # id 2
        )

        def _initializer(shape, dtype, partition_info=None):
            self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
            self.assertEqual(dtypes.float32, dtype)
            self.assertIsNone(partition_info)
            return embedding_values

        # Expected lookup result, using combiner='mean'.
        expected_lookups = (
            # example 0, ids [2], embedding = [7, 11]
            (7., 11.),
            # example 1, ids [0, 1], embedding = mean([1, 2] + [3, 5]) = [2, 3.5]
            (2., 3.5),
            # example 2, ids [], embedding = [0, 0]
            (0., 0.),
            # example 3, ids [1], embedding = [3, 5]
            (3., 5.),
        )

        # Build columns.
        categorical_column = fc.categorical_column_with_identity(
            key='aaa', num_buckets=vocabulary_size)
        embedding_column = fc.embedding_column(categorical_column,
                                               dimension=embedding_dimension,
                                               initializer=_initializer,
                                               trainable=False)

        # Provide sparse input and get dense result.
        dense_features = df.DenseFeatures((embedding_column, ))({
            'aaa':
            sparse_input
        })

        # Assert expected embedding variable and lookups.
        global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
        self.assertCountEqual(
            ('dense_features/aaa_embedding/embedding_weights:0', ),
            tuple([v.name for v in global_vars]))
        self.assertCountEqual([],
                              ops.get_collection(
                                  ops.GraphKeys.TRAINABLE_VARIABLES))

        self.evaluate(variables_lib.global_variables_initializer())
        self.evaluate(lookup_ops.tables_initializer())

        self.assertAllEqual(embedding_values, self.evaluate(global_vars[0]))
        self.assertAllEqual(expected_lookups, self.evaluate(dense_features))
Ejemplo n.º 21
0
 def _sparse(i):
   return sparse_tensor.SparseTensorValue(
       indices=[[0]], values=(i * [1]), dense_shape=[1])
  def test_embedding_column(self):
    vocabulary_size = 3
    sparse_input_a = sparse_tensor.SparseTensorValue(
        # example 0, ids [2]
        # example 1, ids [0, 1]
        indices=((0, 0), (1, 0), (1, 1)),
        values=(2, 0, 1),
        dense_shape=(2, 2))
    sparse_input_b = sparse_tensor.SparseTensorValue(
        # example 0, ids [1]
        # example 1, ids [2, 0]
        indices=((0, 0), (1, 0), (1, 1)),
        values=(1, 2, 0),
        dense_shape=(2, 2))

    embedding_dimension_a = 2
    embedding_values_a = (
        (1., 2.),  # id 0
        (3., 4.),  # id 1
        (5., 6.)  # id 2
    )
    embedding_dimension_b = 3
    embedding_values_b = (
        (11., 12., 13.),  # id 0
        (14., 15., 16.),  # id 1
        (17., 18., 19.)  # id 2
    )
    def _get_initializer(embedding_dimension, embedding_values):
      def _initializer(shape, dtype, partition_info):
        self.assertAllEqual((vocabulary_size, embedding_dimension), shape)
        self.assertEqual(dtypes.float32, dtype)
        self.assertIsNone(partition_info)
        return embedding_values
      return _initializer

    expected_input_layer = [
        # example 0, ids_a [2], ids_b [1]
        [[5., 6., 14., 15., 16.], [0., 0., 0., 0., 0.]],
        # example 1, ids_a [0, 1], ids_b [2, 0]
        [[1., 2., 17., 18., 19.], [3., 4., 11., 12., 13.]],
    ]
    expected_sequence_length = [1, 2]

    categorical_column_a = sfc.sequence_categorical_column_with_identity(
        key='aaa', num_buckets=vocabulary_size)
    embedding_column_a = sfc._sequence_embedding_column(
        categorical_column_a, dimension=embedding_dimension_a,
        initializer=_get_initializer(embedding_dimension_a, embedding_values_a))
    categorical_column_b = sfc.sequence_categorical_column_with_identity(
        key='bbb', num_buckets=vocabulary_size)
    embedding_column_b = sfc._sequence_embedding_column(
        categorical_column_b, dimension=embedding_dimension_b,
        initializer=_get_initializer(embedding_dimension_b, embedding_values_b))

    input_layer, sequence_length = sfc.sequence_input_layer(
        features={
            'aaa': sparse_input_a,
            'bbb': sparse_input_b,
        },
        # Test that columns are reordered alphabetically.
        feature_columns=[embedding_column_b, embedding_column_a])

    global_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
    self.assertItemsEqual(
        ('sequence_input_layer/aaa_embedding/embedding_weights:0',
         'sequence_input_layer/bbb_embedding/embedding_weights:0'),
        tuple([v.name for v in global_vars]))
    with monitored_session.MonitoredSession() as sess:
      self.assertAllEqual(embedding_values_a, global_vars[0].eval(session=sess))
      self.assertAllEqual(embedding_values_b, global_vars[1].eval(session=sess))
      self.assertAllEqual(expected_input_layer, input_layer.eval(session=sess))
      self.assertAllEqual(
          expected_sequence_length, sequence_length.eval(session=sess))