'Tensorflow TypeError: Fetch argument None has invalid type <type 'N

a=tf.Variable(0, name='input')
b=tf.constant(1)

mid_val =tf.add(a,b)
update_value =tf.compat.v1.assign(a,mid_val)
tg=initialize_all_variables()


with tf.compat.v1.Session() as sess:
  sess.run(tg)
  print(sess.run(a))

  for i in range(3):
    sess.run(update_value)
    print(sess.run(a))

It is giving me error in sess.run(tg) and raising a type error and on running it is giving following error:

<ipython-input-20-fd2283cdd3bd> in <module>()
     10 
     11 with tf.compat.v1.Session() as sess:
---> 12   sess.run(tg)
     13   print(sess.run(a))
     14 

3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    263     """
    264     if fetch is None:
--> 265       raise TypeError(f'Argument `fetch` = {fetch} has invalid type '
    266                       f'"{type(fetch).__name__}". Cannot be None')
    267     elif isinstance(fetch, (list, tuple)):

What should I do?



Solution 1:[1]

You are running graph mode operations in eager execution mode (later versions of TF 2.x) which causes this error.

You need to disable eager execution mode to run the above code in TF 2.x as shown below:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

a=tf.Variable(0, name='input')
b=tf.constant(1)

mid_val =tf.add(a,b)
update_value =tf.compat.v1.assign(a,mid_val)
tg=tf.compat.v1.initialize_all_variables()   #add tf.compat.v1 to initialize_all_variables() function


with tf.compat.v1.Session() as sess:
  sess.run(tg)
  print(sess.run(a))

  for i in range(3):
    sess.run(update_value)
    print(sess.run(a))

Output:

0
1
2
3

Or can use %tensorflow_version 1.x to convert TensorFlow as in graph mode before running the above code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer2