Tensorflow 5: Variables

My previous post. .  All my code can be found on github (5_Variables.ipynb)

Variables

We use variables to store data during computation.

Types

There are many different ways to create Variables.  Here are some examples.

We need to allocate memory for the variables.  We do that by calling

Then when the above commands are run we get:

Changing values

To change the value of a variable we use the assign operation.

We can also save an operation on a variable and run it later.

Here i have created a new variable my_variable and initialized it to 0.

I have also created an operation called increment_my_variable that adds one to its current vale.

I can run both the operation or assign the variable directly.

Above each time increment_my_variable runs it increments the value.  But we can also directly assign a value to my_variable as well.

Saving

If you want to save the values of variables use the tf.train.Saver() operation.

 

This will create the files in the current directory.

 

You will use this later to save the weights and biases of trained models.

 

Restoring

Then later call

To restore the values from the file.  Example: