Variable

A variable is a mutable tensor that can be trained using an optimizer. For example, they can be the free variables that constitute the weights and biases of a neural network. 

We will now create two variables, one uniformly initialized, and one initialized with constant values:

import tensorflow as tf
import numpy as np

# variable initialized randomly
var = tf.get_variable("first_variable", shape=[1,3], dtype=tf.float32)

# variable initialized with constant values
init_val = np.array([4,5])
var2 = tf.get_variable("second_variable", shape=[1,2], dtype=tf.int32, initializer=tf.constant_initializer(init_val))

# create the session
sess = tf.Session()
# initialize all the variables
sess.run(tf.global_variables_initializer())

print(sess.run(var))

>> [[ 0.93119466 -1.0498083 -0.2198658 ]]

print(sess.run(var2))

>> [[4 5]]

The variables aren't initialized until global_variables_initializer() is called.

All the variables created in this way are set as trainable, meaning that the graph can modify them, for example, after an optimization operation. The variables can be set as non-trainable, as follows:

var2 = tf.get_variable("variable", shape=[1,2], trainable=False, dtype=tf.int32)

An easy way to access all the variables is as follows:

print(tf.global_variables())

>> [<tf.Variable 'first_variable:0' shape=(1, 3) dtype=float32_ref>, <tf.Variable 'second_variable:0' shape=(1, 2) dtype=int32_ref>]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset