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

以上。

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