def test_get_tensor_with_random_shape(self):
    x = test_utils.get_tensor_with_random_shape()
    self.assertIsInstance(x, tf.Tensor)
    self.assertFalse(x.shape.is_fully_defined())
    # Rank of the Tensor should be known, even though the dimension is not.
    self.assertEqual(1, x.shape.ndims)

    # Assert that unknown shape corresponds to a value of actually random shape
    # at execution time.
    samples = [self.evaluate(x) for _ in range(10)]
    self.assertGreater(len(set([len(s) for s in samples])), 1)

    # Test that source_fn has effect on the output values.
    x_uniform = test_utils.get_tensor_with_random_shape(
        expected_num_elements=50, source_fn=tf.random.uniform)
    x_normal = test_utils.get_tensor_with_random_shape(
        expected_num_elements=50, source_fn=tf.random.normal)
    self.assertGreaterEqual(self.evaluate(tf.reduce_min(x_uniform)), 0.0)
    self.assertLess(self.evaluate(tf.reduce_min(x_normal)), 0.0)
  def test_decode_needs_input_shape_dynamic(self):
    """Tests that mechanism for passing input shape works with dynamic shape."""
    encoder = simple_encoder.SimpleEncoder(
        core_encoder.EncoderComposer(
            test_utils.ReduceMeanEncodingStage()).make())

    x = test_utils.get_tensor_with_random_shape()
    encoded_x, decode_fn = encoder.encode(x)
    decoded_x = decode_fn(encoded_x)

    x, decoded_x = self.evaluate([x, decoded_x])
    self.assertAllEqual(x.shape, decoded_x.shape)
Пример #3
0
    def test_decode_needs_input_shape_unknown_input_shape(self):
        """Tests that encoder works with stages that need input shape for decode.

    This test chains two stages with this property, and provides an input with
    statically unknown shape information.
    """
        encoder = core_encoder.EncoderComposer(
            test_utils.ReduceMeanEncodingStage()).add_parent(
                test_utils.ReduceMeanEncodingStage(), RM_VALS).make()
        x = test_utils.get_tensor_with_random_shape()
        encode_params, decode_params = encoder.get_params(
            encoder.initial_state())
        encoded_x, _, input_shapes = encoder.encode(x, encode_params)
        decoded_x = encoder.decode(encoded_x, decode_params, input_shapes)
        assert x.shape.as_list(
        )[0] is None  # Validate the premise of the test.
        x, decoded_x = self.evaluate([x, decoded_x])

        # Assert shape is correctly recovered, and finctionality is as expected.
        self.assertAllEqual(x.shape, decoded_x.shape)
        self.assertAllClose([x.mean()] * len(x), decoded_x)
Пример #4
0
    def test_decode_needs_input_shape_dynamic(self):
        """Tests that mechanism for passing input shape works with dynamic shape."""
        if tf.executing_eagerly():
            fn = tf.function(test_utils.get_tensor_with_random_shape)
            tensorspec = tf.TensorSpec.from_tensor(
                fn.get_concrete_function().structured_outputs)
            x = fn()
        else:
            x = test_utils.get_tensor_with_random_shape()
            tensorspec = tf.TensorSpec.from_tensor(x)
        encoder = simple_encoder.SimpleEncoder(
            core_encoder.EncoderComposer(
                test_utils.ReduceMeanEncodingStage()).make(), tensorspec)

        # Validate the premise of the test - that encode mehtod expects an unknown
        # shape. This should be true both for graph and eager mode.
        assert (encoder._encode_fn.get_concrete_function().inputs[0].shape.
                as_list() == [None])

        state = encoder.initial_state()
        iteration = _make_iteration_function(encoder)
        x, _, decoded_x, _ = self.evaluate(iteration(x, state))
        self.assertAllEqual(x.shape, decoded_x.shape)
Пример #5
0
 def random_shape_2d_tensor():
   # Returns Tensor of shape [3, <unknown>]
   random_shape_vector = test_utils.get_tensor_with_random_shape()
   return tf.stack([random_shape_vector] * 3)
Пример #6
0
 def test_unknown_shape_raises(self):
   x = test_utils.get_tensor_with_random_shape()
   stage = self.default_encoding_stage()
   params, _ = stage.get_params()
   with self.assertRaisesRegexp(ValueError, 'fully known'):
     stage.encode(x, params)
Пример #7
0
 def get_random_shape_input():
   # Returns a Tensor of shape (?, 6)
   return tf.map_fn(lambda x: x * tf.random.normal([6]),
                    test_utils.get_tensor_with_random_shape())