Variables

When training a model, we use variables to hold and update the parameters. Variables are like in-memory buffers containing tensors. All tensors we used previously were constant tensors, not variables.

Variables are managed or maintained by the session object. Variables persist between sessions, which is useful because tensor and operation objects are immutable:

# tensor variables W1 = tf.ones((3,3))W2 = tf.Variable(tf.zeros((3,3)), name="weights")  with tf.Session() as sess:   print(sess.run(W1))   sess.run(tf.global_variables_initializer())   print(sess.run(W2))

The preceding code gives the following output: 

[[ 1.  1.  1.] [ 1.  1.  1.] [ 1.  1.  1.]][[ 0.  0.  0.] [ 0.  0.  0.] [ 0.  0.  0.]]

TensorFlow variables must be initialized before they ...

Get Neural Network Programming with TensorFlow now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.