lhao0301
lhao0301

Reputation: 2011

When tf.variable() is realized in tensorflow

I started to learn tensorflow two days ago and when I see the sharing variable in tensorflow's offical website I was confused by the tf.Variable(). After I create one variable as follows:

demo = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="test")

I wonder whether demo consists of some random number at once or only after starting one session and run the graph. If it is the former, then why we can't show it by tf.print() and can show it after the session is runing.

Upvotes: 0

Views: 66

Answers (1)

nessuno
nessuno

Reputation: 27042

Because it's how Tensorflow works.

You first define a computational graph, in which you describe the interactions between variables, placeholders and operations. Note that the initialization of a variable is an operation and as such is placed into the graph description.

To compute anything, the graph you defined must launched into a session. The session is than placed into a device (that you can specify) and the selected device executes everything.

Therefore

demo = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="test")

is the definition of 2 operations:

  1. tf.random_normal([5, 5, 32, 32]) creates a operation node in the graph, that consists in the generation of a Tensor with the defined shape filled by random values extracted from the normal distribution.
  2. tf.Variable(value, name="test") creates a variable node (with name=test) first and than an operation node which consists in the assignment of the value to the variable.

Upvotes: 1

Related Questions