Windows 8 Python 3.6 下安裝 TensorFlow 和 keras

     這兩天安裝TensorFlow和keras(基於TensorFlow)踩了不少坑,特此總結。

     安裝tensorflow主要有兩種方式:原環境安裝(不推薦)和虛擬環境安裝(推薦)。

1、原環境安裝(各種坑):

      將tensorflow作爲一個普通工具包安裝:pip install tensorflow

      引入tensorflow:import  etnsorflow

     顯示: ImportError: 

                Importing the multiarray numpy extension module failed.  Most likely you are trying to import a failed                          build  of numpy.If you're working with a numpy git repo, try `git clean -xdf` (removes all files not under                         version control).  Otherwise reinstall numpy.

                 Original error was: DLL load failed: 找不到指定的模塊。

    解決辦法:numpy版本不對,tensorflow要求numpy版本大於等於1.13.3,故卸載原有numpy,重新安裝numpy1.13.3。

                    接下來可能還有類似的版本衝突問題,需要一一解決,故不建議此種安裝方式。

2、虛擬環境安裝(比較順)

     1)用管理員權限打開Anacoda Prompt;

     2)創建虛擬環境,命名爲“tensorflow”;

          命令:conda create -n tensorflow python=3.5

          注意:因爲tensorflow最適合的python版本是3.5,所以無論你的python是什麼版本,建議虛擬一個python3.5的環境

    3)進入虛擬環境;

           命令:activate tensorflow

    4) 安裝tensorflow 

          命令:pip install tensorflow

3、測試tensorflow

          命令:import tensorflow as tf

                   node = tf.constant(4.0)

                  print(node)

   結果:Tensor<”Const:0”,shape=<>,dtype=float32,至此,tensorflow安裝完成。

         注意:真正使用需要進入虛擬的python環境   例如:D:\Pythoninstall\envs\tensorflow,點擊python.exe進入命令行。

4、安裝keras   (需要在tensorflow的虛擬環境中安裝keras

  1)用管理員權限打開Anacoda Prompt

       2)進入tensorflow虛擬環境

        命令:activate tensorflow

      3)安裝keras

       命令:pip install keras

5、測試keras (需要進入Anaconda Prompt)

      利用Keras中mnist數據集測試 

     命令: conda install git

                git clone https://github.com/fchollet/keras.git

                cd keras/examples/

                python mnist_mlp.py

  結果:出現“Test accuracy :0.9838”,程序無錯進行,至此,keras安裝完成。

      


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