1

I am having issue with Session module.

import tensorflow as tf
n1 = tf.constant(3.0)
n2 = tf.constant(4.0)
print(n1)
sess = tf.Session()
print(sess.run([n1,n2]))

below is the console output I have received.

runfile('C:/Users/Rushabh Shah/.spyder-py3/temp.py', wdir='C:/Users/Rushabh Shah/.spyder-py3')
tf.Tensor(3.0, shape=(), dtype=float32)
Traceback (most recent call last):

  File "C:\Users\Rushabh Shah\.spyder-py3\temp.py", line 11, in <module>
    sess = tf.Session()

AttributeError: module 'tensorflow' has no attribute 'Session'
André
  • 1,034
  • 9
  • 19
Rushabh Shah
  • 75
  • 1
  • 2
  • 14
  • Take a look at https://stackoverflow.com/questions/55142951/tensorflow-2-0-attributeerror-module-tensorflow-has-no-attribute-session. Not sure what your version is, but maybe this can help. – Saucy Goat Dec 14 '19 at 15:41
  • Yes this is useful. But after this I am getting new error as below – Rushabh Shah Dec 14 '19 at 15:45
  • tf.Tensor(3.0, shape=(), dtype=float32)Traceback (most recent call last): File "C:\Users\Rushabh Shah\.spyder-py3\temp.py", line 12, in print(sess.run('Hello')) File "G:\Rushabh_Shah\envs\tf_env\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run run_metadata_ptr) File "G:\Rushabh_Shah\envs\tf_env\lib\site-packages\tensorflow_core\python\client\session.py", line 1105, in _run raise RuntimeError('The Session graph is empty. Add operations to the ' RuntimeError: The Session graph is empty. Add operations to the graph before calling run(). – Rushabh Shah Dec 14 '19 at 15:48
  • Take a look at https://stackoverflow.com/questions/58366789/tensorflow-the-session-graph-is-empty-python – Saucy Goat Dec 14 '19 at 15:49

2 Answers2

2

If you are using Tensorflow 2.0 you should use sess = tf.compat.v1.Session() instead of sess = tf.Session().

André
  • 1,034
  • 9
  • 19
Mr Coder
  • 507
  • 3
  • 13
1

change sess = tf.Session() to sess = tf.compat.v1.Session() , it will work.

source : https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session

Priyamvada
  • 216
  • 3
  • 6