Win10快速安装tensorflow CPU版本

快速浏览请直接跳转到文章末尾。

首先安装Anaconda,然后创建一个独立的新环境来安装tensorflow。请确认anaconda已经添加到电脑的环境变量,否则conda指令在cmd命令行将无法使用。

conda create -n tf_cpu_avx2 python==3.7 # -n后为环境名和对应python版本
conda activate tf_cpu_avx2 # 可激活环境
conda deactivate # 可关闭激活状态(在停止操作前暂时不用)

然后可以在创建的新环境下安装tensorflow。可以直接使用

conda search tensorflow # 查看可供安装的tensorflow版本

conda install tensorflow # 直接安装
conda install tensorflow==1.4.0 # 指定安装版本(两者取一)

# 也可以通过pip安装
pip install tensorflow # 直接安装
pip install tensorflow==1.4.0 # 指定安装版本(同上,两者取一)

但是这样安装的话可能会出现一个问题,即安装的tensorflow不支持avx2指令集,与自己的电脑冲突。即

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

这是可以去网页下载支持AVX2指令集的tensorflow版本wheel文件来安装。例如,查询得知python 3.7+64位+win10对应的支持AVX2指令集的tensorflow版本为tensorflow-1.14.0-cp37-cp37m-win_amd64.whl,则在命令行跳转至该文件所在目录下后可以使用如下指令安装

pip install tensorflow-1.14.0-cp37-cp37m-win_amd64.whl

安装完毕后可使用以下代码进行测试。首先命令输入python进入python环境,然后

import tensorflow as tf

m1 = tf.constant([[3., 3.]])
m2 = tf.constant([[2.], [2.]])
product = tf.matmul(m1, m2)

with tf.Session() as sess:
    result = sess.run(product)
    print(result)

如果未报错,则安装成功。如果出现以下警告

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_quint8 = np.dtype([("quint8", np.uint8, 1)])

则可以对numpy进行降级。

pip uninstall numpy # 卸载现有numpy
pip install numpy==1.16.0

最后为了更好的体验,可以安装Jupyter Notebook。过程如下:

conda install jupyter notebook # 安装jupyter notebook
jupyter notebook # 打开notebook
# 键盘按Ctrl+C可退出Notebook 
# deactivate tf_cpu_avx2 # 停止使用后可停止环境激活状态

然后可以安装Keras。

conda install keras

但是import keras时报错。

Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.10.4, library is 1.10.5

尝试重装h5py和hdf5,以下方法都未解决。python中导入keras和tensorflow都会报错。

pip uninstall h5py
pip install h5py

conda install -c anaconda hdf5=1.10.4
# conda install -c anaconda hdf5=1.10.5

无奈创建新环境重装tensorflow和keras。

conda update conda # 更新anaconda

conda create -n tensorflow_cpu python=3.7
conda activate tensorflow_cpu
cd \Anaconda\envs\tensorflow_cpu # 确保下载的.whl安装文件在此目录下
pip install tensorflow-1.14.0-cp37-cp37m-win_amd64.whl # 安装tensorflow

pip uninstall numpy
pip install numpy==1.16.0 # numpy降级

pip install keras # 安装keras

python # 进入python终端
import keras
# 如果显示Using TensorFlow Backend则安装成功

exit() # 退出python终端
pip install jupyter notebook
jupyter notebook # 打开notebook

测试成功。

最后可以顺便安装pandas,matplotlib,和scikit-learn。

conda install pandas
python -m pip install -U matplotlib
conda install scikit-learn

以上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章