TensorFlow 2 软件包现已推出

  • tensorflow:支持 CPU 和 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)
  • tf-nightly:预览 build(不稳定)。Ubuntu 和 Windows 均包含 GPU 支持。

旧版 TensorFlow

对于 TensorFlow 1.x,CPU 和 GPU 软件包是分开的:

  • tensorflow==1.15:仅支持 CPU 的版本
  • tensorflow-gpu==1.15:支持 GPU 的版本(适用于 Ubuntu 和 Windows)

系统要求

  • Python 3.5-3.7
  • pip 19.0 或更高版本(需要 manylinux2010 支持)
  • Ubuntu 16.04 或更高版本(64 位)
  • macOS 10.12.6 (Sierra) 或更高版本(64 位)(不支持 GPU)
  • Windows 7 或更高版本(64 位)(仅支持 Python 3)
  • 适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包
  • Raspbian 9.0 或更高版本
  • GPU 支持需要使用支持 CUDA® 的显卡(适用于 Ubuntu 和 Windows)

注意:必须使用最新版本的 pip,才能安装 TensorFlow 2。

硬件要求

  • 从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。
  • 阅读 GPU 支持指南,以在 Ubuntu 或 Windows 上设置支持 CUDA® 的 GPU 卡。

1. 在系统上安装 Python 开发环境(Python 3)

检查是否已配置 Python 环境:

需要使用 Python 3.5-3.7 和 pip 19.0 及更高版本

python3 --version
pip3 --version
virtualenv --version

如果已安装这些软件包,请跳至下一步。 否则,请安装 Python、pip 软件包管理器和 Virtualenv:

Ubuntu

sudo apt update
sudo apt install python3-dev python3-pip
sudo pip3 install -U virtualenv  # system-wide install

macOS

使用 Homebrew 软件包管理器安装:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
brew update
brew install python  # Python 3
sudo pip3 install -U virtualenv  # system-wide install

注意:升级系统 pip 可能会导致问题。 如果不是在虚拟环境中,请针对下面的命令使用 python3 -m pip。这样可以确保您升级并使用 Python pip,而不是系统 pip。

2.创建虚拟环境(推荐)

Python 虚拟环境用于将软件包安装与系统隔离开来。

Ubuntu/macOS

创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 ./venv 目录来存放它:

virtualenv --system-site-packages -p python3 ./venv

使用特定于 shell 的命令激活该虚拟环境:

source ./venv/bin/activate  # sh, bash, ksh, or zsh

virtualenv 处于有效状态时,shell 提示符带有 (venv) 前缀。

在不影响主机系统设置的情况下,在虚拟环境中安装软件包。首先升级 pip:

pip install --upgrade pip

pip list  # show packages installed within the virtual environment

之后可以使用以下命令退出 virtualenv:

deactivate  # don't exit until you're done using TensorFlow

3.安装 TensorFlow pip 软件包

请从 PyPI 中选择以下某个 TensorFlow 软件包进行安装:

  • tensorflow:支持 CPU 和 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)。
  • tf-nightly:预览 build(不稳定)。Ubuntu 和 Windows 均包含 GPU 支持。
  • tensorflow==1.15:TensorFlow 1.x 的最终版本。

系统会自动安装软件包依赖项。这些依赖项就列在 setup.py 文件的 REQUIRED_PACKAGES 下。

pip install --upgrade tensorflow

验证安装效果:

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

成功:TensorFlow 现已安装完毕。请查看教程开始使用。

软件包位置

部分安装方式需要您提供 TensorFlow Python 软件包的网址。您需要根据 Python 版本指定网址。

版本 网址
Linux
Python 2.7(支持 GPU) https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.1.0-cp27-cp27mu-manylinux2010_x86_64.whl
Python 2.7(仅支持 CPU) https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.1.0-cp27-cp27mu-manylinux2010_x86_64.whl
Python 3.5(支持 GPU) https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.1.0-cp35-cp35m-manylinux2010_x86_64.whl
Python 3.5(仅支持 CPU) https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.1.0-cp35-cp35m-manylinux2010_x86_64.whl
Python 3.6(支持 GPU) https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.1.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.6(仅支持 CPU) https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.1.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.7(支持 GPU) https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.1.0-cp37-cp37m-manylinux2010_x86_64.whl
Python 3.7(仅支持 CPU) https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.1.0-cp37-cp37m-manylinux2010_x86_64.whl
macOS(仅支持 CPU)
Python 2.7 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.1.0-cp27-cp27m-macosx_10_9_x86_64.whl
Python 3.5 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.1.0-cp35-cp35m-macosx_10_6_intel.whl
Python 3.6 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.1.0-cp36-cp36m-macosx_10_9_x86_64.whl
Python 3.7 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Windows
Python 3.5(支持 GPU) https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.1.0-cp35-cp35m-win_amd64.whl
Python 3.5(仅支持 CPU) https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.1.0-cp35-cp35m-win_amd64.whl
Python 3.6(支持 GPU) https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.1.0-cp36-cp36m-win_amd64.whl
Python 3.6(仅支持 CPU) https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.1.0-cp36-cp36m-win_amd64.whl
Python 3.7(支持 GPU) https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.1.0-cp37-cp37m-win_amd64.whl
Python 3.7(仅支持 CPU) https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.1.0-cp37-cp37m-win_amd64.whl
Raspberry PI(仅支持 CPU)
Python 3、Pi0 或 Pi1 https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.1.0-cp35-none-linux_armv6l.whl
Python 3、Pi2 或 Pi3 https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.1.0-cp35-none-linux_armv7l.whl

参考

https://www.tensorflow.org/install/pip?lang=python2#ubuntu-mac-os