tf.keras.backend.get_value

TensorFlow 1 version View source on GitHub

Returns the value of a variable.

tf.keras.backend.get_value(
    x
)

Used in the notebooks

Used in the guide

backend.get_value is the compliment of backend.set_value, and provides a generic interface for reading from variables while abstracting away the differences between TensorFlow 1.x and 2.x semantics.

K = tf.keras.backend  # Common keras convention 
v = K.variable(1.) 
# reassign 
K.set_value(v, 2.) 
print(K.get_value(v)) 
2.0 
# increment 
K.set_value(v, K.get_value(v) + 1) 
print(K.get_value(v)) 
3.0 

Variable semantics in TensorFlow 2 are eager execution friendly. The above code is roughly equivalent to:

v = tf.Variable(1.) 
_ = v.assign(2.) 
print(v.numpy()) 
2.0 
_ = v.assign_add(1.) 
print(v.numpy()) 
3.0 

Arguments:

  • x: input variable.

Returns:

A Numpy array.