WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TensorFlow · Basic · question 7 of 100

What is a TensorFlow Variable, and how is it different from a Tensor?

📕 Buy this interview preparation book: 100 TensorFlow questions & answers — PDF + EPUB for $5

In TensorFlow, a variable is a special type of tensor that holds a mutable value that can be changed during computation. Unlike regular tensors, which are immutable, variables can be updated and modified as the computational graph is executed.

Here are some key differences between a TensorFlow variable and a tensor:

Initialization: A TensorFlow variable must be initialized with an initial value, which can be a tensor or a constant. For example, the following code initializes a variable with the value [1, 2, 3]:

    import tensorflow as tf
    
    variable = tf.Variable([1, 2, 3])

Updating: A TensorFlow variable can be updated using operations like assign() or assign_add(). For example, the following code updates a variable by adding 1 to its value:

    update_op = variable.assign_add([1, 1, 1])

Scope: A TensorFlow variable is scoped to the graph that it is defined in, and can only be accessed within that scope. For example, the following code defines a variable within a scope:

    import tensorflow as tf
    
    with tf.variable_scope("my_scope"):
        variable = tf.Variable([1, 2, 3])

Saving and Restoring: A TensorFlow variable can be saved and restored using the Saver object. For example, the following code saves a variable to a checkpoint file:

    saver = tf.train.Saver()
    saver.save(sess, "/tmp/model.ckpt")

Overall, a TensorFlow variable is a useful tool for representing mutable state in a computational graph. It can be used to store model parameters that need to be updated during training, or to represent other types of mutable data that are used during computation. In contrast, a tensor represents an immutable value that is used as input or output to operations in the computational graph.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TensorFlow interview — then scores it.
📞 Practice TensorFlow — free 15 min
📕 Buy this interview preparation book: 100 TensorFlow questions & answers — PDF + EPUB for $5

All 100 TensorFlow questions · All topics