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 8 of 100

What is a TensorFlow Session (in TensorFlow 1.x) or a TensorFlow Function (in TensorFlow 2.x), and why are they needed?

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

In TensorFlow, a session is an object that represents the environment in which a computational graph is executed. In TensorFlow 1.x, sessions are used to run the computational graph and perform computations on the tensors. In TensorFlow 2.x, functions replace sessions as the way to execute a graph.

Here’s how sessions and functions work in TensorFlow:

Sessions in TensorFlow 1.x: In TensorFlow 1.x, a session is created using the tf.Session() function. Once a session is created, the computational graph can be run by calling the session.run() method and passing in the tensors to be computed.

For example, the following code defines a simple graph and runs it in a session:

    import tensorflow as tf
    
    # Define a computational graph
    a = tf.constant(2)
    b = tf.constant(3)
    c = tf.add(a, b)
    
    # Create a session and run the graph
    with tf.Session() as sess:
        result = sess.run(c)
        print(result)

Functions in TensorFlow 2.x: In TensorFlow 2.x, functions replace sessions as the way to execute a graph. A function is created using the tf.function() decorator, which compiles a TensorFlow graph into a callable Python function. The resulting function can be called like any other Python function, and it will run the graph and return the result.

For example, the following code defines a simple graph and runs it as a function in TensorFlow 2.x:

    import tensorflow as tf
    
    # Define a computational graph
    @tf.function
    def add(a, b):
        return tf.add(a, b)
    
    # Run the graph as a function
    result = add(2, 3)
    print(result)

Sessions and functions are needed in TensorFlow because they provide the environment for executing a computational graph. They allow developers to define complex models and computations, and then execute them efficiently using TensorFlow’s optimized graph execution engine. Sessions and functions also provide a way to manage the state of the graph, including the values of variables, which can be updated during computation. By encapsulating the graph and its state in a session or function, TensorFlow provides a powerful and flexible platform for building and training machine learning models.

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