# 错误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


• 本文已收录于以下专栏：

举报原因： 您举报文章：错误Tensor is not an element of this graph tensorflow 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)