Exemple #1
0
 def name_scopes(self):
     """Returns a tuple of all name_scopes generated by this module."""
     if tf.executing_eagerly():
         raise NotSupportedError(
             "The name_scopes property is not supported in eager mode.")
     return tuple(subgraph.name_scope
                  for subgraph in self._connected_subgraphs)
Exemple #2
0
    def last_connected_subgraph(self):
        """Returns the last subgraph created by this module.

    Returns:
      The last connected subgraph.

    Raises:
      NotConnectedError: If the module is not connected to the Graph.
    """
        if tf.executing_eagerly():
            raise NotSupportedError(
                "Connected sub-graphs are not tracked in eager mode.")
        self._ensure_is_connected()
        return self._connected_subgraphs[-1]
Exemple #3
0
  def get_variables(self,
                    collection=tf.GraphKeys.TRAINABLE_VARIABLES):
    """Returns tuple of `tf.Variable`s declared inside this module.

    Note that this operates by searching this module's variable scope,
    and so does not know about any modules that were constructed elsewhere but
    used inside this module.

    This method explicitly re-enters the Graph which this module has been
    connected to.

    Args:
      collection: Collection to restrict query to. By default this is
        `tf.GraphKeys.TRAINABLE_VARIABLES`, which doesn't
        include non-trainable variables such as moving averages.

    Returns:
      A tuple of `tf.Variable` objects.

    Raises:
      NotConnectedError: If the module is not connected to the Graph.
    """
    self._ensure_is_connected()

    if self._defun_wrapped and tf.executing_eagerly():
      raise NotSupportedError(
          "`module.get_variables()` relies on TensorFlow collections which are "
          "not supported when your module is wrapped with defun. Instead use "
          "`module.trainable_variables` or `module.variables`.")

    # Explicitly re-enter Graph, in case the module is being queried with a
    # different default Graph from the one it was connected to. If this was not
    # here then querying the variables from a different graph scope would
    # produce an empty tuple.
    with self._graph.as_default():
      return util.get_variables_in_scope(
          self.variable_scope, collection=collection)
Exemple #4
0
 def __getstate__(self):
     raise NotSupportedError(
         "Sonnet AbstractModule instances cannot be serialized. You should "
         "instead serialize all necessary configuration which will allow "
         "modules to be rebuilt.")
Exemple #5
0
 def connected_subgraphs(self):
     """Returns the subgraphs created by this module so far."""
     if tf.executing_eagerly():
         raise NotSupportedError(
             "Connected sub-graphs are not tracked in eager mode.")
     return tuple(self._connected_subgraphs)