当我在Tensorflow 2.0环境中执行命令sess = tf.Session()时,我得到了一个错误消息,如下所示:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
系统信息:
操作系统平台及发行版本:Windows 10
Python版本:3.7.1
Tensorflow版本:2.0.0-alpha0(已安装pip)
复制步骤:
安装:
PIP安装——升级PIP
PIP install tensorflow==2.0.0-alpha0
PIP安装keras
PIP install numpy==1.16.2
执行:
执行命令:import tensorflow as tf
执行命令:sess = tf.Session()
我在安装windows10 + python3.7(64bit) + anacconda3 + jupyter笔记本电脑后第一次尝试python时遇到了这个问题。
我通过参考“https://vispud.blogspot.com/2019/05/tensorflow200a0-attributeerror-module.html”解决了这个问题
我同意
我相信“Session()”已经在TF 2.0中被删除了。
我插入了两行。一个是tf. compat_v1 .disable_eager_execution()另一个是sess = tf. compat_v1 . session ()
我的Hello.py如下:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))
TF2在默认情况下运行Eager Execution,从而消除了对session的需求。如果你想运行静态图形,更合适的方法是在TF2中使用tf.function()。虽然在TF2中仍然可以通过tf. compatat .v1.Session()访问Session,但我不鼓励使用它。通过比较hello worlds中的差异可能有助于演示这种差异:
TF1。X你好世界:
import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(msg))
TF2。X你好世界:
import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)
更多信息请参见Effective TensorFlow 2
根据TF 1:1符号映射,在TF 2.0中您应该使用TF . compatat .v1. session()而不是TF . session ()
https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0
得到TF 1。在TF 2.0中可以运行类似x的行为
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但这样就不能从TF 2.0的许多改进中获益。更多细节请参考迁移指南
https://www.tensorflow.org/guide/migrate
如果你在做这件事的时候,
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
然后我建议你遵循这些步骤,注意:只适用于TensorFlow2和CPU进程
步骤1:告诉你的代码就像编译器是TF1一样,并禁用TF2行为,使用以下代码:
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
步骤2:当导入库时,提醒你的代码必须像TF1一样,是的,每一次。
tf.disable_v2_behavior()
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
结论:这应该工作,让我知道如果有什么问题,也如果它是GPU,然后提到添加keras后端代码。此外,TF2不支持会话,对此有单独的理解,TensorFlow上已经提到过,链接是:
TensorFlow TF2中使用会话的页面
其他主要的TF2变化已经在这个链接中提到,它很长,但请通过它,使用Ctrl+F协助。链接,
Effective TensorFlow 2 Page Link
运行TF 1并不像你想的那么容易。x和TF 2。x环境我发现了一些错误,需要审查一些变量的使用,当我在互联网上修复神经元网络的问题。转换为TF 2。X是更好的主意。
(更容易适应)
TF - 2。X
while not done:
next_obs, reward, done, info = env.step(action)
env.render()
img = tf.keras.preprocessing.image.array_to_img(
img,
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
predictions = model_self_1.predict(img_array) ### Prediction
### Training: history_highscores = model_highscores.fit(batched_features, epochs=1 ,validation_data=(dataset.shuffle(10))) # epochs=500 # , callbacks=[cp_callback, tb_callback]
TF - 1。X
with tf.compat.v1.Session() as sess:
saver = tf.compat.v1.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(savedir + '\\invader_001'))
train_loss, _ = sess.run([loss, training_op], feed_dict={X:o_obs, y:y_batch, X_action:o_act})
for layer in mainQ_outputs:
model.add(layer)
model.add(tf.keras.layers.Flatten() )
model.add(tf.keras.layers.Dense(6, activation=tf.nn.softmax))
predictions = model.predict(obs) ### Prediction
### Training: summ = sess.run(summaries, feed_dict={X:o_obs, y:y_batch, X_action:o_act})
我在安装windows10 + python3.7(64bit) + anacconda3 + jupyter笔记本电脑后第一次尝试python时遇到了这个问题。
我通过参考“https://vispud.blogspot.com/2019/05/tensorflow200a0-attributeerror-module.html”解决了这个问题
我同意
我相信“Session()”已经在TF 2.0中被删除了。
我插入了两行。一个是tf. compat_v1 .disable_eager_execution()另一个是sess = tf. compat_v1 . session ()
我的Hello.py如下:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.compat.v1.Session()
print(sess.run(hello))