# 错误Tensor is not an element of this graph tensorflow

1、说明：tensorflow使用图来定义计算,在session中来执行图中定义的计算,如果没有显式的说明,那么session就跟默认的图相关联.graph 和 session应该是一一对应的.下面,举例说明,session如果和graph不一一对应的话,会出现error

import tensorflow as tf

def activation(e, f, g):

return e + f + g

with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')

res = activation(a, b, c)

init = tf.initialize_all_variables()

with tf.Session() as sess:
# Start running operations on the Graph.
sess.run(init)
hi = sess.run(res)
print hi


2、分析原因:

import tensorflow as tf

def activation(e, f, g):

return e + f + g

with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')

res = activation(a, b, c)


init = tf.initialize_all_variables()

with tf.Session() as sess:
# Start running operations on the Graph.
sess.run(init)
hi = sess.run(res)
print hi


import tensorflow as tf

def activation(e, f, g):

return e + f + g

with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')

res = activation(a, b, c)

init = tf.initialize_all_variables()

with tf.Session() as sess:
# Start running operations on the Graph.
sess.run(init)
#hi = sess.run(res)
#print hi


import tensorflow as tf

def activation(e, f, g):

return e + f + g

with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')

res = activation(a, b, c)

init = tf.initialize_all_variables()

with tf.Session() as sess:
# Start running operations on the Graph.
sess.run(init)
hi = sess.run(res)
print hi


#### ValueError: Tensor Tensor("mrcnn_detection/PyFunc:0", dtype=float32) is not an element of this graph

2018-01-13 23:57:58

2017-11-03 13:05:40

#### keras框架 反复调用model 模型 出错

2018-01-10 21:44:35

#### 深度学习入门---Keras报错集合笔记

2018-05-21 16:40:49

#### ######（较深的应用）TensorFlow学习（三）：Graph和Session ######（较深的应用）

2017-03-03 16:32:38

#### keras（tensorflow后端）给模型建立单独的graph

2018-01-18 11:06:56

#### TensorFlow学习（三）：Graph和Session

2016-12-24 16:23:56

#### Tensorflow、Keras使用过程中的问题

2017-11-24 18:31:26

#### Keras+Django多次加载model出现错误

2018-06-24 17:16:18

#### GraphQL学习笔记（GraphQL + Spring boot demo） part 2

2017-07-29 23:15:11