hub.ModuleSpec

Class ModuleSpec

Represents the contents of a Module before it has been instantiated.

A ModuleSpec is the blueprint used by Module to create one or more instances of a specific module in one or more graphs. The details on how to construct the Module are internal to the library implementation but methods to inspect a Module interface are public.

__init__

__init__()

Do not instantiate directly.

Methods

export

export(
    path,
    _sentinel=None,
    checkpoint_path=None,
    name_transform_fn=None
)

Exports a ModuleSpec with weights taken from a checkpoint.

This is an helper to export modules directly from a ModuleSpec without having to create a session and set the variables to the intended values.

Example usage:

spec = hub.create_module_spec(module_fn)
spec.export("/path/to/export_module",
            checkpoint_path="/path/to/training_model")

In some cases, the variable name in the checkpoint does not match the variable name in the module. It is possible to work around that by providing a checkpoint_map_fn that performs the variable mapping. For example with: name_transform_fn = lambda x: "extra_scope/" + x.

Args:

  • path: path where to export the module to.
  • _sentinel: used to prevent positional arguments besides path.
  • checkpoint_path: path where to load the weights for the module. Mandatory parameter and must be passed by name.
  • name_transform_fn: optional function to provide mapping between variable name in the module and the variable name in the checkpoint.

Raises:

  • ValueError: if missing mandatory checkpoint_path parameter.

get_attached_message

get_attached_message(
    key,
    message_type,
    tags=None,
    required=False
)

Returns the message attached to the module under the given key, or None.

Module publishers can attach protocol messages to modules at creation time to provide module consumers with additional information, e.g., on module usage or provenance (see see hub.attach_message()). A typical use would be to store a small set of named values with modules of a certain type so that a support library for consumers of such modules can be parametric in those values.

This method can also be called on a Module instantiated from a ModuleSpec, then tags are set to those used in module instatiation.

Args:

  • key: A string with the key of an attached message.
  • message_type: A concrete protocol message class (not object) used to parse the attached message from its serialized representation. The message type for a particular key must be advertised with the key.
  • tags: Optional set of strings, specifying the graph variant from which to read the attached message.
  • required: An optional boolean. Setting it true changes the effect of an unknown key from returning None to raising a KeyError with text about attached messages.

Returns:

An instance of message_type with the message contents attached to the module, or None if key is unknown and required is False.

Raises:

  • KeyError: if key is unknown and required is True.

get_input_info_dict

get_input_info_dict(
    signature=None,
    tags=None
)

Describes the inputs required by a signature.

Args:

  • signature: A string with the signature to get inputs information for. If None, the default signature is used if defined.
  • tags: Optional set of strings, specifying the graph variant to query.

Returns:

A dict from input names to objects that provide (1) a property dtype, (2) a method get_shape() and (3) a read-only boolean property is_sparse. The first two are compatible with the common API of Tensor and SparseTensor objects.

Raises:

  • KeyError: if there is no such signature or graph variant.

get_output_info_dict

get_output_info_dict(
    signature=None,
    tags=None
)

Describes the outputs provided by a signature.

Args:

  • signature: A string with the signature to get ouputs information for. If None, the default signature is used if defined.
  • tags: Optional set of strings, specifying the graph variant to query.

Returns:

A dict from input names to objects that provide (1) a property dtype, (2) a method get_shape() and (3) a read-only boolean property is_sparse. The first two are compatible with the common API of Tensor and SparseTensor objects.

Raises:

  • KeyError: if there is no such signature or graph variant.

get_signature_names

get_signature_names(tags=None)

Returns the module's signature names as an iterable of strings.

get_tags

get_tags()

Lists the graph variants as an iterable of set of tags.