TensorFlow variables

A TensorFlow variable is the best way to represent shared, persistent state manipulated by your program.

Variables are manipulated via the tf$Variable class. A tf$Variable represents a tensor whose value can be changed by running ops on it. Specific ops allow you to read and modify the values of this tensor. Higher level libraries like Keras use tf$Variable to store model parameters. This guide covers how to create, update, and manage tf$Variables in TensorFlow.

Create a variable

To create a variable, simply provide the initial value

This creates a variable which is a three-dimensional tensor with shape [1, 2, 3] filled with zeros. This variable will, by default, have the dtype tf$float32. The dtype is, if not specified, inferred from the initial value.

If there’s a tf$device scope active, the variable will be placed on that device; otherwise the variable will be placed on the “fastest” device compatible with its dtype (this means most variables are automatically placed on a GPU if one is available). For example, the following snippet creates a variable named v and places it on the second GPU device:

with(tf$device("/device:GPU:1"), {
  v <- tf$Variable(tf$zeros(c(10, 10)))
})

Ideally though you should use the tf$distribute API, as that allows you to write your code once and have it work under many different distributed setups.

Use a variable

To use the value of a tf$Variable in a TensorFlow graph, simply treat it like a normal tf$Tensor:

To assign a value to a variable, use the methods assign, assign_add, and friends in the tf$Variable class. For example, here is how you can call these methods:

## <tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=1.0>

Most TensorFlow optimizers have specialized ops that efficiently update the values of variables according to some gradient descent-like algorithm. See tf$keras$optimizers$Optimizer for an explanation of how to use optimizers.

You can also explicitly read the current value of a variable, using read_value:

## <tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=1.0>
## tf.Tensor(1.0, shape=(), dtype=float32)

When the last reference to a tf$Variable goes out of scope its memory is freed.