在 Ubuntu 20.04|18.04/Debian 10|9 上安装 Tensorflow(仅 CPU)
在这篇博文中,我们将在 Ubuntu 20.04|18.04/Debian 10|9 上安装 TensorFlow 机器学习库。如果您需要 Tensorflow GPU,则您的 Ubuntu/Debian 系统上应该有专用显卡 – NVIDIA、AMD 等。为 Tensorflow GPU 安装的软件是 CUDA Toolkit。
在 Ubuntu 20.04|18.04 LTS/Debian 10|9 上安装 Tensorflow(仅 CPU)
要在 Ubuntu 20.04|18.04 上安装 Tensorflow(仅限 CPU),您需要使用不支持 GPU 的 Tensorflow 版本,该版本需要 Python 2.7 或 Python 3.3+。通过运行以下命令来安装 Python 和所需的模块:
# Python2
sudo apt update
sudo apt -y install python python-pip python-setuptools python-dev
# Python 3
sudo apt update
sudo apt -y install python3 python3-pip python3-setuptools python3-dev
然后使用 pip Python 包管理器安装 Tensorflow。
#pip2
sudo pip install --upgrade tensorflow requests
#pip3
sudo pip3 install --upgrade tensorflow requests
如果您有支持 CUDA 的 GPU 卡,则可以安装 GPU 软件包。
#Python2
sudo pip install tensorflow-gpu
#Python3
sudo pip3 install tensorflow-gpu
但不要忘记 GPU 包需要支持 CUDA® 的 GPU 卡。
验证 Ubuntu 20.04|18.04/Debian 10|9 上的 Tensorflow(仅限 CPU)安装
验证您的 Tensorflow 是否正常工作。
#python2
python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
#python3
python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
输出 :
2018-12-19 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)
运行测试模型:
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 图,绘制有关图执行情况的定量指标,并显示其他数据,例如通过图的图像。
通过运行以下命令启动 TensorBoard:
mkdir ~/tensor_logs
tensorboard --logdir=~/tensor_logs
运行 tensorboard
命令时,如下所示的输出将打印在屏幕上。
TensorBoard 1.12.1 at http://ubuntu-01:6006 (Press CTRL+C to quit)
您可以按 CTRL+C
终止 TensorBoard 进程
并不是说默认情况下 Tensorflow 输出存储在 /tmp
目录下。完全配置 TensorBoard 后,访问 http://[ServerHostname|IPAddress]:6006
上的 URL。仪表板如下所示:
在 Docker 容器中运行 Tensorflow(仅限 CPU)
您还可以在 Docker 容器中运行 TensorFlow。如果您没有在 Ubuntu/Debian Linux 上安装 Docker 引擎,我们的指南应该会派上用场。
如何在 Ubuntu/Debian/Fedora/Arch/CentOS 上安装 Docker CE
TensorFlow Docker 映像已配置为运行 TensorFlow。 Docker 容器在虚拟环境中运行,是设置 GPU 支持的最简单方法。
下载 TensorFlow Docker 镜像:
docker pull tensorflow/tensorflow
下载后,通过运行以下命令启动 Jupyter 笔记本服务器:
docker run -it -p 8888:8888 tensorflow/tensorflow
如果您只想运行 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])))"
从官方网站阅读有关在 Docker 中运行 TensorFlow 的更多信息。