Ubuntu18.04環境下使用Anaconda搭建Tensorflow


下載Anaconda

  • 打開Anaconda下載地址,然后下載最新的Anaconda。
  • 打開終端,進入Anaconda下載包所在的文件夾,運行bash Anaconda3-2020.02-Linux-x86_64.sh,一路y即可。
  • 安裝完成后運行conda --version檢測是否安裝成功。

搭建Tensorflow

  • 在終端中輸入conda create -n tensorflow python=3.7,一路y即可。
  • 在終端中輸入conda activate tensorflow 即可激活Tensorflow環境,conda deactivate即可退出Tensorflow環境。
  • 在終端中輸入人pip install --ignore-installed --upgrade tensorflow進行安裝/升級。

此時,Tensorflow環境已經搭建完畢。

進行測試

在終端中輸入touch test.py創建文件,修改文件內容為

import tensorflow as tf

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

if __name__=='__main__':
    g = tf.Graph()
    # add ops to the user created graph
    with g.as_default():
        hello = tf.constant('Hello Tensorflow')
        sess = tf.compat.v1.Session()
        print(sess.run(hello))

在終端中輸入python test.py,結果如下

在Pycharm中整合Tensorflow環境

  • file -> new project
  • 選擇Existing interpreter -> Conda Environment -> Ok -> Create即可。

進行測試

創建test.py,輸入如下代碼:

import tensorflow as tf

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

if __name__=='__main__':
    g = tf.Graph()
    # add ops to the user created graph
    with g.as_default():
        hello = tf.constant('Hello Tensorflow')
        sess = tf.compat.v1.Session()
        print(sess.run(hello))


運行結果如下:

通常會彈出一些提示信息

2020-04-20 17:43:50.307721: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer.so.6'; dlerror: libnvinfer.so.6: cannot open shared object file: No such file or directory
2020-04-20 17:43:50.307796: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libnvinfer_plugin.so.6'; dlerror: libnvinfer_plugin.so.6: cannot open shared object file: No such file or directory
2020-04-20 17:43:50.307805: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:30] Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

這些信息時提示當前系統沒有安裝TensorRT相關的內容,如果不需要GPU支持,直接忽略即可,解決這些Warning的方法:在終端中運行如下代碼

# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt-get update

# Install NVIDIA driver
sudo apt-get install --no-install-recommends nvidia-driver-430
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
    cuda-10-1 \
    libcudnn7=7.6.4.38-1+cuda10.1  \
    libcudnn7-dev=7.6.4.38-1+cuda10.1


# Install TensorRT. Requires that libcudnn7 is installed above.
sudo apt-get install -y --no-install-recommends libnvinfer6=6.0.1-1+cuda10.1 \
    libnvinfer-dev=6.0.1-1+cuda10.1 \
    libnvinfer-plugin6=6.0.1-1+cuda10.1

結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM