module 'Tensorflow’ has no atrribute 'Session’解決方案
來,上錯誤:
錯誤原因:
版本不符,tensorflow2.0版本問題
解決方案:
Tensorflow Core r2.0默認情況下已啓用急切執行。因此可使用tf.compat.v1.Session()和 run()函數。
有兩種表示形式:
1.常規的:Session()、run()、close()
import tensorflow as tf
#構建圖
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
#啓動默認圖
sess = tf.compat.v1.Session()
#加入節點,提取結果
result = sess.run(product)
print(result)
sess.close()
運行結果:[[12.]]
2、with代碼塊
import tensorflow as tf
#使用with代碼塊
with tf.compat.v1.Session() as ses:
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a+b
print(ses.run(c))
運行結果:11.0