To hold the model weights, TensorFlow uses Variable instances. In our Keras example, we can list the content of the model by accessing model.variables. It will return the list of all variables contained in our model:
print([variable.name for variable in model.variables])
# Prints ['sequential/dense/kernel:0', 'sequential/dense/bias:0', 'sequential/dense_1/kernel:0', 'sequential/dense_1/bias:0']
In our example, variable management (including naming) has been entirely handled by Keras. As we saw earlier, it is also possible to create our own variables:
a = tf.Variable(3, name='my_var')
print(a) # Prints <tf.Variable 'my_var:0' shape=() dtype=int32, numpy=3>
Note that for large projects, it is recommended to name variables to clarify the code and ease debugging. To change the value of a variable, use the Variable.assign method:
a.assign(a + 1)
print(a.numpy()) # Prints 4
Failing to use the .assign()...