Exemplo n.º 1
0
    def test_preprocess(self):
        """Tests that preprocess prepares the tensor as expected."""
        # Test preprocessing with input of dimension 1.
        n = 10
        x = tf.random.uniform((n, ), dtype=tf.float64)
        z, _, _ = ops.preprocess(x, axis=-1)
        self.assertEqual(z.shape.rank, 2)
        self.assertEqual(z.shape, (1, n))
        self.assertAllEqual(z[0], x)

        # Test preprocessing with input of dimension 2.
        x = tf.random.uniform((3, n), dtype=tf.float64)
        z, _, _ = ops.preprocess(x, axis=-1)
        self.assertEqual(z.shape.rank, 2)
        self.assertEqual(z.shape, x.shape)
        self.assertAllEqual(z, x)

        # Test preprocessing with input of dimension 2, preparing for axis 0
        x = tf.random.uniform((3, n), dtype=tf.float64)
        z, _, _ = ops.preprocess(x, axis=0)
        self.assertEqual(z.shape.rank, 2)
        self.assertEqual(z.shape, (x.shape[1], x.shape[0]))
        batch = 1
        self.assertAllEqual(z[batch], x[:, batch])

        # Test preprocessing with input of dimension > 2
        shape = [4, 21, 7, 10]
        x = tf.random.uniform(shape, dtype=tf.float64)
        axis = 2
        n = shape.pop(axis)
        z, _, _ = ops.preprocess(x, axis=axis)
        self.assertEqual(z.shape.rank, 2)
        self.assertEqual(z.shape, (np.prod(shape), n))
Exemplo n.º 2
0
 def test_postprocess(self):
     """Tests that postprocess is the inverse of preprocess."""
     shape = (4, 21, 7, 10)
     for i in range(1, len(shape)):
         x = tf.random.uniform(shape[:i])
         for axis in range(x.shape.rank):
             y, transp, s = ops.preprocess(x, axis)
             z = ops.postprocess(y, transp, s)
             self.assertAllEqual(x, z)