Understand TensorFlow sess.run(): A Beginner Introduction – TensorFlow Tutorial

By | May 2, 2020

In tensorflow, we often use sess.run() to call operations or calculate the value of a tensor. However, there are some tips you should notice when you are using it. In this tutorial, we will use some examples to discuss these tips.

Syntax of sess.run()


It will run operations and evaluate tensors in fetches.

The return value of sess.run

We must notice its return value.

  • If fetches is a tensor, it will return a single value.
  • If fetches is a list, it will return a list.

For example:

import tensorflow as tf
import numpy as np

graph = tf.Graph()
with graph.as_default() as g:
    w1 = tf.Variable(np.array([1,2], dtype = np.float32))
    w2 = tf.Variable(np.array([2,2], dtype = np.float32))
    wx = tf.multiply(w1, w2)  
    initialize = tf.global_variables_initializer()

with tf.Session(graph=graph) as sess:
    wx_v= sess.run([wx])

In this example, we will compute the value of tensor wx. A python list is called by sess.run(). Run this code, you will get the result:

[array([2., 4.], dtype=float32)]

From the result, we can find the return value wx_v is a python list, which contains the value of wx.

Moreover, if a tensor is called by sess.run(). What is the result?

with tf.Session(graph=graph) as sess:
    wx_v= sess.run(wx)

Run this code, we will get the result:

[2. 4.]

The data type of wx_v is not a python list, it is the real value of wx.

Leave a Reply

Your email address will not be published. Required fields are marked *