AttributeError: understanding module 'tensorflow' has no attribute 'Session'

Asked 2 years ago, Updated 2 years ago, 68 views

Running the following code

AttributeError: module 'tensorflow' has no attribute 'Session'

appears.Why is that?

import tensorflow as tf  
sess=tf.Session()
hello=tf.constant('Hello')
print(sess.run(hello))

python python3 tensorflow

2022-09-30 16:45

1 Answers

Tensorflow 2.0 does not use tf.Session or tf.placeholder.
https://www.tensorflow.org/guide/migrate

Every v1.Session.run call should be replaced by aPython function.
- The feed_dict and v1.placeholdersbecomefunction arguments.
- The fetches become the function's return value.
- During conversion eager execution allow easy debugging with standard Python tools like pdb.
After that add a tf.functiondecorator to make it efficiently in graph.

Instead of Session, run it as a normal function of python.

import tensorflow as tf
from tensorflow.keras.backend import event

def example(x,c):
    return c

hello=tf.constant('Hello')
f=tf.function(example)
print(eval(f([], hello)))

It seems that what we were passing in feed_dict in v1 was changed to pass in the function argument (in this case x).However, the function is created so that the value of x (placeholder in v1) passed as an argument is calculated with other constants (tf.constant), etc., and the return value is output.The change in v2 is that placeholder has changed to an argument for a function

If you really want to see the contents of tensors such as constants, you can see them using the keras backend eval.Therefore, if you just want to evaluate the value of a constant, you can do it by eval(hello) on the keras backend without creating a function.


2022-09-30 16:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.