下载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
结果如下: