Tf.variable vs tf.constant in tensor flow

I ran the following code in

W = tf.Variable(tf.zeros([1, 3]), dtype=tf.float32, name="W") B = tf.constant([[1, 2, 3]], dtype=tf.float32, name="B") act = tf.add(W, B) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) sess.run(act) writer = tf.summary.FileWriter("./graphs", sess.graph) writer.close() 

And checked it with a tensor:

enter image description here

I am confused by the read operation and the operation to what is denoted by (W) . The constant B goes directly to the Add operation, and tf.variable contains all these work nodes inside. Here are my questions:

  • What is a (W) operation? Constant B is a regular circle denoting a constant. Nodes in oval form indicate a node operation. (W) doesn't look like any operation, but is denoted with the same oval-shaped node? What is this node work?

  • Add node explicitly reads a (W) node with a read operation, not a constant of node B Why is read necessary for variable nodes?

+5
source share
2 answers

W operation tf.Variable you stated here: W = tf.Variable(tf.zeros([1, 3]), dtype=tf.float32, name="W") . Behind the scenes, he performs many operations (for example, W.initializer - your init op, W.value() your read op, W.assign() to assign a value to yourself and may be more). You also see your initial zeros value.

All this is internal to tf.Variable, and you should not worry about that. That is why all this was curled (distracted) from you behind a large red frame.

+1
source

Due to the lack of reference to any documentation at an intermediate level, this is my pragmatic conceptual model of tensor flow variables.

Further, from https://www.tensorflow.org/programmers_guide/graphs#visualizing_your_graph it seems, at least, to imply an answer to your question.

"Executing v = tf.Variable (0) adds to the graph a tf.Operation, which will save the written tensor value that is saved between calls to tf.Session.run. The tf.Variable object wraps this operation and can be used as a tensor that will read "the current value of the stored value. The tf.Variable object also has methods such as assign and assign_add that create tf.Operation objects that update the stored value when executed."

And this is from https://www.tensorflow.org/programmers_guide/variables

"Internally, tf.Variable maintains a constant tensor. Specific operating systems allow you to read and modify the values ​​of this tensor."

And this is from http://www.goldsborough.me/tensorflow/ml/ai/python/2017/06/28/20-21-45-a_sweeping_tour_of_tensorflow/

"are memory buffers containing tensors."

Note that the lines between the nodes of the graph MUST be tensors. tf.constant (...) returns the tensor (class instance). However, tf.Variable (...) does not return a Tensor instance, but an instance of the Variable class

 x = tf.Variable(...) print(type(x)) # <class 'tensorflow.python.ops.variables.Variable'> y = tf.constant(...) print(type(y)) # <class 'tensorflow.python.framework.ops.Tensor'> 

To use the tf variable in an operation (whose arguments must be tensors), its value must first be "converted" to a tensor, and the "read" operation returns the "hidden" tensor that the variable represents. I believe the value is returned as tf.constant (which is a tensor).

Pay attention to the capital V in tf.Variable (...), and the small c in tf.constant (..). Tf.Variable (...) returns an instance of the tf.Variable class, so tf.Variable (...) creates an instance of the class, and read () is (visualizing a) a method in this class that returns value. When a value is assigned to a variable, it modifies this β€œhidden” tensor.

On the other hand, at least conceptually, tf.constant (...) is a factory function that returns an instance of the Tensor class.

It would be nice to have a link to some intermediate level documentation about this.

+2
source

Source: https://habr.com/ru/post/1269429/


All Articles