Introduction to Variables

A TensorFlow variable is the recommended way to represent shared, persistent state your program manipulates. This guide covers how to create, update, and manage instances of tf$Variable in TensorFlow.

Variables are created and tracked 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 tf$keras use tf$Variable to store model parameters.

Setup

This notebook discusses variable placement. If you want to see on what device your variables are placed, uncomment this line.

library(tensorflow)

# Uncomment to see where your variables get placed (see below)
# tf$debugging$set_log_device_placement(TRUE)

Create a variable

To create a variable, provide an initial value. The tf$Variable will have the same dtype as the initialization value.

my_tensor <- as_tensor(1:4, "float32", shape = c(2, 2))
Loaded Tensorflow version 2.9.1
(my_variable <- tf$Variable(my_tensor))
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
# Variables can be all kinds of types, just like tensors

(bool_variable <- tf$Variable(c(FALSE, FALSE, FALSE, TRUE)))
<tf.Variable 'Variable:0' shape=(4,) dtype=bool, numpy=array([False, False, False,  True])>
(complex_variable <- tf$Variable(c(5 + 4i, 6 + 1i)))
<tf.Variable 'Variable:0' shape=(2,) dtype=complex128, numpy=array([5.+4.j, 6.+1.j])>

A variable looks and acts like a tensor, and, in fact, is a data structure backed by a tf$Tensor. Like tensors, they have a dtype and a shape, and can be exported to regular R arrays.

cat("Shape: "); my_variable$shape
Shape: 
TensorShape([2, 2])
cat("DType: "); my_variable$dtype
DType: 
tf.float32
cat("As R array: "); str(as.array(my_variable))
As R array: 
 num [1:2, 1:2] 1 3 2 4

Most tensor operations work on variables as expected, although variables cannot be reshaped.

message("A variable: ")
A variable: 
my_variable
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
message("Viewed as a tensor: ")
Viewed as a tensor: 
as_tensor(my_variable)
tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)
message("Index of highest value: ")
Index of highest value: 
tf$math$argmax(my_variable)
tf.Tensor([1 1], shape=(2), dtype=int64)
# This creates a new tensor; it does not reshape the variable.
message("Copying and reshaping: ") 
Copying and reshaping: 
tf$reshape(my_variable, c(1L, 4L))
tf.Tensor([[1. 2. 3. 4.]], shape=(1, 4), dtype=float32)

As noted above, variables are backed by tensors. You can reassign the tensor using tf$Variable$assign. Calling assign does not (usually) allocate a new tensor; instead, the existing tensor’s memory is reused.

a <- tf$Variable(c(2, 3))

# assigning allowed, input is automatically 
# cast to the dtype of the Variable, float32
a$assign(as.integer(c(1, 2)))
<tf.Variable 'UnreadVariable' shape=(2,) dtype=float32, numpy=array([1., 2.], dtype=float32)>
# resize the variable is not allowed
try(a$assign(c(1.0, 2.0, 3.0)))
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Cannot assign value to variable ' Variable:0': Shape mismatch.The variable shape (2,), and the assigned value shape (3,) are incompatible.

If you use a variable like a tensor in operations, you will usually operate on the backing tensor.

Creating new variables from existing variables duplicates the backing tensors. Two variables will not share the same memory.

a <- tf$Variable(c(2, 3))
# Create b based on the value of a

b <- tf$Variable(a)
a$assign(c(5, 6))
<tf.Variable 'UnreadVariable' shape=(2,) dtype=float32, numpy=array([5., 6.], dtype=float32)>
# a and b are different

as.array(a)
[1] 5 6
as.array(b)
[1] 2 3
# There are other versions of assign

as.array(a$assign_add(c(2,3))) # c(7, 9)
[1] 7 9
as.array(a$assign_sub(c(7,9))) # c(0, 0)
[1] 0 0

Lifecycles, naming, and watching

In TensorFlow, tf$Variable instance have the same lifecycle as other R objects. When there are no references to a variable it is automatically deallocated (garbage-collected).

Variables can also be named which can help you track and debug them. You can give two variables the same name.

# Create a and b; they will have the same name but will be backed by
# different tensors.

a <- tf$Variable(my_tensor, name = "Mark")
# A new variable with the same name, but different value

# Note that the scalar add `+` is broadcast
b <- tf$Variable(my_tensor + 1, name = "Mark")

# These are elementwise-unequal, despite having the same name
print(a == b)
tf.Tensor(
[[False False]
 [False False]], shape=(2, 2), dtype=bool)

Variable names are preserved when saving and loading models. By default, variables in models will acquire unique variable names automatically, so you don’t need to assign them yourself unless you want to.

Although variables are important for differentiation, some variables will not need to be differentiated. You can turn off gradients for a variable by setting trainable to false at creation. An example of a variable that would not need gradients is a training step counter.

(step_counter <- tf$Variable(1L, trainable = FALSE))
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1>

Placing variables and tensors

