Skip to content

Commit f563082

Browse files
fchollettensorflower-gardener
authored andcommitted
Remove keras.experimental.export_saved_model API. It was deprecated in TF 1.15, which was 3 years ago. It was never a stable API in the first place (it was an experimental API) and had no backwards compatibility guarantee.
Consider upgrading to: 1. `model.save()` 2. `tf.saved_model.save()` PiperOrigin-RevId: 474040485
1 parent 1bac508 commit f563082

File tree

1 file changed

+0
-106
lines changed

1 file changed

+0
-106
lines changed

tensorflow_probability/python/layers/distribution_layer_test.py

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,6 @@ def assertSerializable(self, model, batch_size=1):
351351
self.assertAllEqual(self.evaluate(model(x).log_prob(y)),
352352
self.evaluate(model_copy(x).log_prob(y)))
353353

354-
def assertExportable(self, model, batch_size=1):
355-
"""Assert a Keras model supports export_saved_model/load_from_saved_model.
356-
357-
Args:
358-
model: A Keras model with Tensor output.
359-
batch_size: The batch size to use when checking that the model produces
360-
the same results as a serialized/deserialized copy. Default value: 1.
361-
"""
362-
batch_shape = [batch_size]
363-
364-
input_shape = batch_shape + model.input.shape[1:].as_list()
365-
dtype = model.input.dtype.as_numpy_dtype
366-
367-
model_dir = self.create_tempdir()
368-
tf1.keras.experimental.export_saved_model(model, model_dir.full_path)
369-
model_copy = tf1.keras.experimental.load_from_saved_model(
370-
model_dir.full_path)
371-
372-
x = np.random.uniform(-3., 3., input_shape).astype(dtype)
373-
self.assertAllEqual(self.evaluate(model(x)), self.evaluate(model_copy(x)))
374-
self.assertAllEqual(model.predict(x), model_copy.predict(x))
375-
376354
def test_serialization(self):
377355
model = tfk.Sequential([
378356
tfkl.Dense(2, input_shape=(5,)),
@@ -382,15 +360,6 @@ def test_serialization(self):
382360
])
383361
self.assertSerializable(model)
384362

385-
model = tfk.Sequential([
386-
tfkl.Dense(2, input_shape=(5,)),
387-
# pylint: disable=g-long-lambda
388-
tfpl.DistributionLambda(lambda t: tfd.Normal(
389-
loc=t[..., 0:1], scale=tf.exp(t[..., 1:2]))),
390-
tfkl.Lambda(lambda d: d.mean() + d.stddev())
391-
])
392-
self.assertExportable(model, batch_size=4)
393-
394363
@staticmethod
395364
def _make_distribution(t):
396365
return tfpl.MixtureSameFamily.new(t, 3, tfpl.IndependentNormal([2]))
@@ -405,16 +374,6 @@ def test_serialization_static_method(self):
405374
model.compile(optimizer='adam', loss='mse')
406375
self.assertSerializable(model, batch_size=3)
407376

408-
model = tfk.Sequential([
409-
tfkl.Dense(15, input_shape=(5,)),
410-
tfpl.DistributionLambda(
411-
DistributionLambdaSerializationTest._make_distribution,
412-
convert_to_tensor_fn=tfd.Distribution.mean),
413-
tfkl.Lambda(lambda x: x + 1.)
414-
])
415-
model.compile(optimizer='adam', loss='mse')
416-
self.assertExportable(model)
417-
418377
def test_serialization_closure_over_lambdas_tensors_and_numpy_array(self):
419378
if six.PY2 and not tf.executing_eagerly():
420379
self.skipTest('Serialization of constant graph-mode Tensors is not '
@@ -433,14 +392,6 @@ def make_distribution(t):
433392
])
434393
self.assertSerializable(model, batch_size=4)
435394

436-
model = tfk.Sequential([
437-
tfkl.Dense(15, input_shape=(5,)),
438-
# pylint: disable=unnecessary-lambda
439-
tfpl.DistributionLambda(lambda t: make_distribution(t)),
440-
tfkl.Lambda(lambda d: d.mean() + d.stddev())
441-
])
442-
self.assertExportable(model, batch_size=2)
443-
444395

