Tensorflow feed_dict key不能解释为十

2024-09-27 07:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学习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])

Tags: selfanaddtfasfeed错误dict

热门问题