def testPadTensorToBatchSize(self): with self.session(): # Cannot pad a 0-dimensional Tensor. tensor_0d = tf.constant(1) with self.assertRaises(ValueError): dataset_ops.pad_tensor_to_batch_size(tensor_0d, 10) # 1-dimensional Tensor. Un-padded batch size is 5. tensor_1d = tf.range(5, dtype=tf.int32) self.assertEqual([5], tensor_1d.shape) self.assertAllEqual([0, 1, 2, 3, 4], tensor_1d.eval()) tensor_1d_pad5 = dataset_ops.pad_tensor_to_batch_size(tensor_1d, 5) self.assertEqual([5], tensor_1d_pad5.shape) self.assertAllEqual([0, 1, 2, 3, 4], tensor_1d_pad5.eval()) tensor_1d_pad8 = dataset_ops.pad_tensor_to_batch_size(tensor_1d, 8) self.assertEqual([8], tensor_1d_pad8.shape) self.assertAllEqual([0, 1, 2, 3, 4, 0, 0, 0], tensor_1d_pad8.eval()) # 2-dimensional Tensor. Un-padded batch size is 3. tensor_2d = tf.reshape(tf.range(9, dtype=tf.int32), [3, 3]) self.assertEqual([3, 3], tensor_2d.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8]], tensor_2d.eval()) tensor_2d_pad3 = dataset_ops.pad_tensor_to_batch_size(tensor_2d, 3) self.assertEqual([3, 3], tensor_2d_pad3.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8]], tensor_2d_pad3.eval()) tensor_2d_pad4 = dataset_ops.pad_tensor_to_batch_size(tensor_2d, 4) self.assertEqual([4, 3], tensor_2d_pad4.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 0, 0]], tensor_2d_pad4.eval())
def testPadTensorToBatchSize(self): with self.test_session(): # Cannot pad a 0-dimensional Tensor. tensor_0d = tf.constant(1) with self.assertRaises(ValueError): dataset_ops.pad_tensor_to_batch_size(tensor_0d, 10) # 1-dimensional Tensor. Un-padded batch size is 5. tensor_1d = tf.range(5, dtype=tf.int32) self.assertEqual([5], tensor_1d.shape) self.assertAllEqual([0, 1, 2, 3, 4], tensor_1d.eval()) tensor_1d_pad5 = dataset_ops.pad_tensor_to_batch_size(tensor_1d, 5) self.assertEqual([5], tensor_1d_pad5.shape) self.assertAllEqual([0, 1, 2, 3, 4], tensor_1d_pad5.eval()) tensor_1d_pad8 = dataset_ops.pad_tensor_to_batch_size(tensor_1d, 8) self.assertEqual([8], tensor_1d_pad8.shape) self.assertAllEqual([0, 1, 2, 3, 4, 0, 0, 0], tensor_1d_pad8.eval()) # 2-dimensional Tensor. Un-padded batch size is 3. tensor_2d = tf.reshape(tf.range(9, dtype=tf.int32), [3, 3]) self.assertEqual([3, 3], tensor_2d.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8]], tensor_2d.eval()) tensor_2d_pad3 = dataset_ops.pad_tensor_to_batch_size(tensor_2d, 3) self.assertEqual([3, 3], tensor_2d_pad3.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8]], tensor_2d_pad3.eval()) tensor_2d_pad4 = dataset_ops.pad_tensor_to_batch_size(tensor_2d, 4) self.assertEqual([4, 3], tensor_2d_pad4.shape) self.assertAllEqual([[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 0, 0]], tensor_2d_pad4.eval())