Fix up tf.pad tests to be eager friendly
PiperOrigin-RevId: 324113822 Change-Id: I2b849ff51274995a8fc5206ccdeb95ca13c19cfc
This commit is contained in:
parent
a8213a7b94
commit
9465808ae3
@ -22,10 +22,11 @@ import numpy as np
|
||||
|
||||
from tensorflow.python.framework import constant_op
|
||||
from tensorflow.python.framework import dtypes
|
||||
from tensorflow.python.framework import errors
|
||||
from tensorflow.python.framework import ops
|
||||
from tensorflow.python.framework import test_util
|
||||
from tensorflow.python.ops import array_ops
|
||||
from tensorflow.python.ops import gradient_checker
|
||||
from tensorflow.python.ops import gradient_checker_v2
|
||||
from tensorflow.python.platform import test
|
||||
|
||||
|
||||
@ -86,7 +87,8 @@ class PadOpTest(test.TestCase):
|
||||
def _testPad(self, np_inputs, paddings, mode, constant_values):
|
||||
np_val = self._npPad(np_inputs, paddings, mode=mode,
|
||||
constant_values=constant_values)
|
||||
with self.cached_session(use_gpu=True):
|
||||
|
||||
with test_util.use_gpu():
|
||||
tf_val = array_ops.pad(np_inputs, paddings, mode=mode,
|
||||
constant_values=constant_values)
|
||||
out = self.evaluate(tf_val)
|
||||
@ -99,16 +101,17 @@ class PadOpTest(test.TestCase):
|
||||
mode,
|
||||
constant_values,
|
||||
paddings_dtype=dtypes.int32):
|
||||
with self.cached_session(use_gpu=True):
|
||||
inx = ops.convert_to_tensor(x)
|
||||
xs = list(x.shape)
|
||||
ina = ops.convert_to_tensor(a, paddings_dtype)
|
||||
y = array_ops.pad(inx, ina, mode=mode, constant_values=constant_values)
|
||||
# Expected y's shape to be:
|
||||
ys = list(np.array(x.shape) + np.sum(np.array(a), axis=1))
|
||||
jacob_t, jacob_n = gradient_checker.compute_gradient(
|
||||
inx, xs, y, ys, x_init_value=x)
|
||||
self.assertAllClose(jacob_t, jacob_n, rtol=1e-5, atol=1e-5)
|
||||
|
||||
def pad(x):
|
||||
return array_ops.pad(
|
||||
x,
|
||||
ops.convert_to_tensor(a, paddings_dtype),
|
||||
mode=mode,
|
||||
constant_values=constant_values)
|
||||
|
||||
with self.cached_session():
|
||||
jacob_t, jacob_n = gradient_checker_v2.compute_gradient(pad, [x])
|
||||
self.assertAllClose(jacob_t, jacob_n, rtol=1e-5, atol=1e-5)
|
||||
|
||||
def _testAll(self, np_inputs, paddings, constant_values):
|
||||
for mode in ("CONSTANT", "REFLECT", "SYMMETRIC", "reflect", "symmetric",
|
||||
@ -122,71 +125,76 @@ class PadOpTest(test.TestCase):
|
||||
self._testGradient(np_inputs, paddings, mode=mode,
|
||||
constant_values=constant_values)
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testInputDims(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaises(ValueError):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"Shape must be rank 1 but is rank 6|"
|
||||
"paddings must be the rank of inputs"):
|
||||
array_ops.pad(array_ops.reshape(
|
||||
[1, 2], shape=[1, 2, 1, 1, 1, 1]),
|
||||
array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsDim(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaises(ValueError):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"Shape must be rank 2 but is rank 1|"
|
||||
"paddings must be a matrix with 2 columns"):
|
||||
array_ops.pad(array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]),
|
||||
array_ops.reshape(
|
||||
[1, 2], shape=[2]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsDim2(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaises(ValueError):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"Dimension must be 2 but is 1|"
|
||||
"paddings must be a matrix with 2 columns"):
|
||||
array_ops.pad(array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]),
|
||||
array_ops.reshape(
|
||||
[1, 2], shape=[2, 1]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsDim3(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaises(ValueError):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"Shape must be rank 1 but is rank 2|"
|
||||
"paddings must be the rank of inputs"):
|
||||
array_ops.pad(array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]),
|
||||
array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsDim4(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaises(ValueError):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"Shape must be rank 3 but is rank 2|"
|
||||
"paddings must be the rank of inputs"):
|
||||
array_ops.pad(array_ops.reshape(
|
||||
[1, 2], shape=[1, 2]),
|
||||
array_ops.reshape(
|
||||
[1, 2, 3, 4, 5, 6], shape=[3, 2]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsNonNegative(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaisesRegex(ValueError, "must be non-negative"):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"must be non-negative"):
|
||||
array_ops.pad(constant_op.constant(
|
||||
[1], shape=[1]),
|
||||
constant_op.constant(
|
||||
[-1, 0], shape=[1, 2]))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPaddingsNonNegative2(self):
|
||||
with self.session(use_gpu=True):
|
||||
with self.assertRaisesRegex(ValueError, "must be non-negative"):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
|
||||
"must be non-negative"):
|
||||
array_ops.pad(constant_op.constant(
|
||||
[1], shape=[1]),
|
||||
constant_op.constant(
|
||||
[-1, 0], shape=[1, 2]))
|
||||
|
||||
def testPaddingsMaximum(self):
|
||||
with self.session(use_gpu=True):
|
||||
with test_util.use_gpu():
|
||||
with self.assertRaises(Exception):
|
||||
array_ops.pad(constant_op.constant(
|
||||
[1], shape=[2]),
|
||||
@ -204,7 +212,7 @@ class PadOpTest(test.TestCase):
|
||||
with self.cached_session():
|
||||
x = [[1, 2, 3], [4, 5, 6]]
|
||||
with self.assertRaisesRegex(ValueError, "Unknown padding mode"):
|
||||
array_ops.pad(x, [[1, 0], [2, 1]], mode="weird").eval()
|
||||
self.evaluate(array_ops.pad(x, [[1, 0], [2, 1]], mode="weird"))
|
||||
|
||||
def testPaddingTypes(self):
|
||||
paddings = [[1, 0], [2, 0]]
|
||||
@ -216,6 +224,7 @@ class PadOpTest(test.TestCase):
|
||||
paddings,
|
||||
mode=mode,
|
||||
constant_values=0)
|
||||
|
||||
with test_util.use_gpu():
|
||||
tf_val = array_ops.pad(
|
||||
inputs,
|
||||
@ -223,6 +232,7 @@ class PadOpTest(test.TestCase):
|
||||
mode=mode,
|
||||
constant_values=0)
|
||||
out = self.evaluate(tf_val)
|
||||
|
||||
self.assertAllEqual(np_val, out)
|
||||
self.assertShapeEqual(np_val, tf_val)
|
||||
|
||||
@ -246,7 +256,6 @@ class PadOpTest(test.TestCase):
|
||||
np.random.randint(-100, 100, (4, 2, 1, 3)).astype(t),
|
||||
[[0, 0], [0, 0], [0, 0], [0, 0]], -123)
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testFloatTypes(self):
|
||||
for t in [np.float32, np.float64]:
|
||||
self._testAll(np.random.rand(2, 5).astype(t), [[1, 0], [2, 0]], 0.0)
|
||||
@ -273,7 +282,7 @@ class PadOpTest(test.TestCase):
|
||||
constant_values="PAD")
|
||||
symmetric = array_ops.pad(x, [[1, 0], [0, 1]], mode="SYMMETRIC",
|
||||
constant_values="PAD")
|
||||
with self.session(use_gpu=True):
|
||||
with test_util.use_gpu():
|
||||
self.assertAllEqual(
|
||||
[[b"PAD", b"PAD", b"PAD"], [b"Hello", b"World", b"PAD"],
|
||||
[b"Goodnight", b"Moon", b"PAD"]], self.evaluate(constant))
|
||||
@ -285,73 +294,75 @@ class PadOpTest(test.TestCase):
|
||||
[[b"Hello", b"World", b"World"], [b"Hello", b"World", b"World"],
|
||||
[b"Goodnight", b"Moon", b"Moon"]], self.evaluate(symmetric))
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testShapeFunctionEdgeCases(self):
|
||||
# Unknown paddings shape.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4, 4, 4])
|
||||
padded = array_ops.pad(inp, array_ops.placeholder(dtypes.int32))
|
||||
self.assertEqual([None, None, None, None], padded.get_shape().as_list())
|
||||
# Shape function requires placeholders and a graph
|
||||
with ops.Graph().as_default():
|
||||
# Unknown paddings shape.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4, 4, 4])
|
||||
padded = array_ops.pad(inp, array_ops.placeholder(dtypes.int32))
|
||||
self.assertEqual([None, None, None, None], padded.get_shape().as_list())
|
||||
|
||||
# Unknown input shape.
|
||||
inp = array_ops.placeholder(dtypes.float32)
|
||||
padded = array_ops.pad(inp, [[2, 2], [2, 2]])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
# Unknown input shape.
|
||||
inp = array_ops.placeholder(dtypes.float32)
|
||||
padded = array_ops.pad(inp, [[2, 2], [2, 2]])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
|
||||
# Unknown input and paddings shape.
|
||||
inp = array_ops.placeholder(dtypes.float32)
|
||||
padded = array_ops.pad(inp, array_ops.placeholder(dtypes.int32))
|
||||
self.assertAllEqual(None, padded.get_shape().ndims)
|
||||
# Unknown input and paddings shape.
|
||||
inp = array_ops.placeholder(dtypes.float32)
|
||||
padded = array_ops.pad(inp, array_ops.placeholder(dtypes.int32))
|
||||
self.assertAllEqual(None, padded.get_shape().ndims)
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testPartialShapeInformation(self):
|
||||
unknown = array_ops.placeholder(dtypes.int32)
|
||||
# Partial shapes requires placeholders and a graph
|
||||
with ops.Graph().as_default():
|
||||
unknown = array_ops.placeholder(dtypes.int32)
|
||||
|
||||
# Known input shape, partial unknown padding (one dimension).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[1, 2], unknown])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
# Known input shape, partial unknown padding (one dimension).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[1, 2], unknown])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
|
||||
# Known input shape, partial unknown padding (begin).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[unknown, 0], [1, 2]])
|
||||
self.assertEqual([None, 7], padded.get_shape().as_list())
|
||||
# Known input shape, partial unknown padding (begin).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[unknown, 0], [1, 2]])
|
||||
self.assertEqual([None, 7], padded.get_shape().as_list())
|
||||
|
||||
# Known input shape, partial unknown padding (end).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[1, 2], [0, unknown]])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
# Known input shape, partial unknown padding (end).
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[1, 2], [0, unknown]])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
|
||||
# Unknown input shape, partial unknown padding (one dimension).
|
||||
padded = array_ops.pad(unknown, [[1, 2], unknown])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
# Unknown input shape, partial unknown padding (one dimension).
|
||||
padded = array_ops.pad(unknown, [[1, 2], unknown])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
|
||||
# Unknown input shape (rank known), partial unknown padding (one dimension).
|
||||
rank_known = array_ops.placeholder(dtypes.int32)
|
||||
rank_known.set_shape([None, None])
|
||||
padded = array_ops.pad(rank_known, [[1, 2], unknown])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
# Unknown input shape (rank known), partial unknown padding (one dim).
|
||||
rank_known = array_ops.placeholder(dtypes.int32)
|
||||
rank_known.set_shape([None, None])
|
||||
padded = array_ops.pad(rank_known, [[1, 2], unknown])
|
||||
self.assertEqual([None, None], padded.get_shape().as_list())
|
||||
|
||||
# Known input shape, partial unknown padding (begin), with constant begin.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp, [[constant_op.constant(1, shape=[]), 2],
|
||||
[0, unknown]])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
# Known input shape, partial unknown padding (begin), with constant begin.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(
|
||||
inp, [[constant_op.constant(1, shape=[]), 2], [0, unknown]])
|
||||
self.assertEqual([7, None], padded.get_shape().as_list())
|
||||
|
||||
# Known input shape, partial unknown padding (begin), with constant dim.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp,
|
||||
[constant_op.constant(1, shape=[2]), [0, unknown]])
|
||||
self.assertEqual([6, None], padded.get_shape().as_list())
|
||||
# Known input shape, partial unknown padding (begin), with constant dim.
|
||||
inp = constant_op.constant(0.0, shape=[4, 4])
|
||||
padded = array_ops.pad(inp,
|
||||
[constant_op.constant(1, shape=[2]), [0, unknown]])
|
||||
self.assertEqual([6, None], padded.get_shape().as_list())
|
||||
|
||||
# Zero padding on a known dimension.
|
||||
inp = array_ops.placeholder(dtypes.int32, [None, None, 20])
|
||||
padded = array_ops.pad(inp, [[0, 0], [0, unknown], [0, 0]])
|
||||
self.assertEqual([None, None, 20], padded.get_shape().as_list())
|
||||
# Zero padding on a known dimension.
|
||||
inp = array_ops.placeholder(dtypes.int32, [None, None, 20])
|
||||
padded = array_ops.pad(inp, [[0, 0], [0, unknown], [0, 0]])
|
||||
self.assertEqual([None, None, 20], padded.get_shape().as_list())
|
||||
|
||||
def testScalars(self):
|
||||
paddings = np.zeros((0, 2), dtype=np.int32)
|
||||
inp = np.asarray(7)
|
||||
with self.session(use_gpu=True):
|
||||
with test_util.use_gpu():
|
||||
tf_val = array_ops.pad(inp, paddings)
|
||||
out = self.evaluate(tf_val)
|
||||
self.assertAllEqual(inp, out)
|
||||
@ -367,7 +378,6 @@ class PadOpTest(test.TestCase):
|
||||
self.assertAllEqual(inp, out)
|
||||
self.assertShapeEqual(inp, tf_val)
|
||||
|
||||
@test_util.run_deprecated_v1
|
||||
def testCollapseAdjacentNonPaddedDimensions(self):
|
||||
# pyformat: disable
|
||||
paddings_values = [[[0, 0], [0, 0], [0, 0], [0, 1]],
|
||||
|
Loading…
Reference in New Issue
Block a user