gpt4 book ai didi

python - Tensorflow feed_dict 键不能解释为 Tensor

转载 作者:太空狗 更新时间:2023-10-30 01:32:25 24 4
gpt4 key购买 nike

我刚开始用 python 学习 Tensorflow。当我从一个简单的 AddTwo 类开始时出现以下错误。错误信息是:

Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", dtype=float32) is not an element of this graph.

谁能帮我指出正确的方法?

import numpy as np
import tensorflow as tf

class AddTwo(object):

def __init__(self):
self.graph = tf.Graph()

with self.graph.as_default():
self.sess = tf.Session()
self.X = tf.placeholder(tf.float32)
self.Y = tf.placeholder(tf.float32)

# Create an op to add the two placeholders.
self.Z = tf.add(self.X, self.Y)

def Add(self, x, y):
with tf.Session() as sess:
#self.Z = tf.add(x, y)
result = sess.run(self, feed_dict={self.X: x, self.Y: y})
return result

调用AddTwo类的主要代码:

adder = graph.AddTwo()  
print adder.Add(50, 7)
print adder.Add([1,5],[6,7])

最佳答案

正如我在评论中建议的那样,您应该使用创建的图形打开 session ,因此代码应该是这样的:

with self.graph.as_default():
# no session here
self.X = tf.placeholder(tf.float32)
self.Y = tf.placeholder(tf.float32)

# open session with providing the graph
with tf.Session(graph=self.graph) as sess:
pass

关于python - Tensorflow feed_dict 键不能解释为 Tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732627/

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