Check raised regex in convolutional tests (#19202)
* Strengthen the tests by checking regex * Check regex in conv_transpose tests * Check regex in depthwise conv tests * Check regex in separable conv tests * Reformatting * Fix linting * Fix formatting
This commit is contained in:
parent
ce7cdcef0d
commit
22a4ea757b
@ -132,7 +132,7 @@ class BaseDepthwiseConv(Layer):
|
||||
if self.depth_multiplier is not None and self.depth_multiplier <= 0:
|
||||
raise ValueError(
|
||||
"Invalid value for argument `depth_multiplier`. Expected a "
|
||||
"strictly positive value. Received "
|
||||
"strictly positive value. Received "
|
||||
f"depth_multiplier={self.depth_multiplier}."
|
||||
)
|
||||
|
||||
|
@ -135,7 +135,7 @@ class BaseSeparableConv(Layer):
|
||||
if self.depth_multiplier is not None and self.depth_multiplier <= 0:
|
||||
raise ValueError(
|
||||
"Invalid value for argument `depth_multiplier`. Expected a "
|
||||
"strictly positive value. Received "
|
||||
"strictly positive value. Received "
|
||||
f"depth_multiplier={self.depth_multiplier}."
|
||||
)
|
||||
|
||||
|
@ -486,19 +486,38 @@ class ConvBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_bad_init_args(self):
|
||||
# `filters` is not positive.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"Invalid value for argument `filters`. Expected a "
|
||||
"strictly positive value. Received filters=0.",
|
||||
):
|
||||
layers.Conv1D(filters=0, kernel_size=1)
|
||||
|
||||
# `kernel_size` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `kernel_size` argument must be a tuple of \d+ "
|
||||
r"integers. Received kernel_size=\(1, 0\), including values \{0\} "
|
||||
r"that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.Conv2D(filters=2, kernel_size=(1, 0))
|
||||
|
||||
# `strides` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `strides` argument must be a tuple of \d+ "
|
||||
r"integers. Received strides=\(1, 0\), including values \{0\} that "
|
||||
r"do not satisfy `value > 0`",
|
||||
):
|
||||
layers.Conv2D(filters=2, kernel_size=(2, 2), strides=(1, 0))
|
||||
|
||||
# `dilation_rate > 1` while `strides > 1`.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"`strides > 1` not supported in conjunction with "
|
||||
r"`dilation_rate > 1`. Received: strides=\(2, 2\) and "
|
||||
r"dilation_rate=\(2, 1\)",
|
||||
):
|
||||
layers.Conv2D(
|
||||
filters=2, kernel_size=(2, 2), strides=2, dilation_rate=(2, 1)
|
||||
)
|
||||
@ -512,7 +531,11 @@ class ConvBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
layers.Conv2D(filters=5, kernel_size=(2, 2), groups=0)
|
||||
|
||||
# `filters` cannot be divided by `groups`.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"The number of filters must be evenly divisible by the"
|
||||
" number of groups. Received: groups=2, filters=5.",
|
||||
):
|
||||
layers.Conv2D(filters=5, kernel_size=(2, 2), groups=2)
|
||||
|
||||
|
||||
|
@ -507,21 +507,40 @@ class ConvTransposeBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_bad_init_args(self):
|
||||
# `filters` is not positive.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"Invalid value for argument `filters`. Expected a "
|
||||
"strictly positive value. Received filters=0.",
|
||||
):
|
||||
layers.Conv1DTranspose(filters=0, kernel_size=1)
|
||||
|
||||
# `kernel_size` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `kernel_size` argument must be a tuple of "
|
||||
r"\d+ integers. Received kernel_size=\(1, 0\), including values"
|
||||
r" \{0\} that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.Conv2DTranspose(filters=2, kernel_size=(1, 0))
|
||||
|
||||
# `strides` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `strides` argument must be a tuple of \d+ "
|
||||
r"integers. Received strides=\(1, 0\), including values \{0\} "
|
||||
r"that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.Conv2DTranspose(
|
||||
filters=2, kernel_size=(2, 2), strides=(1, 0)
|
||||
)
|
||||
|
||||
# `dilation_rate > 1` while `strides > 1`.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"`strides > 1` not supported in conjunction with "
|
||||
r"`dilation_rate > 1`. Received: strides=\(2, 2\) and "
|
||||
r"dilation_rate=\(2, 1\)",
|
||||
):
|
||||
layers.Conv2DTranspose(
|
||||
filters=2, kernel_size=(2, 2), strides=2, dilation_rate=(2, 1)
|
||||
)
|
||||
|
@ -293,21 +293,41 @@ class DepthwiseConvBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_bad_init_args(self):
|
||||
# `depth_multiplier` is not positive.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"Invalid value for argument `depth_multiplier`. "
|
||||
"Expected a strictly positive value. Received "
|
||||
"depth_multiplier=0.",
|
||||
):
|
||||
layers.DepthwiseConv1D(depth_multiplier=0, kernel_size=1)
|
||||
|
||||
# `kernel_size` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `kernel_size` argument must be a tuple of 2 "
|
||||
r"integers. Received kernel_size=\(1, 0\), including values "
|
||||
r"\{0\} that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.DepthwiseConv2D(depth_multiplier=2, kernel_size=(1, 0))
|
||||
|
||||
# `strides` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `strides` argument must be a tuple of \d+ "
|
||||
r"integers. Received strides=\(1, 0\), including values \{0\} "
|
||||
r"that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.DepthwiseConv2D(
|
||||
depth_multiplier=2, kernel_size=(2, 2), strides=(1, 0)
|
||||
)
|
||||
|
||||
# `dilation_rate > 1` while `strides > 1`.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"`strides > 1` not supported in conjunction with "
|
||||
r"`dilation_rate > 1`. Received: strides=\(2, 2\) and "
|
||||
r"dilation_rate=\(2, 1\)",
|
||||
):
|
||||
layers.DepthwiseConv2D(
|
||||
depth_multiplier=2,
|
||||
kernel_size=(2, 2),
|
||||
|
@ -147,21 +147,40 @@ class SeparableConvBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_bad_init_args(self):
|
||||
# `depth_multiplier` is not positive.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"Invalid value for argument `depth_multiplier`. "
|
||||
"Expected a strictly positive value. Received "
|
||||
"depth_multiplier=0.",
|
||||
):
|
||||
layers.SeparableConv1D(depth_multiplier=0, filters=1, kernel_size=1)
|
||||
|
||||
# `filters` is not positive.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"Invalid value for argument `filters`. Expected a "
|
||||
"strictly positive value. Received filters=0.",
|
||||
):
|
||||
layers.SeparableConv1D(depth_multiplier=1, filters=0, kernel_size=1)
|
||||
|
||||
# `kernel_size` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `kernel_size` argument must be a tuple of "
|
||||
r"\d+ integers. Received kernel_size=\(1, 0\), including values"
|
||||
r" \{0\} that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.SeparableConv2D(
|
||||
depth_multiplier=2, filters=2, kernel_size=(1, 0)
|
||||
)
|
||||
|
||||
# `strides` has 0.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"The `strides` argument must be a tuple of \d+ "
|
||||
r"integers. Received strides=\(1, 0\), including values \{0\} "
|
||||
r"that do not satisfy `value > 0`",
|
||||
):
|
||||
layers.SeparableConv2D(
|
||||
depth_multiplier=2,
|
||||
filters=2,
|
||||
@ -170,7 +189,12 @@ class SeparableConvBasicTest(testing.TestCase, parameterized.TestCase):
|
||||
)
|
||||
|
||||
# `dilation_rate > 1` while `strides > 1`.
|
||||
with self.assertRaises(ValueError):
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
r"`strides > 1` not supported in conjunction with "
|
||||
r"`dilation_rate > 1`. Received: strides=\(2, 2\) and "
|
||||
r"dilation_rate=\(2, 1\)",
|
||||
):
|
||||
layers.SeparableConv2D(
|
||||
depth_multiplier=2,
|
||||
filters=2,
|
||||
|
Loading…
Reference in New Issue
Block a user