gpt4 book ai didi

tensorflow - 关于 tensorflow 图 : what am I wrong with this program?

转载 作者:行者123 更新时间:2023-12-03 16:53:56 28 4
gpt4 key购买 nike

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.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)

输出错误:
    Value Error: Fetch argument <tf.Tensor 'add_1:0' shape=(3,) dtype=int32> of
<tf.Tensor 'add_1:0' shape=(3,) dtype=int32> cannot be interpreted as a Tensor.
(Tensor Tensor("add_1:0", shape=(3,), dtype=int32) is not an element of this graph.)

最佳答案

该错误实际上为您提供了失败原因的提示。当您开始 session 时,您实际上使用的图表与 res 引用的图表不同。 .最简单的解决方案是简单地将所有内容移入 with tf.Graph().as_default(): .

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.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)

或者,您也可以删除行 with tf.Graph().as_default():然后它也会按预期工作。

关于tensorflow - 关于 tensorflow 图 : what am I wrong with this program?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826648/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com