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

PyTorch · Intermediate · question 21 of 100

Explain the concept of dynamic computation graphs in PyTorch and how they differ from static computation graphs in TensorFlow.?

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

Dynamic computation graphs in PyTorch and static computation graphs in TensorFlow are two different approaches to building and executing deep learning models.

A computation graph is a way of representing a mathematical function with a directed graph that shows the flow of data through the function. In deep learning, computation graphs are used to represent the flow of data through neural networks.

In TensorFlow, computation graphs are static, meaning that the graph is defined once and then executed repeatedly. This has the advantage of allowing for optimizations to be made to the graph before execution, which can improve performance. However, it also means that the graph cannot be changed during execution.

In contrast, PyTorch uses dynamic computation graphs, which means that the graph is built on-the-fly during execution. Every time a function is called, a node is added to the graph representing that function and its inputs. This allows for greater flexibility in building and modifying models, as the graph can be changed on-the-fly as needed.

For example, consider the following PyTorch code:

import torch

x = torch.tensor(1.0, requires_grad=True)
y = torch.tensor(2.0, requires_grad=True)
z = x + y
z.backward()
print(x.grad)

This code creates two tensors ‘x‘ and ‘y‘, and then adds them together to create ‘z‘. It then calls ‘backward()‘ on ‘z‘ to compute the gradients with respect to ‘x‘ and ‘y‘. When ‘backward()‘ is called, PyTorch builds a computation graph on-the-fly, adding nodes for ‘x‘, ‘y‘, and ‘z‘, and then computes the derivatives using automatic differentiation.

In TensorFlow, the equivalent code would look something like this:

import tensorflow as tf

x = tf.Variable(1.0)
y = tf.Variable(2.0)
z = x + y
grads = tf.gradients(z, [x, y])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(grads)

This code creates two variables ‘x‘ and ‘y‘, and then adds them together to create ‘z‘. It then calls ‘gradients()‘ on ‘z‘ to compute the derivatives with respect to ‘x‘ and ‘y‘. The computation graph is defined statically, so the code must create a ‘Session‘ object and initialize the variables before executing the graph.

In summary, dynamic computation graphs in PyTorch allow for greater flexibility in building and modifying models, but may be less efficient than static computation graphs in TensorFlow, which allow for greater optimization opportunities.

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

All 100 PyTorch questions · All topics