Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow feed_dict key cannot be interpreted as Tensor

I just starts to learn Tensorflow in python. I got the following error when I starts with a simple AddTwo class. the error messages are:

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

Can anyone help me to point out the correct way for me?

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

main code that calls the AddTwo class:

adder = graph.AddTwo()  
print adder.Add(50, 7)
print adder.Add([1,5],[6,7])
like image 494
Jerry Song Avatar asked Nov 09 '22 03:11

Jerry Song


1 Answers

As I suggested in comment, you should open the session with created graph, so code should be like this:

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
like image 133
VMAtm Avatar answered Nov 15 '22 08:11

VMAtm