How to run multiple graphs in a Session - Tensorflow API

后端 未结 3 1687
情歌与酒
情歌与酒 2021-02-02 14:00

Tensorflow API has provided few pre-trained models and allowed us to trained them with any dataset.

I would like to know how to initialize and use multiple graphs in on

3条回答
  •  迷失自我
    2021-02-02 14:27

    The graph arg in one session should be None or an instance of a graph.

    Here is the source code:

    class BaseSession(SessionInterface):
      """A class for interacting with a TensorFlow computation.
      The BaseSession enables incremental graph building with inline
      execution of Operations and evaluation of Tensors.
      """
    
      def __init__(self, target='', graph=None, config=None):
        """Constructs a new TensorFlow session.
        Args:
          target: (Optional) The TensorFlow execution engine to connect to.
          graph: (Optional) The graph to be used. If this argument is None,
            the default graph will be used.
          config: (Optional) ConfigProto proto used to configure the session.
        Raises:
          tf.errors.OpError: Or one of its subclasses if an error occurs while
            creating the TensorFlow session.
          TypeError: If one of the arguments has the wrong type.
        """
        if graph is None:
          self._graph = ops.get_default_graph()
        else:
          if not isinstance(graph, ops.Graph):
            raise TypeError('graph must be a tf.Graph, but got %s' % type(graph))
    

    And we can see from the bellow snippet that it cannot be a list.

    if graph is None:
      self._graph = ops.get_default_graph()
    else:
      if not isinstance(graph, ops.Graph):
        raise TypeError('graph must be a tf.Graph, but got %s' % type(graph))
    

    And from the ops.Graph(find by help(ops.Graph)) object, we can see that it cannot be multiple graphs.

    For more about the seesion and graph:

    If no `graph` argument is specified when constructing the session,
    the default graph will be launched in the session. If you are
    using more than one graph (created with `tf.Graph()` in the same
    process, you will have to use different sessions for each graph,
    but each graph can be used in multiple sessions. In this case, it
    is often clearer to pass the graph to be launched explicitly to
    the session constructor.
    

提交回复
热议问题