MCPcopy Index your code
hub / github.com/tensorflow/hub / testInputOutputDict

Method testInputOutputDict

tensorflow_hub/keras_layer_test.py:496–533  ·  view source on GitHub ↗

Tests use of input/output dicts.

(self, pass_output_shapes)

Source from the content-addressed store, hash-verified

494 @parameterized.named_parameters(("NoOutputShapes", False),
495 ("WithOutputShapes", True))
496 def testInputOutputDict(self, pass_output_shapes):
497 """Tests use of input/output dicts."""
498 # Create a SavedModel to compute sigma=[x+y, x+2y] and maybe delta=x-y.
499 export_dir = os.path.join(self.get_temp_dir(), "with-dicts")
500 _save_model_with_dict_input_output(export_dir)
501 # Build a Model from it using Keras' "functional" API.
502 x_in = tf_keras_v2.layers.Input(shape=(1,), dtype=tf.float32)
503 y_in = tf_keras_v2.layers.Input(shape=(1,), dtype=tf.float32)
504 dict_in = dict(x=x_in, y=y_in)
505 kwargs = dict(arguments=dict(return_dict=True)) # For the SavedModel.
506 if pass_output_shapes:
507 # Shape inference works without this, but we pass it anyways to exercise
508 # that code path and see that map_structure is called correctly
509 # and calls Tensor.set_shape() with compatible values.
510 kwargs["output_shape"] = dict(sigma=(2,), delta=(1,))
511 imported = hub.KerasLayer(export_dir, **kwargs)
512 dict_out = imported(dict_in)
513 delta_out = dict_out["delta"]
514 sigma_out = dict_out["sigma"]
515 concat_out = tf_keras_v2.layers.concatenate([delta_out, sigma_out])
516 model = tf_keras_v2.Model(dict_in, [delta_out, sigma_out, concat_out])
517 # Test the model.
518 x = np.array([[11.], [22.], [33.]], dtype=np.float32)
519 y = np.array([[1.], [2.], [3.]], dtype=np.float32)
520 outputs = model(dict(x=x, y=y))
521 self.assertLen(outputs, 3)
522 delta, sigma, concat = [x.numpy() for x in outputs]
523 self.assertAllClose(delta, np.array([[10.], [20.], [30.]]))
524 self.assertAllClose(sigma, np.array([[12., 13.], [24., 26.], [36., 39.]]))
525 self.assertAllClose(
526 concat, np.array([[10., 12., 13.], [20., 24., 26.], [30., 36., 39.]]))
527 # Test round-trip through config.
528 config = imported.get_config()
529 new_layer = hub.KerasLayer.from_config(_json_cycle(config))
530 if pass_output_shapes:
531 self.assertEqual(new_layer._output_shape, imported._output_shape)
532 else:
533 self.assertFalse(hasattr(new_layer, "_output_shape"))
534
535 @parameterized.named_parameters(("NoOutputShapes", False),
536 ("WithOutputShapes", True))

Callers

nothing calls this directly

Calls 4

get_configMethod · 0.95
_json_cycleFunction · 0.85
from_configMethod · 0.80

Tested by

no test coverage detected