TensorFlow安装使用

TensorFlow安装使用

更新源数据

临时使用

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

默认使用

# windows系统使用cmd快速设置
pip install pip -U    # 升级pip到最新版本
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

国内源:

  1. 新版ubuntu要求使用https源,要注意。
  2. 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  3. 阿里云:http://mirrors.aliyun.com/pypi/simple/
  4. 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  5. 华中理工大学:http://pypi.hustunique.com/
  6. 山东理工大学:http://pypi.sdutlinux.org/
  7. 豆瓣:http://pypi.douban.com/simple/

永久修改,一劳永逸:

Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)

内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
windows下,在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini。内容同上。

安装

环境及版本说明

我们在以下 64 位系统上测试过 TensorFlow2 并且这些系统支持 TensorFlow:

  • Python 3.6–3.9
  • Ubuntu 16.04 或更高版本
  • Windows 7 或更高版本(含 C++ 可再发行软件包
  • macOS 10.12.6 (Sierra) 或更高版本(不支持 GPU)

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 的 pip 软件包管理器安装 TensorFlow。

TensorFlow 2 软件包需要使用高于 19.0 的 pip 版本(对于 macOS 来说,则需要高于 20.3 的 pip 版本)。

官方软件包支持 Ubuntu、Windows 和 macOS。

有关支持 CUDA® 的卡,请参阅 GPU 指南

阅读 pip 安装指南

# Requires the latest pip
pip install --upgrade pip

# Current stable release for CPU and GPU
pip install tensorflow

# Or try the preview build (unstable)
pip install tf-nightly

运行 TensorFlow 容器

TensorFlow Docker 映像已经过配置,可运行 TensorFlow。Docker 容器可在虚拟环境中运行,是设置 GPU 支持的最简单方法。

 docker pull tensorflow/tensorflow:latest  # Download latest stable image
 docker run -it -p 8888:8888 tensorflow/tensorflow:latest-jupyter  # Start Jupyter server 

阅读 Docker 安装指南

python虚拟环境创建

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

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

python -m venv --system-site-packages .\venv

激活虚拟环境:

.\venv\Scripts\activate

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

pip install --upgrade pip

pip list  # show packages installed within the virtual environment

之后退出虚拟环境:

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

TensorFlow安装

从 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])))"

系统安装

pip3 install --user --upgrade tensorflow  # install in $HOME

验证安装效果:

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

GPU支持

pip 软件包

您可以参阅 pip 安装指南,了解可用的软件包、系统要求和说明。TensorFlow pip 软件包对采用 CUDA® 的显卡提供 GPU 支持:

pip install tensorflow

本指南将介绍最新稳定版 TensorFlow 的 GPU 支持和安装步骤。

旧版 TensorFlow

对于 1.15 及更早版本,CPU 和 GPU 软件包是分开的:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

硬件要求

支持以下带有 GPU 的设备:

  • CUDA® 架构为 3.5、5.0、6.0、7.0、7.5、8.0 或更高的 NVIDIA® GPU 卡。请参阅支持 CUDA® 的 GPU 卡列表。
  • 对于 CUDA® 架构不受支持的 GPU,或为了避免从 PTX 进行 JIT 编译,亦或是为了使用不同版本的 NVIDIA® 库,请参阅在 Linux 下从源代码编译指南。
  • 软件包不包含 PTX 代码,但最新支持的 CUDA® 架构除外;因此,如果设置了 CUDA_FORCE_PTX_JIT=1,TensorFlow 将无法在旧版 GPU 上加载。(有关详细信息,请参阅应用兼容性。)