445396
@test_util.test_graph_and_eager_modes
446397
class DistributionLambdaVariableCreation(test_util.TestCase):
@@ -874,34 +825,6 @@ def test_serialization(self):
874825
self.assertAllEqual(self.evaluate(model(x).log_prob(ones)),
875826
self.evaluate(model_copy(x).log_prob(ones)))
876827

877-
def test_model_export(self):
878-
event_shape = [3, 2]
879-
params_size = self.layer_class.params_size(event_shape)
880-
batch_shape = [4]
881-
882-
low = self._build_tensor(-3., dtype=self.dtype)
883-
high = self._build_tensor(3., dtype=self.dtype)
884-
x = self.evaluate(tfd.Uniform(low, high).sample(
885-
batch_shape + [params_size], seed=42))
886-
887-
model = tfk.Sequential([
888-
tfkl.Dense(params_size, input_shape=(params_size,), dtype=self.dtype),
889-
self.layer_class(event_shape, validate_args=True,
890-
convert_to_tensor_fn='mean', dtype=self.dtype),
891-
# NOTE: For TensorFlow to be able to serialize the graph (i.e., for
892-
# serving), the model must output a Tensor and not a Distribution.
893-
tfkl.Dense(1, dtype=self.dtype),
894-
])
895-
model.compile(optimizer='adam', loss='mse')
896-
897-
model_dir = self.create_tempdir()
898-
tf1.keras.experimental.export_saved_model(model, model_dir.full_path)
899-
model_copy = tf1.keras.experimental.load_from_saved_model(
900-
model_dir.full_path)
901-
902-
self.assertAllEqual(self.evaluate(model(x)), self.evaluate(model_copy(x)))
903-
self.assertEqual(self.dtype, model(x).dtype.as_numpy_dtype)
904-
905828

906829
@test_util.test_graph_and_eager_modes
907830
class _IndependentBernoulliTest(_IndependentLayerTest):
@@ -1246,35 +1169,6 @@ def test_serialization(self):
12461169
self.assertAllEqual(self.evaluate(model(x).log_prob(ones)),
12471170
self.evaluate(model_copy(x).log_prob(ones)))
12481171

1249-
def test_model_export(self):
1250-
n = 5
1251-
event_shape = [3, 2]
1252-
params_size = self.layer_class.params_size(n, event_shape)
1253-
batch_shape = [4]
1254-
1255-
low = self._build_tensor(-3., dtype=self.dtype)
1256-
high = self._build_tensor(3., dtype=self.dtype)
1257-
x = self.evaluate(tfd.Uniform(low, high).sample(
1258-
batch_shape + [params_size], seed=42))
1259-
1260-
model = tfk.Sequential([
1261-
tfkl.Dense(params_size, input_shape=(params_size,), dtype=self.dtype),
1262-
self.layer_class(n, event_shape, validate_args=True,
1263-
convert_to_tensor_fn='mean', dtype=self.dtype),
1264-
# NOTE: For TensorFlow to be able to serialize the graph (i.e., for
1265-
# serving), the model must output a Tensor and not a Distribution.
1266-
tfkl.Dense(1, dtype=self.dtype),
1267-
])
1268-
model.compile(optimizer='adam', loss='mse')
1269-
1270-
model_dir = self.create_tempdir()
1271-
tf1.keras.experimental.export_saved_model(model, model_dir.full_path)
1272-
model_copy = tf1.keras.experimental.load_from_saved_model(
1273-
model_dir.full_path)
1274-
1275-
self.assertAllEqual(self.evaluate(model(x)), self.evaluate(model_copy(x)))
1276-
self.assertEqual(self.dtype, model(x).dtype.as_numpy_dtype)
1277-
12781172

12791173
@test_util.test_graph_and_eager_modes
12801174
class _MixtureLogisticTest(_MixtureLayerTest):

0 commit comments

Comments
 (0)