TensorFlow 安裝配置

Anaconda(Python) + TensorFlow 安裝 (Ubuntu 16.04 & Python3 & no GPU support)

安裝Anaconda

  • step 1 下載 Anaconda
URL:https://www.continuum.io/downloads
  • step 2 下載后(Anaconda3-4.4.0-Linux-x86_64),在Ubuntu下執(zhí)行命令安裝:
bash Anaconda3-4.4.0-Linux-x86_64

# 根據(jù)提示安裝Anaconda3-4.4.0-Linux-x86_64,若無特別需求情況下可直接安裝至默認路徑下
# 安裝最后會提示是否將anaconda3的bin路徑添加至.bashrc中,因為在后面的使用中會使用到這個目錄下的命令,故建議添加

安裝Bazel

執(zhí)行以下命令即安裝:

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
    
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

sudo apt-get update && sudo apt-get install bazel

安裝TensorFlow(CPU版)

通過pip安裝TensorFlow

  • step 1 安裝tensorflow

      pip3 install tensorflow 
    

源碼編譯安裝

  • step 1 下載TensorFlow項目(建議下載最新的Release版本)
TensorFlow GitHub: https://github.com/tensorflow/tensorflow/
  • step 2 將下載的TensorFlow解壓縮

  • step 3 切換到TensorFlow的目錄下,執(zhí)行configure配置安裝參數(shù),根據(jù)電腦配置選定相關的安裝模塊(若無特殊要求,選在默認參數(shù)即可)

      ./configure
    
  • step 4 通過Bazel編譯

      bazel build --copt=-march=native -c opt //tensorflow/tools/pip_package:build_pip_package
    
  • step 5 將編譯后的文件打包成pip安裝包

      bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg/
    
  • step 6 通過pip安裝編譯好的TensorFlow

      pip install /tmp/tensorflow_pkg/tensorflow-1.2.0rc2-cp36-cp36m-linux_x86_64.whl 
    注意: 此處文件名可能不同
    
  • step 7 測試:

    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))

若執(zhí)行并正確輸出,則安裝成功

問題排除

執(zhí)行提示:

/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found ... "GLIBCXX_3.4.21"
or
/home/mozre/anaconda3/bin/../lib/libstdc++.so.6 not found ... "GLIBCXX_3.4.21"

查看so文件下是否有GLIBCXX_3.4.21:

    strings /home/mozre/anaconda3/bin/../lib/libstdc++.so.6.0.19 | grep GLIBCXX
    
    GLIBCXX_3.4
    GLIBCXX_3.4.1
    GLIBCXX_3.4.2
    GLIBCXX_3.4.3
    GLIBCXX_3.4.4
    GLIBCXX_3.4.5
    GLIBCXX_3.4.6
    GLIBCXX_3.4.7
    GLIBCXX_3.4.8
    GLIBCXX_3.4.9
    GLIBCXX_3.4.10
    GLIBCXX_3.4.11
    GLIBCXX_3.4.12
    GLIBCXX_3.4.13
    GLIBCXX_3.4.14
    GLIBCXX_3.4.15
    GLIBCXX_3.4.16
    GLIBCXX_3.4.17
    GLIBCXX_3.4.18
    GLIBCXX_3.4.19
    GLIBCXX_FORCE_NEW
    GLIBCXX_DEBUG_MESSAGE_LENGTH
    # 從上訴結果看的確是沒有我們所需的GLIBCXX_3.4.21

原因: gcc版本過低,升級gcc:

sudo apt-get install libstdc++6
sudo add-apt-repository ppa:ubuntu-toolchain-r/test 
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

若執(zhí)行上述命令還是存在執(zhí)行提示上述信息,則可能是由于下載的gcc lib包沒有更新到你執(zhí)行的目錄下,這里需要手動去更新:

  • 因為上面已經(jīng)更新過gcc,所以我們的系統(tǒng)lib庫中應該有最新的lib包,查找相關的lib包:

      sudo find / -name "libstdc++.so*"
    

    查找結果類似:

      /usr/lib/vmware-tools/lib64/libstdc++.so.6
      /usr/lib/vmware-tools/lib64/libstdc++.so.6/libstdc++.so.6
      /usr/lib/vmware-tools/lib32/libstdc++.so.6
      /usr/lib/vmware-tools/lib32/libstdc++.so.6/libstdc++.so.6
      /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so
      /usr/lib/x86_64-linux-gnu/libstdc++.so.6
      /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23
    

    我們可以看到/usr/lib/x86_64-linux-gnu/libstdc++.so.6和/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23

    通過上訴的命令查看:

       strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23 | grep GLIBCXX
    

    結果為:

      GLIBCXX_3.4
      GLIBCXX_3.4.1
      GLIBCXX_3.4.2
      GLIBCXX_3.4.3
      GLIBCXX_3.4.4
      GLIBCXX_3.4.5
      GLIBCXX_3.4.6
      GLIBCXX_3.4.7
      GLIBCXX_3.4.8
      GLIBCXX_3.4.9
      GLIBCXX_3.4.10
      GLIBCXX_3.4.11
      GLIBCXX_3.4.12
      GLIBCXX_3.4.13
      GLIBCXX_3.4.14
      GLIBCXX_3.4.15
      GLIBCXX_3.4.16
      GLIBCXX_3.4.17
      GLIBCXX_3.4.18
      GLIBCXX_3.4.19
      GLIBCXX_3.4.20
      GLIBCXX_3.4.21
      GLIBCXX_3.4.22
      GLIBCXX_3.4.23
      GLIBCXX_DEBUG_MESSAGE_LENGTH
    

    故我們只需要將雙面的的lib文件拷貝到對應的文件下,并創(chuàng)建相關的軟連接即可:

    • 將lib文件copy至對應目錄下
      cp /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23 /home/mozre/anaconda3/bin/../lib/
    • 刪除有調用此lib的軟連接
      rm /home/mozre/anaconda3/bin/../lib/libstdc++.so.6
    • 建立此lib與目標調的軟連接
      ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23 libstdc++.so.6
    • 查看是否配置成功
      strings /home/mozre/anaconda3/bin/../lib/libstdc++.so.6 (若測試結果為上面strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.23 結果一致,則說明成功)
    • 運行測試代碼
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容