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

913人阅读 评论(0)

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


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：357820次
• 积分：5755
• 等级：
• 排名：第4707名
• 原创：227篇
• 转载：42篇
• 译文：0篇
• 评论：58条
阅读排行
最新评论