首页 > 解决方案 > 模块“张量流”没有属性“会话”无法解析

问题描述

请帮帮我!我解决不了!

所以......我的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'

我了解tf.Session()不支持在tfdeeplearning 环境中tensorflow 2.2.0运行该行后我拥有的版本。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'

所以,我没有选择

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

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

标签: pythontensorflowjupyter-notebook

解决方案


安装 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 执行上运行。不需要通过调用会话来创建图形。您仍然可以tf.compat.v1.Session()在 TF2中访问会话


推荐阅读