ติดตั้ง TensorFlow ด้วย pip

คู่มือนี้มีไว้สำหรับ TensorFlow เวอร์ชันเสถียรล่าสุด สำหรับการสร้างตัวอย่าง (ทุกคืน) ให้ใช้แพ็คเกจ pip ชื่อ tf-nightly โปรดดู ตารางเหล่านี้ สำหรับข้อกำหนดเวอร์ชันเก่าของ TensorFlow สำหรับรุ่น CPU เท่านั้น ให้ใช้แพ็คเกจ pip ชื่อ tensorflow-cpu

ต่อไปนี้เป็นคำสั่งการติดตั้งเวอร์ชันด่วน เลื่อนลงเพื่อดูคำแนะนำทีละขั้นตอน

ลินุกซ์

python3 -m pip install tensorflow[and-cuda]
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

แมคโอเอส

# There is currently no official GPU support for MacOS.
python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Windows พื้นเมือง

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
# Anything above 2.10 is not supported on the GPU on Windows Native
python -m pip install "tensorflow<2.11"
# Verify the installation:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

วินโดวส์ WSL2

python3 -m pip install tensorflow[and-cuda]
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

ซีพียู

python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

ทุกคืน

python3 -m pip install tf-nightly
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

ข้อกำหนดด้านฮาร์ดแวร์

รองรับอุปกรณ์ที่ใช้ GPU ต่อไปนี้:

  • การ์ด NVIDIA® GPU พร้อมสถาปัตยกรรม CUDA® 3.5, 5.0, 6.0, 7.0, 7.5, 8.0 และสูงกว่า ดูรายการ การ์ด GPU ที่รองรับ CUDA®
  • สำหรับ GPU ที่มีสถาปัตยกรรม CUDA® ที่ไม่รองรับ หรือเพื่อหลีกเลี่ยงการคอมไพล์ JIT จาก PTX หรือใช้ไลบรารี NVIDIA® เวอร์ชันอื่น โปรดดู Linux บิวด์จากคู่มือแหล่งที่มา
  • แพ็คเกจไม่มีรหัส PTX ยกเว้นสถาปัตยกรรม CUDA® ที่รองรับล่าสุด ดังนั้น TensorFlow จึงไม่สามารถโหลดบน GPU รุ่นเก่าได้เมื่อตั้งค่า CUDA_FORCE_PTX_JIT=1 (ดูรายละเอียด ความเข้ากันได้ของแอปพลิเคชัน )

ความต้องการของระบบ

  • Ubuntu 16.04 หรือสูงกว่า (64 บิต)
  • macOS 10.12.6 (Sierra) หรือสูงกว่า (64 บิต) (ไม่รองรับ GPU)
  • Windows Native - Windows 7 หรือสูงกว่า (64 บิต) (ไม่รองรับ GPU หลังจาก TF 2.10)
  • Windows WSL2 - Windows 10 19044 หรือสูงกว่า (64 บิต)

ข้อกำหนดซอฟต์แวร์

ซอฟต์แวร์ NVIDIA® ต่อไปนี้จำเป็นสำหรับการรองรับ GPU เท่านั้น

คำแนะนำทีละขั้นตอน

ลินุกซ์

1. ความต้องการของระบบ

  • Ubuntu 16.04 หรือสูงกว่า (64 บิต)

TensorFlow รองรับ Ubuntu อย่างเป็นทางการเท่านั้น อย่างไรก็ตาม คำแนะนำต่อไปนี้อาจใช้ได้กับ Linux distro อื่นๆ ด้วย

2. การตั้งค่า GPU

คุณสามารถข้ามส่วนนี้ได้หากคุณเรียกใช้ TensorFlow บน CPU เท่านั้น

ติดตั้ง ไดรเวอร์ NVIDIA GPU หากคุณยังไม่ได้ติดตั้ง คุณสามารถใช้คำสั่งต่อไปนี้เพื่อตรวจสอบว่ามีการติดตั้งแล้ว

nvidia-smi

3. ติดตั้ง TensorFlow

