Tensor is not an element of this graph

前端 未结 7 1178
日久生厌
日久生厌 2020-12-02 15:49

I\'m getting this error

\'ValueError: Tensor Tensor(\"Placeholder:0\", shape=(1, 1), dtype=int32) is not an element of this graph.\'

7条回答
  •  天涯浪人
    2020-12-02 16:21

    When you create a Model, the session hasn't been restored yet. All placeholders, variables and ops that are defined in Model.__init__ are placed in a new graph, which makes itself a default graph inside with block. This is the key line:

    with tf.Graph().as_default():
      ...
    

    This means that this instance of tf.Graph() equals to tf.get_default_graph() instance inside with block, but not before or after it. From this moment on, there exist two different graphs.

    When you later create a session and restore a graph into it, you can't access the previous instance of tf.Graph() in that session. Here's a short example:

    with tf.Graph().as_default() as graph:
      var = tf.get_variable("var", shape=[3], initializer=tf.zeros_initializer)
    
    # This works
    with tf.Session(graph=graph) as sess:
      sess.run(tf.global_variables_initializer())
      print(sess.run(var))  # ok because `sess.graph == graph`
    
    # This fails
    saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
    with tf.Session() as sess:
      saver.restore(sess, "/tmp/model.ckpt")
      print(sess.run(var))   # var is from `graph`, not `sess.graph`!
    

    The best way to deal with this is give names to all nodes, e.g. 'input', 'target', etc, save the model and then look up the nodes in the restored graph by name, something like this:

    saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
    with tf.Session() as sess:
      saver.restore(sess, "/tmp/model.ckpt")      
      input_data = sess.graph.get_tensor_by_name('input')
      target = sess.graph.get_tensor_by_name('target')
    

    This method guarantees that all nodes will be from the graph in session.

提交回复
热议问题