使用 pip 安装 TensorFlow
使用 pip 安装 TensorFlow
TensorFlow 2.0 Alpha 版现已推出
-
tensorflow==2.0.0-alpha0
– 仅支持 CPU 的预览 TF 2.0 Alpha 版(不稳定)
-
tensorflow==2.0.0-alpha0-gpu
– 支持 GPU 的预览 TF 2.0 Alpha 版(不稳定,适用于 Ubuntu 和 Windows)
可用软件包
-
tensorflow
– 仅支持 CPU 的最新稳定版(适用于 Ubuntu 和 Windows)
-
tensorflow-gpu
– 支持 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)
-
tf-nightly
– 仅支持 CPU 的预览每夜版(不稳定)
-
tf-nightly-gpu
– 支持 GPU 的预览每夜版(不稳定,适用于 Ubuntu 和 Windows)
系统要求
- Ubuntu 16.04 或更高版本(64 位)
- macOS 10.12.6 (Sierra) 或更高版本(64 位)(不支持 GPU)
- Windows 7 或更高版本(64 位)(仅支持 Python 3)
- Raspbian 9.0 或更高版本
硬件要求
- 从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。
- 阅读 GPU 支持指南,以在 Ubuntu 或 Windows 上设置支持 CUDA® 的 GPU 卡。
1. 在系统上安装 Python 开发环境
检查是否已配置 Python 环境:
<span class="pln">python3 </span><span class="pun">--</span><span class="pln">version</span><span class="pln">pip3 </span><span class="pun">--</span><span class="pln">version</span><span class="pln">virtualenv </span><span class="pun">--</span><span class="pln">version</span>
如果已安装这些软件包,请跳至下一步。
否则,请安装 Python、pip 软件包管理器和 Virtualenv:
<span class="pln">sudo apt update</span><span class="pln">sudo apt install python3</span><span class="pun">-</span><span class="pln">dev python3</span><span class="pun">-</span><span class="pln">pip</span><span class="pln">sudo pip3 install </span><span class="pun">-</span><span class="pln">U virtualenv </span><span class="com"># system-wide install</span>
2. 创建虚拟环境(推荐)
Python 虚拟环境用于将软件包安装与系统隔离开来。
目录来存放它:
virtualenv --system-site-packages -p python3 ./venv
使用特定于 shell 的命令激活该虚拟环境:
source ./venv/bin/activate # sh, bash, ksh, or zsh
当 virtualenv 处于有效状态时,shell 提示符带有
前缀。
在不影响主机系统设置的情况下,在虚拟环境中安装软件包。首先升级
:
<span class="pln">pip install </span><span class="pun">--</span><span class="pln">upgrade pip</span><span class="pln">pip list </span><span class="com"># show packages installed within the virtual environment</span>
之后要退出 virtualenv,请使用以下命令:
deactivate # don't exit until you're done using TensorFlow
3. 安装 TensorFlow pip 软件包
请从 PyPI 中选择以下某个 TensorFlow 软件包进行安装:
-
tensorflow
– 仅支持 CPU 的最新稳定版(建议新手使用)
-
tensorflow-gpu
– 支持 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)
-
tf-nightly
– 仅支持 CPU 的预览每夜版(不稳定)
-
tf-nightly-gpu
– 支持 GPU 的预览每夜版(不稳定,适用于 Ubuntu 和 Windows)
-
tensorflow==2.0.0-alpha0
– 仅支持 CPU 的预览 TF 2.0 Alpha 版(不稳定)
-
tensorflow-gpu==2.0.0-alpha0
– 支持 GPU 的预览 TF 2.0 Alpha 版(不稳定,Ubuntu 和 Windows)
pip install --upgrade tensorflow
验证安装效果:
python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
软件包位置
有几个安装机制需要您提供 TensorFlow Python 软件包的网址。您需要根据 Python 版本指定网址。
版本 | 网址 |
---|---|
Linux | |
Python 2.7(仅支持 CPU) | https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp27-none-linux_x86_64.whl |
Python 2.7(支持 GPU) | https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp27-none-linux_x86_64.whl |
Python 3.4(仅支持 CPU) | https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp34-cp34m-linux_x86_64.whl |
Python 3.4(支持 GPU) | https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp34-cp34m-linux_x86_64.whl |
Python 3.5(仅支持 CPU) | https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl |
Python 3.5(支持 GPU) | https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp35-cp35m-linux_x86_64.whl |
Python 3.6(仅支持 CPU) | https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl |
Python 3.6(支持 GPU) | https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-linux_x86_64.whl |
macOS(仅支持 CPU) | |
Python 2.7 | https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py2-none-any.whl |
Python 3.4、3.5、3.6 | https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl |
Windows | |
Python 3.5(仅支持 CPU) | https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp35-cp35m-win_amd64.whl |
Python 3.5(支持 GPU) | https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp35-cp35m-win_amd64.whl |
Python 3.6(仅支持 CPU) | https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp36-cp36m-win_amd64.whl |
Python 3.6(支持 GPU) | https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl |