Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'

前端 未结 10 1241
小鲜肉
小鲜肉 2020-12-02 05:41

When I am executing the command sess = tf.Session() in Tensorflow 2.0 environment, I am getting an error message as below:

Traceback (most recent         


        
10条回答
  •  粉色の甜心
    2020-12-02 06:18

    TF2 runs Eager Execution by default, thus removing the need for Sessions. If you want to run static graphs, the more proper way is to use tf.function() in TF2. While Session can still be accessed via tf.compat.v1.Session() in TF2, I would discourage using it. It may be helpful to demonstrate this difference by comparing the difference in hello worlds:

    TF1.x hello world:

    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(msg))
    

    TF2.x hello world:

    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    tf.print(msg)
    

    For more info, see Effective TensorFlow 2

提交回复
热议问题