[英]ImportError: cannot import name 'Session' from 'tensorflow'
I'm struggle with running module.我正在努力运行模块。
from tensorflow import Session, ConfigProto, GPUOptions
gpuoptions = GPUOptions(allow_growth=True)
session = Session(config=ConfigProto(gpu_options=gpuoptions))
K.set_session(session)
classifier = Sequential()
I don't know why it's not working.我不知道为什么它不起作用。
It just shows me:它只是告诉我:
ImportError: cannot import name 'Session' from 'tensorflow' (C:\Users\hayou\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py)
I guess you're using TensorFlow 2.x.我猜你正在使用 TensorFlow 2.x。 In that case, use tf.compat.v1.--function--() instead.在这种情况下,请改用 tf.compat.v1.--function--() 。
import tensorflow as tf
gpuoptions = tf.compat.v1.GPUOptions(allow_growth=True)
session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpuoptions))
K.set_session(session)
classifier = Sequential()
In my case I was using tensorflow v2.2
which doesn't have Session .就我而言,我使用的是没有Session 的tensorflow v2.2
。 However it has been present in previous versions, so you might want to install an older version of tensorflow like v1.15
which contains Session class.然而,它已经出现在以前的版本中,所以你可能想要安装一个旧版本的 tensorflow,比如包含Session类的v1.15
。
pip install tensorflow==1.15
I'm not exactly sure this is the best solution, but it worked for me, so I hope it does work for you as well!我不确定这是最好的解决方案,但它对我有用,所以我希望它也对你有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.