下面是我在 Amazon EC2 上配置 Ubuntu 深度学习实例时使用的基本步骤:设置 Git 部署目标、安装 Bazel、准备 TensorFlow、安装 Python 模块,并开放 Jupyter 以便进行交互式工作。
1. 构建 Git 部署服务器
如果客户端机器上还没有 SSH 密钥,先生成一个:
ssh-keygen -t rsa -C "user.email"
在实例上创建或使用一个裸 Git 仓库,然后编辑它的 post-receive 钩子:
vim sample.git/hooks/post-receive
post-receive 钩子示例:
#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/Deployment/sample git checkout -f
chmod -R 777 /home/ubuntu/Deployment/sample
让钩子可执行:
chmod +x sample.git/hooks/post-receive
之后,推送到这个裸仓库时,最新文件会被检出到 /home/ubuntu/Deployment/sample。
2. 安装 Bazel
将 Bazel APT 仓库添加为软件包源。这是一次性的设置步骤:
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
如果想安装 Bazel 的测试版本,请在仓库行中将 stable 替换为 testing。
安装 Bazel:
sudo apt-get update
sudo apt-get install bazel
安装完成后,可以用以下命令升级 Bazel:
sudo apt-get upgrade bazel
对于较新的 Ubuntu 版本,Bazel 的具体安装方法可能已经变化。请查看当前的 Bazel 文档,并在本地验证已安装版本:
bazel version
3. 编译 TensorFlow
TensorFlow 的编译高度依赖实例上安装的 TensorFlow 版本、CUDA 版本、cuDNN 版本、Python 版本和 GPU 驱动。编译前先验证本地环境:
python --version
python3 --version
nvcc --version
nvidia-smi
bazel version
典型的源码构建流程如下:
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
然后构建 Python wheel:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
根据你配置的 Python 环境,使用 pip 或 pip3 安装生成的 wheel:
pip install /tmp/tensorflow_pkg/tensorflow-*.whl
验证安装:
python -c "import tensorflow as tf; print(tf.__version__)"
4. 安装 Python 模块
安装常用的 Python 包:
pip install tensorflow numpy pandas
如果实例默认使用 Python 3,请使用:
pip3 install tensorflow numpy pandas
通常,把包安装在虚拟环境中会更安全:
python3 -m venv ~/venvs/deeplearning
source ~/venvs/deeplearning/bin/activate
pip install --upgrade pip
pip install tensorflow numpy pandas
检查这些模块是否能正确导入:
python - <<'PY'
import tensorflow as tf
import numpy as np
import pandas as pd
print('tensorflow', tf.__version__)
print('numpy', np.__version__)
print('pandas', pd.__version__)
PY
5. 配置 Jupyter
安装 Jupyter:
pip install jupyter
生成配置文件:
jupyter notebook --generate-config
设置密码:
jupyter notebook password
在实例上启动 Jupyter:
jupyter notebook --ip=0.0.0.0 --no-browser
如果需要从浏览器访问 notebook,请打开正确的 EC2 安全组端口,或使用 SSH 隧道。SSH 隧道通常更安全:
ssh -L 8888:localhost:8888 ubuntu@your-ec2-host
然后打开这个本地 URL:
http://localhost:8888
参考:<https://punchagan.muse-amuse.in/posts/create-a-public-jupyter-server-quickly.html>