TensorFlow ต้องใช้ pip เวอร์ชันล่าสุด ดังนั้นควรอัปเกรดการติดตั้ง pip เพื่อให้แน่ใจว่าคุณกำลังใช้เวอร์ชันล่าสุด

pip install --upgrade pip

จากนั้นติดตั้ง TensorFlow ด้วย pip

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

4. ตรวจสอบการติดตั้ง

ตรวจสอบการตั้งค่า CPU:

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

หากส่งคืนเทนเซอร์ แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

ตรวจสอบการตั้งค่า GPU:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

หากส่งคืนรายการอุปกรณ์ GPU แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

แมคโอเอส

1. ความต้องการของระบบ

  • macOS 10.12.6 (Sierra) หรือสูงกว่า (64 บิต)

ขณะนี้ไม่มีการรองรับ GPU อย่างเป็นทางการสำหรับการรัน TensorFlow บน MacOS คำแนะนำต่อไปนี้ใช้สำหรับการทำงานบน CPU

2. ตรวจสอบเวอร์ชั่น Python

ตรวจสอบว่าสภาพแวดล้อม Python ของคุณได้รับการกำหนดค่าแล้วหรือไม่:

python3 --version
python3 -m pip --version

3. ติดตั้ง TensorFlow

TensorFlow ต้องใช้ pip เวอร์ชันล่าสุด ดังนั้นควรอัปเกรดการติดตั้ง pip เพื่อให้แน่ใจว่าคุณกำลังใช้เวอร์ชันล่าสุด

pip install --upgrade pip

จากนั้นติดตั้ง TensorFlow ด้วย pip

pip install tensorflow

4. ตรวจสอบการติดตั้ง

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

หากส่งคืนเทนเซอร์ แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

Windows พื้นเมือง

1. ความต้องการของระบบ

  • Windows 7 หรือสูงกว่า (64 บิต)

2. ติดตั้ง Microsoft Visual C++ แบบแจกจ่ายต่อได้

ติดตั้ง Microsoft Visual C++ Redistributable สำหรับ Visual Studio 2015, 2017 และ 2019 ตั้งแต่เวอร์ชัน TensorFlow 2.1.0 เป็นต้นไป จำเป็นต้องมีไฟล์ msvcp140_1.dll จากแพ็คเกจนี้ (ซึ่งอาจไม่ได้มาจากแพ็คเกจที่แจกจ่ายต่อได้รุ่นเก่า) การแจกจ่ายซ้ำนั้นมาพร้อมกับ Visual Studio 2019 แต่สามารถติดตั้งแยกกันได้:

  1. ไปที่ การดาวน์โหลด Microsoft Visual C++
  2. เลื่อนลงไปที่ส่วน Visual Studio 2015, 2017 และ 2019
  3. ดาวน์โหลดและติดตั้ง Microsoft Visual C++ Redistributable สำหรับ Visual Studio 2015, 2017 และ 2019 สำหรับแพลตฟอร์มของคุณ

ตรวจสอบให้แน่ใจว่า เปิดใช้งานเส้นทางยาว บน Windows

3. ติดตั้งมินิคอนด้า

Miniconda เป็นวิธีที่แนะนำสำหรับการติดตั้ง TensorFlow ด้วยการรองรับ GPU โดยจะสร้างสภาพแวดล้อมแยกต่างหากเพื่อหลีกเลี่ยงการเปลี่ยนแปลงซอฟต์แวร์ที่ติดตั้งในระบบของคุณ นี่เป็นวิธีที่ง่ายที่สุดในการติดตั้งซอฟต์แวร์ที่จำเป็น โดยเฉพาะสำหรับการตั้งค่า GPU

ดาวน์โหลด ตัวติดตั้ง Windows Miniconda ดับเบิลคลิกไฟล์ที่ดาวน์โหลดมา และปฏิบัติตามคำแนะนำบนหน้าจอ

4. สร้างสภาพแวดล้อมแบบคอนดา

สร้างสภาพแวดล้อม conda ใหม่ชื่อ tf ด้วยคำสั่งต่อไปนี้

conda create --name tf python=3.9

