下载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支持,直接忽略即可。