使用 pip 安裝 TensorFlow

TensorFlow 2 軟件包現(xiàn)已推出

  • tensorflow:支持 CPU 和 GPU 的最新穩(wěn)定版(適用于 Ubuntu 和 Windows)
  • tf-nightly:預(yù)覽 build(不穩(wěn)定)。Ubuntu 和 Windows 均包含 GPU 支持。

舊版 TensorFlow

對于 TensorFlow 1.x,CPU 和 GPU 軟件包是分開的:

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

系統(tǒng)要求

  • 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++ 可再發(fā)行軟件包
  • Raspbian 9.0 或更高版本
  • GPU 支持需要使用支持 CUDA? 的顯卡(適用于 Ubuntu 和 Windows)

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

硬件要求

  • 從 TensorFlow 1.6 開始,二進(jìn)制文件使用 AVX 指令,這些指令可能無法在舊版 CPU 上運(yùn)行。
  • 閱讀 GPU 支持指南,以在 Ubuntu 或 Windows 上設(shè)置支持 CUDA? 的 GPU 卡。

1. 在系統(tǒng)上安裝 Python 開發(fā)環(huán)境(Python 3)

檢查是否已配置 Python 環(huán)境:

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

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

如果已安裝這些軟件包,請?zhí)料乱徊健?br> 否則,請安裝 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

注意:升級系統(tǒng) pip 可能會(huì)導(dǎo)致問題。
如果不是在虛擬環(huán)境中,請針對下面的命令使用 python3 -m pip。這樣可以確保您升級并使用 Python pip,而不是系統(tǒng) pip。

2.創(chuàng)建虛擬環(huán)境(推薦)

Python 虛擬環(huán)境用于將軟件包安裝與系統(tǒng)隔離開來。

Ubuntu/macOS

創(chuàng)建一個(gè)新的虛擬環(huán)境,方法是選擇 Python 解釋器并創(chuàng)建一個(gè) ./venv 目錄來存放它:

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

使用特定于 shell 的命令激活該虛擬環(huán)境:

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

當(dāng) virtualenv 處于有效狀態(tài)時(shí),shell 提示符帶有 (venv) 前綴。

在不影響主機(jī)系統(tǒng)設(shè)置的情況下,在虛擬環(huán)境中安裝軟件包。首先升級 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 中選擇以下某個(gè) TensorFlow 軟件包進(jìn)行安裝:

  • tensorflow:支持 CPU 和 GPU 的最新穩(wěn)定版(適用于 Ubuntu 和 Windows)。
  • tf-nightly:預(yù)覽 build(不穩(wěn)定)。Ubuntu 和 Windows 均包含 GPU 支持。
  • tensorflow==1.15:TensorFlow 1.x 的最終版本。

系統(tǒng)會(huì)自動(dòng)安裝軟件包依賴項(xiàng)。這些依賴項(xiàng)就列在 setup.py 文件的 REQUIRED_PACKAGES 下。

pip install --upgrade tensorflow

驗(yàn)證安裝效果:

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

成功:TensorFlow 現(xiàn)已安裝完畢。請查看教程開始使用。

軟件包位置

部分安裝方式需要您提供 TensorFlow Python 軟件包的網(wǎng)址。您需要根據(jù) Python 版本指定網(wǎng)址。

版本 網(wǎng)址
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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容