赞
踩
首先安装python。
可以选择不同的版本:
这里选择的是python3.6.7
Python Release Python 3.6.7 | Python.org
选择Windows x86-64 executable installer 安装程序。
下载得到:
然后进行安装。
安装时把下面的add to path勾选上。
然后就好了。
如果之前已安装有别的版本的python,在系统环境变量里将python36上移到前面。
接下来安装TensorFlow:
在命令提示符窗口里输入 pip3 install TensorFlow==2.1.0
然后你会发现最下面会有这么一段话:
protobuf requires Python '>=3.7' but the running Python is 3.6.7
You are using pip version 10.0.1, however version 21.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
输入pip3 list 发现TensorFlow并没有安装好。
所有,接下来,输入 python -m pip install --upgrade pip
然后再输入 pip3 install TensorFlow==2.1.0
最后你发现,这个就安装好了:
Successfully installed TensorFlow-2.1.0 absl-py-1.4.0 astor-0.8.1 cached-property-1.5.2 cachetools-4.2.4 certifi-2022.12.7 charset-normalizer-2.0.12 dataclasses-0.8 gast-0.2.2 google-auth-1.35.0 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.48.2 h5py-3.1.0 idna-3.4 importlib-metadata-4.8.3 keras-applications-1.0.8 keras-preprocessing-1.1.2 markdown-3.3.7 numpy-1.19.5 oauthlib-3.2.2 opt-einsum-3.3.0 protobuf-3.19.6 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.27.1 requests-oauthlib-1.3.1 rsa-4.9 scipy-1.4.1 setuptools-59.6.0 six-1.16.0 tensorboard-2.1.1 tensorflow-estimator-2.1.0 termcolor-1.1.0 typing-extensions-4.1.1 urllib3-1.26.15 werkzeug-2.0.3 wheel-0.37.1 wrapt-1.15.0 zipp-3.6.0
输入 pip show tensorflow 查看:
在pycharm中使用代码测试:
- import tensorflow as tf
- hello = tf.constant('Hello, Tensorflow!')
- sess = tf.Session()
-
- print(sess.run(hello))
嗯,可能出现下面的情况:
sess = tf.Session() AttributeError: module 'tensorflow' has no attribute 'Session'
怎么办?
将代码 sess = tf.Session() 修改为
sess = tf.compat.v1.Session()
在Tensorflow2版本中使用 sess=tf.Session() 会出现这个问题并不是安装错误,而是因为在新的Tensorflow 2版本中已经移除了Session这一模块。
修改后再运行:
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
再加上 tf.compat.v1.disable_eager_execution()
- import tensorflow as tf
- tf.compat.v1.disable_eager_execution()
- hello = tf.constant('Hello, Tensorflow!')
- #sess = tf.Session()
- sess = tf.compat.v1.Session()
- print(sess.run(hello))
最后运行结果:
看到没,最后运行出来了。
安装TensorFlow1版本应该不会这样报错。
附:
tensorflow1.0版本的测试代码:
- import tensorflow as tf
- hello = tf.constant('hello,tensorflow')
- sess=tf.Session()
- print(sess.run(hello))
tensorflow2.0版本的测试代码:
- import tensorflow as tf
- tf.compat.v1.disable_eager_execution()
- hello = tf.constant('hello,tensorflow')
- sess = tf.compat.v1.Session()
- print(sess.run(hello))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。