错误Tensor is not an element of this graph tensorflow

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

  1. import tensorflow as tf  
  2.   
  3. def activation(e, f, g):  
  4.   
  5.   return e + f + g  
  6.   
  7. with tf.Graph().as_default():  
  8.   a = tf.constant([545], name='a')  
  9.   b = tf.constant([012], name='b')  
  10.   c = tf.constant([505], name='c')  
  11.   
  12.   res = activation(a, b, c)  
  13.   
  14. init = tf.initialize_all_variables()  
  15.   
  16. with tf.Session() as sess:  
  17.   # Start running operations on the Graph.  
  18.   sess.run(init)  
  19.   hi = sess.run(res)  
  20.   print hi  

说明:运行该脚本会报错: is not an element of this graph。该错误的意思就是说操作不在图中,也就是和我们的session相关联的图中并没有该操作.why?

2、分析原因:

首先,tensorflow会为我们指定一张默认的图.然后sesssion会直接和该默认图相关联.除非我们自定义一张图.不然,我们的操作都是在那张默认图上的.然后我们分析报错的代码:

  1. import tensorflow as tf  
  2.   
  3. def activation(e, f, g):  
  4.   
  5.   return e + f + g  
  6.   
  7. with tf.Graph().as_default():  
  8.   a = tf.constant([545], name='a')  
  9.   b = tf.constant([012], name='b')  
  10.   c = tf.constant([505], name='c')  
  11.   
  12.   res = activation(a, b, c)  
说明:此段代码描述的是,新增一张图,然后在该图上去定义操作,也就是目前我们有两张图,一张是系统自定义的图.
  1. init = tf.initialize_all_variables()  
  2.   
  3. with tf.Session() as sess:  
  4.   # Start running operations on the Graph.  
  5.   sess.run(init)  
  6.   hi = sess.run(res)  
  7.   print hi  
说明:此段代码,其实是描述的是,在默认的图上去定义了初始化操作,然后想在默认图上去执行res操作,注意res操作是在我们自定义的图上的。所以,当然是会报错的.

修改一:

  1. import tensorflow as tf  
  2.   
  3. def activation(e, f, g):  
  4.   
  5.   return e + f + g  
  6.   
  7. with tf.Graph().as_default():  
  8.   a = tf.constant([545], name='a')  
  9.   b = tf.constant([012], name='b')  
  10.   c = tf.constant([505], name='c')  
  11.   
  12.   res = activation(a, b, c)  
  13.   
  14. init = tf.initialize_all_variables()  
  15.   
  16. with tf.Session() as sess:  
  17.   # Start running operations on the Graph.  
  18.   sess.run(init)  
  19.   #hi = sess.run(res)  
  20.   #print hi  
说明:这样修改就没问题,只在session上运行init操作,也就是在默认图上执行session操作.

修改二:

  1. import tensorflow as tf  
  2.   
  3. def activation(e, f, g):  
  4.   
  5.   return e + f + g  
  6.   
  7. with tf.Graph().as_default():  
  8.   a = tf.constant([545], name='a')  
  9.   b = tf.constant([012], name='b')  
  10.   c = tf.constant([505], name='c')  
  11.   
  12.   res = activation(a, b, c)  
  13.   
  14.   init = tf.initialize_all_variables()  
  15.   
  16.   with tf.Session() as sess:  
  17.     # Start running operations on the Graph.  
  18.     sess.run(init)  
  19.     hi = sess.run(res)  
  20.     print hi  

说明:把所有的操作都定义在我们指定的图上面,并且,session也定义在缩进内,相当于该session只和我们定义的图相关联,只执行我们定义在图内的操作,这样就不会报错.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值