Search⌘ K

Defining Variables and Output in TensorFlow

Explore how to define mutable variables in TensorFlow with specific shapes, initial values, and data types. Understand variable naming conventions and how to create outputs derived from tensors, enabling you to manage model parameters and computational graphs efficiently.

Defining variables in TensorFlow

Variables play an important role in TensorFlow. A variable is essentially a tensor with a specific shape defining how many dimensions the variable will have and the size of each dimension. However, unlike a regular TensorFlow tensor, variables are mutable, meaning that the value of the variables can change after they are defined. This is an ideal property to have to implement the parameters of a learning model (for example, neural network weights), where the weights change slightly after each step of learning. For example, if we define a variable with x = tf.Variable(0,dtype=tf.int32), we can change the value of that variable using a TensorFlow operation such as tf.assign(x,x+1). However, if we define a tensor such as x = tf.constant(0,dtype=tf.int32), we can’t change the value of the tensor as we could for a variable. It should stay 0 until the end of the program execution.

Variable creation is quite simple. In our sigmoid ...