0

Here's a part of the code I am using:

subX = tf.placeholder(tf.float32, ())
op1 = tf.assign(subX,x_hat-x)

When I execute this code snippet, I get:

AttributeError: 'Tensor' object has no attribute 'assign'

However, whenever I execute this, it works fine:

subX = tf.Variable(tf.zeros((299, 299, 3)))
op1 = tf.assign(subX,x_hat-x)

I don't understand why the latter works but not the former. This answer basically says that the Variable requires an initial value, while the placeholder does not. In both cases I am just overwriting them, so why does it matter? What's the difference between tf.placeholder and tf.Variable?

JobHunter69
  • 1,706
  • 5
  • 25
  • 49

1 Answers1

0

A placeholder is not meant to be used like this. Instead, it is an input point to your computational graph. You can do something like:

my_var = tf.placeholder(tf.float32)
my_const = tf.constant(12.3)

with tf.Session() as sess:
    result = sess.run(my_const*my_var, feed_dict={my_var: 45.7})

Then, print(result) would give the float 562.11005.

By that I mean that the placeholder (my_var, here) is simply a symbolic node representing an input to your computational graph and trying to assign some value to such representation at graph-creation time is conceptually wrong. If you want to dig deeper in the computational model of Tensorflow you may be interested in this TF graph explanation.

josoler
  • 1,393
  • 9
  • 15
  • So simply put it, I can never assign to a placeholder? – JobHunter69 Jul 10 '19 at 22:17
  • Yes, this is something you don't want to do. Why have you thought of a placeholder for such purpose in the first place? You might be forcing two unrelated dots to join – josoler Jul 10 '19 at 22:26
  • 1
    I'm just experimenting due to a bug in the code I'm having. For some reason, tf.assign is not giving me the correct output I am looking for – JobHunter69 Jul 10 '19 at 22:31