1

I've googled and came to know that Tensorflow's constant() function generates a constant Tensor (big surprise!) and cannot be modified.

But when I do:

>>> a = tf.constant(0.0)
>>> a = a + 1.0

I don't see any error generated by Tensorflow.

I understand the reason, a is now a new tensor operation Add (<tf.Tensor 'add_1:0' shape=() dtype=float32>).

My question is, What is the use of Tensorflow constant if we can modify it? Does it has anything to do with graph optimization? Am I missing something trivial here?

Thanks in advance.

1 Answers1

6

yes, you're missing something trivial.

 a = tf.constant(0.0)

a is a python variable that holds a constant node of the computational graph. In the graph it has a name, let's call it constant:0.

a = a + 1.0

a is a new python variable (the assignment is a destructive operation) that holds the add operation between the node constant:0 that is still defined in the graph and a new constant node, automatically create when using 1.0 (constant_1:0).

Thus, in this line, you're overriding a python variable in order to make it holding an add node: you're not touching the values of the graph node constant:0.

The constant, in fact, is defined in the graph that tensorflow describes and it can't be changed. What you can change, instead, is the python variable that points to a certain node in the graph.

You can think about the python variables as pointers to the graph's nodes.

nessuno
  • 26,493
  • 5
  • 83
  • 74
  • Ah, now I understant. I mentioned "I understand the reason, a is now a new tensor operation Add" in the question, but I didn't see that Tensorflow dataflow graph and Python are two different things. Thank you –  Oct 17 '17 at 09:29
  • Is there any way to intentionally generate this error? –  Oct 17 '17 at 09:33
  • Is there any way I can generate a tensorflow error stating something like "cannot modify constant tensor"? –  Oct 18 '17 at 08:10
  • You can try to **assign into the graph** a value to a constant tensor. This will bring an error (because the constant object has no `assign` attribute). Just try `assign = tf.assign(a, 10.)` and `sess.run(assing)`. The program will crash at the like `tf.assing(a,10)` telling you that `Tensor object has no attribute assign` – nessuno Oct 18 '17 at 09:20
  • Got it. Thank you. –  Oct 19 '17 at 14:35