SciTech-BigDataAIML-Tensorflow-Variables

发布时间 2024-01-01 19:33:57作者: abaelhe

Lifecycles, naming, and watching

  • tf.Variable instance have the same lifecycle as other Python objects in Python-based TensorFlow,
    When there are no references to a variable it is automatically deallocated.

  • 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)
  • 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.

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