คุณสามารถปิดการใช้งานและเปิดใช้งานได้ด้วยคำสั่งต่อไปนี้

conda deactivate
conda activate tf

ตรวจสอบให้แน่ใจว่าได้เปิดใช้งานแล้วสำหรับการติดตั้งที่เหลือ

5. การตั้งค่า GPU

คุณสามารถข้ามส่วนนี้ได้หากคุณเรียกใช้ TensorFlow บน CPU เท่านั้น

ก่อนติดตั้ง ไดรเวอร์ NVIDIA GPU หากคุณยังไม่ได้

จากนั้นติดตั้ง CUDA, cuDNN ด้วย conda

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

6. ติดตั้ง TensorFlow

TensorFlow ต้องใช้ pip เวอร์ชันล่าสุด ดังนั้นควรอัปเกรดการติดตั้ง pip เพื่อให้แน่ใจว่าคุณกำลังใช้เวอร์ชันล่าสุด

pip install --upgrade pip

จากนั้นติดตั้ง TensorFlow ด้วย pip

# Anything above 2.10 is not supported on the GPU on Windows Native
pip install "tensorflow<2.11" 

7. ตรวจสอบการติดตั้ง

ตรวจสอบการตั้งค่า CPU:

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

หากส่งคืนเทนเซอร์ แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

ตรวจสอบการตั้งค่า GPU:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

หากส่งคืนรายการอุปกรณ์ GPU แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

วินโดวส์ WSL2

1. ความต้องการของระบบ

  • Windows 10 19044 หรือสูงกว่า (64 บิต) ซึ่งสอดคล้องกับ Windows 10 เวอร์ชัน 21H2 ซึ่งเป็นการอัปเดตเดือนพฤศจิกายน 2021

ดูเอกสารต่อไปนี้เพื่อ:

2. การตั้งค่า GPU

คุณสามารถข้ามส่วนนี้ได้หากคุณเรียกใช้ TensorFlow บน CPU เท่านั้น

ติดตั้ง ไดรเวอร์ NVIDIA GPU หากคุณยังไม่ได้ติดตั้ง คุณสามารถใช้คำสั่งต่อไปนี้เพื่อตรวจสอบว่ามีการติดตั้งแล้ว

nvidia-smi

3. ติดตั้ง TensorFlow

TensorFlow ต้องใช้ pip เวอร์ชันล่าสุด ดังนั้นควรอัปเกรดการติดตั้ง pip เพื่อให้แน่ใจว่าคุณกำลังใช้เวอร์ชันล่าสุด

pip install --upgrade pip

จากนั้นติดตั้ง TensorFlow ด้วย pip

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

4. ตรวจสอบการติดตั้ง

ตรวจสอบการตั้งค่า CPU:

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

หากส่งคืนเทนเซอร์ แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

ตรวจสอบการตั้งค่า GPU:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

หากส่งคืนรายการอุปกรณ์ GPU แสดงว่าคุณติดตั้ง TensorFlow สำเร็จแล้ว

ตำแหน่งแพ็คเกจ

กลไกการติดตั้งบางอย่างจำเป็นต้องมี URL ของแพ็คเกจ TensorFlow Python ค่าที่คุณระบุจะขึ้นอยู่กับเวอร์ชัน Python ของคุณ

เวอร์ชัน URL
ลินุกซ์
รองรับ GPU หลาม 3.9 https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 CPU เท่านั้น https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU หลาม 3.10 https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 CPU เท่านั้น https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU หลาม 3.11 https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 CPU เท่านั้น https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
macOS (ซีพียูเท่านั้น)
หลาม 3.9 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp39-cp39-macosx_10_15_x86_64.whl
หลาม 3.10 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp310-cp310-macosx_10_15_x86_64.whl
หลาม 3.11 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp311-cp311-macosx_10_15_x86_64.whl
หน้าต่าง
Python 3.9 CPU เท่านั้น https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp39-cp39-win_amd64.whl
Python 3.10 CPU เท่านั้น https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp310-cp310-win_amd64.whl
Python 3.11 CPU เท่านั้น https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp311-cp311-win_amd64.whl