For better performance, TensorFlow will attempt to place tensors and variables on the fastest device compatible with its dtype. This means most variables are placed on a GPU if one is available.

However, you can override this. In this snippet, place a float tensor and a variable on the CPU, even if a GPU is available. By turning on device placement logging (see above), you can see where the variable is placed.

Note: Although manual placement works, using distribution strategies can be a more convenient and scalable way to optimize your computation.

If you run this notebook on different backends with and without a GPU you will see different logging. Note that logging device placement must be turned on at the start of the session.

with(tf$device('CPU:0'), {
  # Create some tensors
  a <- tf$Variable(array(1:6, c(2, 3)), dtype = "float32")
  b <- as_tensor(array(1:6, c(3, 2)), dtype = "float32")
  c <- tf$matmul(a, b)
})

c
tf.Tensor(
[[22. 49.]
 [28. 64.]], shape=(2, 2), dtype=float32)

It’s possible to set the location of a variable or tensor on one device and do the computation on another device. This will introduce delay, as data needs to be copied between the devices.

You might do this, however, if you had multiple GPU workers but only want one copy of the variables.

with(tf$device('CPU:0'), {
  a <- tf$Variable(array(1:6, c(2, 3)), dtype = "float32")
  b <- tf$Variable(array(1:3, c(1, 3)), dtype = "float32")
})

with(tf$device('GPU:0'), {
  # Element-wise multiply
  k <- a * b
})

k
tf.Tensor(
[[ 1.  6. 15.]
 [ 2.  8. 18.]], shape=(2, 3), dtype=float32)

Note: Because tf$config$set_soft_device_placement() is turned on by default, even if you run this code on a device without a GPU, it will still run. The multiplication step will happen on the CPU.

For more on distributed training, refer to the guide.

Next steps

To understand how variables are typically used, see our guide on automatic differentiation.

Environment Details

tensorflow::tf_config()
TensorFlow v2.9.1 (~/.virtualenvs/r-tensorflow-site/lib/python3.9/site-packages/tensorflow)
Python v3.9 (~/.virtualenvs/r-tensorflow-site/bin/python)
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tensorflow_2.9.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9           here_1.0.1           lattice_0.20-45     
 [4] png_0.1-7            rprojroot_2.0.3      digest_0.6.29       
 [7] grid_4.2.1           jsonlite_1.8.0       magrittr_2.0.3      
[10] evaluate_0.15        tfruns_1.5.0         rlang_1.0.4         
[13] stringi_1.7.8        cli_3.3.0            whisker_0.4         
[16] Matrix_1.4-1         reticulate_1.25-9000 rmarkdown_2.14      
[19] tools_4.2.1          stringr_1.4.0        htmlwidgets_1.5.4   
[22] xfun_0.31            yaml_2.3.5           fastmap_1.1.0       
[25] compiler_4.2.1       base64enc_0.1-3      htmltools_0.5.2     
[28] knitr_1.39          
system2(reticulate::py_exe(), c("-m pip freeze"), stdout = TRUE) |> writeLines()
absl-py==1.1.0
asttokens==2.0.5
astunparse==1.6.3
backcall==0.2.0
cachetools==5.2.0
certifi==2022.6.15
charset-normalizer==2.1.0
decorator==5.1.1
dill==0.3.5.1
etils==0.6.0
executing==0.8.3
flatbuffers==1.12
gast==0.4.0
google-auth==2.9.0
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
googleapis-common-protos==1.56.4
grpcio==1.47.0
h5py==3.7.0
idna==3.3
importlib-metadata==4.12.0
importlib-resources==5.8.0
ipython==8.4.0
jedi==0.18.1
keras==2.9.0
Keras-Preprocessing==1.1.2
keras-tuner==1.1.2
kt-legacy==1.0.4
libclang==14.0.1
Markdown==3.3.7
matplotlib-inline==0.1.3
numpy==1.23.1
oauthlib==3.2.0
opt-einsum==3.3.0
packaging==21.3
pandas==1.4.3
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.2.0
promise==2.3
prompt-toolkit==3.0.30
protobuf==3.19.4
ptyprocess==0.7.0
pure-eval==0.2.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pydot==1.4.2
Pygments==2.12.0
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
requests==2.28.1
requests-oauthlib==1.3.1
rsa==4.8
scipy==1.8.1
six==1.16.0
stack-data==0.3.0
tensorboard==2.9.1
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.9.1
tensorflow-datasets==4.6.0
tensorflow-estimator==2.9.0
tensorflow-hub==0.12.0
tensorflow-io-gcs-filesystem==0.26.0
tensorflow-metadata==1.9.0
termcolor==1.1.0
toml==0.10.2
tqdm==4.64.0
traitlets==5.3.0
typing_extensions==4.3.0
urllib3==1.26.10
wcwidth==0.2.5
Werkzeug==2.1.2
wrapt==1.14.1
zipp==3.8.1
TF Devices:
-  PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU') 
-  PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU') 
CPU cores: 12 
Date rendered: 2022-07-14 
Page render time: 3 seconds