Ubuntu16.04 虚拟环境中安装 TensorFlow 2.0(CPU)
第一步——TensorFlow 2.0 系统要求
第二步——设置虚拟环境
参考:https://blog.csdn.net/frozennet/article/details/105420207
第三步——安装TensorFlow 2.0
新建工程文件夹
$ mkdir ~/tf-demo
进入工程文件夹
$ cd ~/tf-demo
新建虚拟环境
$ python3 -m venv tensorflow-dev
启动虚拟环境
$ source tensorflow-dev/bin/activate
关闭虚拟环境
$ deactivate
安装TensorFlow 2.0,非GPU版本
$ pip3 install tensorflow==2.0.0-alpha0
出现的错误
错误描述:
ERROR: markdown 3.1.1 has requirement setuptools>=36, but you’ll have setuptools 20.7.0 which is incompatible.
原因:
setuptools版本不匹配
解决方法:
(1)卸载原有的setuptools
$ sudo apt-get remove python-setuptools
(2)卸载setuptools依赖的软件包
$ sudo apt-get autoremove
(3)重新安装其它版本setuptools
$ sudo pip install setuptools==39.0.0
参考:
关于cleaning up with apt-get:https://www.networkworld.com/article/3453032/cleaning-up-with-apt-get.html
关于解决错误:https://blog.csdn.net/chengyq116/article/details/93654175
第四步——验证安装
新建hello.py文件
$ vim 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))
参考:http://www.tensorfly.cn/tfdoc/get_started/os_setup.html#virtualenv_install
运行.py文件
$ python3 hello.py
过程输出信息
过程描述:
执行
python
导入tensorflow 模块
import tensorflow as tf
输出信息
FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
原因:安装numpy的版本过高
解决方法:安装低版本的numpy
pip install -U numpy==1.16.0
再次,导入tensorflow 模块,未出现错误信息
参考: https://blog.csdn.net/kobe_academy/article/details/99706595
不同的系统,输出信息不同
2020-04-10 03:26:55.418775: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2020-04-10 03:26:55.427746: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2400000000 Hz 2020-04-10 03:26:55.428398: I tensorflow/compiler/xla/service/service.cc:162] XLA service 0x35f5630 executing computations on platform Host. Devices: 2020-04-10 03:26:55.428458: I tensorflow/compiler/xla/service/service.cc:169] StreamExecutor device (0): <undefined>, <undefined>
这表明您拥有一个可以通过TensorFlow进行优化以提高性能的指令集。如果看到此消息,则可以忽略它并继续。
理论输出
Hello TensorFlow
实际输出
b'Hello TensorFlow'
出现错误
错误描述:错误输出
b'Hello TensorFlow'
原因:
Tensorflow将字符串表示为Bytes对象. This ‘b’ is Python’s way of marking Bytes objects
解决方法:
通过应用.decode()将session.run()返回的Bytes对象转换为常规的python字符串
修改代码:
import tensorflow as tf tf.compat.v1.disable_eager_execution() hello=tf.constant("Hello TensorFlow") sess=tf.compat.v1.Session() print(sess.run(hello).decode('UTF-8'))
正确输出:
Hello TensorFlow
参考
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-tensorflow-on-ubuntu-16-04