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])
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With