【问题标题】:module 'tensorflow' has no attribute 'Session' cannot resolve模块“tensorflow”没有属性“会话”无法解析
【发布时间】:2020-07-26 21:25:34
【问题描述】:

请帮帮我!我无法解决!

所以...我的 python 版本是 Python 3.5.6,Anaconda 版本是 Conda 4.8.3。我正在使用 Jupyter Notebook。

我在 MacOS 中工作。

我已经创建了一个环境 tfdeeplearning:

conda create -n tfdeeplearning python=3.5

我已经安装了以下内容:

1. conda install jupyter
2. conda install numpy
3. conda install pandas
4. conda install scikit-learn
5. conda install matplotlib
6. pip install --upgrade tensorflow 

所以,在 Jupyter Notebook 中,我正在尝试运行此代码:

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

我经常收到此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-f75057d1d95f> in <module>()
----> 1 sess = tf.Session()

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

我了解到tensorflow 2.2.0 不支持tf.Session(),这是我在 tfdeeplearning 环境中运行pip install --upgrade tensorflow 后的版本。

我也试过这样做:

pip install tensorflow==1.4.0

这破坏了环境 tfdeeplearning,我不得不重置环境。

我试过了:

pip uninstall tensorflow-gpu
pip install tensorflow-gpu

这又破坏了环境,我不得不重新设置它。

然后我在 jupyter notebooks 中尝试了这个,它正在替换 import tensorflow as tf:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

这也不起作用,因为这次错误说

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

所以,我没有选择

附:这是我在 conda 中称为 tfdeeplearning 的虚拟环境中的所有依赖项的列表,如果它有帮助的话:

absl-py==0.9.0
appnope==0.1.0
astunparse==1.6.3
bleach==3.1.5
cachetools==4.1.1
certifi==2018.8.24
chardet==3.0.4
cycler==0.10.0
Cython==0.29.21
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.2.3
gast==0.3.3
google-auth==1.19.2
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.30.0
h5py==2.10.0
idna==2.10
importlib-metadata==1.7.0
ipykernel==4.10.0
ipython==5.8.0
ipython-genutils==0.2.0
ipywidgets==7.4.1
Jinja2==2.11.2
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.3.3
jupyter-console==5.2.0
jupyter-core==4.5.0
Keras-Preprocessing==1.1.2
kiwisolver==1.0.1
Markdown==3.2.2
MarkupSafe==1.0
matplotlib==3.0.0
mistune==0.8.3
mkl-fft==1.0.6
mkl-random==1.0.1
nbconvert==5.5.0
nbformat==5.0.7
notebook==5.6.0
numpy==1.18.5
oauthlib==3.1.0
opt-einsum==3.3.0
packaging==20.4
pandas==0.22.0
pandocfilters==1.4.2
pexpect==4.6.0
pickleshare==0.7.4
prometheus-client==0.8.0
prompt-toolkit==1.0.15
protobuf==3.12.2
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
Pygments==2.6.1
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.1
pyzmq==17.1.2
qtconsole==4.7.5
QtPy==1.9.0
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scikit-learn==0.20.0
scipy==1.4.1
Send2Trash==1.5.0
simplegeneric==0.8.1
six==1.15.0
TBB==0.1
tensorboard==2.2.2
tensorboard-plugin-wit==1.7.0
tensorflow==2.2.0
tensorflow-estimator==2.2.0
termcolor==1.1.0
terminado==0.8.1
testpath==0.4.4
tornado==5.1.1
traitlets==4.3.2
urllib3==1.25.10
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==1.0.1
widgetsnbextension==3.4.1
wrapt==1.12.1
zipp==1.2.0

【问题讨论】:

    标签: python tensorflow jupyter-notebook


    【解决方案1】:

    安装 TensorFlow 1.15.3。

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

    Tensorflow 2.x 默认在 Eager 执行上运行。不需要通过调用会话来创建图形。你仍然可以在 TF2 中通过tf.compat.v1.Session() 访问会话

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2020-07-02
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2020-02-04
      相关资源
      最近更新 更多