Improve Sequential test coverage

This commit is contained in:
Francois Chollet 2023-04-12 17:35:54 -07:00
parent 4d04065907
commit 8b8e0bc15f
2 changed files with 31 additions and 1 deletions

@ -40,7 +40,7 @@ class Sequential(Model):
): ):
raise ValueError( raise ValueError(
f"Sequential model '{self.name}' has already been configured to " f"Sequential model '{self.name}' has already been configured to "
f"use input shape {self._layers[0].batch_input_shape}. You cannot add " f"use input shape {self._layers[0].batch_shape}. You cannot add "
f"a different Input layer to it." f"a different Input layer to it."
) )

@ -94,5 +94,35 @@ class SequentialTest(testing.TestCase):
def test_dict_inputs(self): def test_dict_inputs(self):
pass pass
def test_errors(self):
# Trying to pass 2 Inputs
model = Sequential()
model.add(Input(shape=(2,), batch_size=3))
with self.assertRaisesRegex(ValueError, "already been configured"):
model.add(Input(shape=(2,), batch_size=3))
with self.assertRaisesRegex(ValueError, "already been configured"):
model.add(layers.InputLayer(shape=(2,), batch_size=3))
# Same name 2x
model = Sequential()
model.add(layers.Dense(2, name="dense"))
with self.assertRaisesRegex(ValueError, "should have unique names"):
model.add(layers.Dense(2, name="dense"))
# No layers
model = Sequential()
x = np.random.random((3, 2))
with self.assertRaisesRegex(ValueError, "no layers"):
model(x)
# Build conflict
model = Sequential()
model.add(Input(shape=(2,), batch_size=3))
model.add(layers.Dense(2))
with self.assertRaisesRegex(ValueError, "already been configured"):
model.build((3, 4))
# But this works
model.build((3, 2))
def test_serialization(self): def test_serialization(self):
pass pass