@@ -351,28 +351,6 @@ def assertSerializable(self, model, batch_size=1):
351
351
self .assertAllEqual (self .evaluate (model (x ).log_prob (y )),
352
352
self .evaluate (model_copy (x ).log_prob (y )))
353
353
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
-
376
354
def test_serialization (self ):
377
355
model = tfk .Sequential ([
378
356
tfkl .Dense (2 , input_shape = (5 ,)),
@@ -382,15 +360,6 @@ def test_serialization(self):
382
360
])
383
361
self .assertSerializable (model )
384
362
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
-
394
363
@staticmethod
395
364
def _make_distribution (t ):
396
365
return tfpl .MixtureSameFamily .new (t , 3 , tfpl .IndependentNormal ([2 ]))
@@ -405,16 +374,6 @@ def test_serialization_static_method(self):
405
374
model .compile (optimizer = 'adam' , loss = 'mse' )
406
375
self .assertSerializable (model , batch_size = 3 )
407
376
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
-
418
377
def test_serialization_closure_over_lambdas_tensors_and_numpy_array (self ):
419
378
if six .PY2 and not tf .executing_eagerly ():
420
379
self .skipTest ('Serialization of constant graph-mode Tensors is not '
@@ -433,14 +392,6 @@ def make_distribution(t):
433
392
])
434
393
self .assertSerializable (model , batch_size = 4 )
435
394
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
-
444
395
445
396
@test_util .test_graph_and_eager_modes
446
397
class DistributionLambdaVariableCreation (test_util .TestCase ):
@@ -874,34 +825,6 @@ def test_serialization(self):
874
825
self .assertAllEqual (self .evaluate (model (x ).log_prob (ones )),
875
826
self .evaluate (model_copy (x ).log_prob (ones )))
876
827
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
-
905
828
906
829
@test_util .test_graph_and_eager_modes
907
830
class _IndependentBernoulliTest (_IndependentLayerTest ):
@@ -1246,35 +1169,6 @@ def test_serialization(self):
1246
1169
self .assertAllEqual (self .evaluate (model (x ).log_prob (ones )),
1247
1170
self .evaluate (model_copy (x ).log_prob (ones )))
1248
1171
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
-
1278
1172
1279
1173
@test_util .test_graph_and_eager_modes
1280
1174
class _MixtureLogisticTest (_MixtureLayerTest ):
0 commit comments