在本文,我们将在Ubuntu 18.04系统上安装TensorFlow(仅限CPU)机器学习库,同时也适用于Debian 9系统。如果你需要Tensorflow GPU,你应该在Ubuntu 18.04系统上有专用的显卡--NVIDIA,AMD e.t.c,为Tensorflow GPU安装的软件是CUDA Toolkit,参考Ubuntu 18.04系统下安装tensorflow-gpu 1.9一文。
在Ubuntu 18.04 LTS系统上安装Tensorflow(CPU)的方法
说明:要在Ubuntu 18.04上安装Tensorflow(仅限CPU),需要Python 2.7或Python 3.3+。
1、通过运行以下命令安装Python和所需的模块:
sudo apt update
sudo apt -y install python python-pip python-setuptools python-dev
2、然后使用pip Python包管理器安装Tensorflow:
pip install --upgrade tensorflow requests
3、如果有支持CUDA的GPU卡,则可以安装GPU包:
pip install tensorflow-gpu
注:GPU封装需要支持CUDA的GPU卡。
4、验证Tensorflow是否正常:
python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
5、正常的话输出以下信息:
2018-12-21 00:53:36.272184: I tensorflow/core/platform/cpu_feature_guard.cc:141]
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
tf.Tensor(820.4219, shape=(), dtype=float32)
6、运行测试模型:
mkdir ~/tensorflow_projects
cd ~/tensorflow_projects
git clone https://github.com/tensorflow/models.git
export PYTHONPATH="$PYTHONPATH:$(pwd)/models"
cd models/official/mnist
python mnist.py
使用TensorBoard的方法
说明:TensorBoard是一组可视化工具,可以更轻松地理解,调试和优化TensorFlow程序。使用TensorBoard可视化TensorFlow图,绘制有关图表执行的量化指标,并显示其他数据,如通过它的图像。
1、运行以下命令启动TensorBoard:
mkdir ~/tensor_logs
tensorboard --logdir=~/tensor_logs
2、在运行tensorboard命令时,下面的输出将打印在屏幕上:
TensorBoard 1.12.1 at http://ubuntu-01:6006 (Press CTRL+C to quit)
3、可以按CTRL + C来结束TensorBoard进程
不是默认情况下,Tensorflow输出存储在/tmp目录下,完全配置TensorBoard后,访问http://[ServerHostname|IPAddress]:6006上的URL,仪表板如下所示:
在Docker容器中运行Tensorflow(CPU)的方法
说明可以在docker容器中运行TensorFlow,如果没有在Ubuntu 18.04上安装Docker,可以参考在Ubuntu 18.04.1服务器上安装docker的步骤,附基本配置说明。TensorFlow Docker镜像已配置为运行TensorFlow,Docker容器在虚拟环境中运行,是设置GPU支持最简单的方法。
1、下载TensorFlow Docker镜像:
docker pull tensorflow/tensorflow
2、下载后,运行以下命令启动Jupyter notebook服务器:
docker run -it -p 8888:8888 tensorflow/tensorflow
3、如果只想运行TensorFlow测试,请使用:
docker run -it --rm tensorflow/tensorflow \
python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
相关主题