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

คู่มือนี้ใช้สำหรับ TensorFlow เวอร์ชันเสถียรล่าสุด สำหรับบิลด์ตัวอย่าง (nightly) ให้ใช้แพ็กเกจ 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])))"

วินโดวส์เนทีฟ

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 ต่อไปนี้:

  • การ์ด GPU NVIDIA® ที่มีสถาปัตยกรรม 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 12.0 (Monterey) หรือสูงกว่า (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 เท่านั้น

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

nvidia-smi

3. สร้างสภาพแวดล้อมเสมือนจริงด้วย venv

โมดูล venv เป็นส่วนหนึ่งของไลบรารีมาตรฐานของ Python และเป็นวิธีที่แนะนำอย่างเป็นทางการในการสร้างสภาพแวดล้อมเสมือน

ไปที่ไดเร็กทอรีสภาพแวดล้อมเสมือนที่คุณต้องการและสร้างสภาพแวดล้อม venv ใหม่ชื่อ tf ด้วยคำสั่งต่อไปนี้

python3 -m venv tf 

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

source tf/bin/activate    

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

4. ติดตั้ง TensorFlow

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

pip install --upgrade pip

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

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

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

ตรวจสอบการตั้งค่า 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 เรียบร้อยแล้ว หากไม่เป็นเช่นนั้น ให้ดำเนินการตามขั้นตอนถัดไป

6. [เฉพาะ GPU] การกำหนดค่าสภาพแวดล้อมเสมือน

หากการทดสอบ GPU ในส่วนสุดท้ายไม่สำเร็จ สาเหตุที่เป็นไปได้มากที่สุดคือระบบตรวจไม่พบส่วนประกอบ และ/หรือขัดแย้งกับการติดตั้ง CUDA ของระบบที่มีอยู่ ดังนั้นคุณจำเป็นต้องเพิ่มลิงก์สัญลักษณ์เพื่อแก้ไขปัญหานี้

  • สร้างลิงก์สัญลักษณ์ไปยังไลบรารีที่แชร์ของ NVIDIA:
pushd $(dirname $(python -c 'print(__import__("tensorflow").__file__)'))
ln -svf ../nvidia/*/lib/*.so* .
popd
  • สร้างลิงก์สัญลักษณ์ไปยัง ptxas:
ln -sf $(find $(dirname $(dirname $(python -c "import nvidia.cuda_nvcc;         
print(nvidia.cuda_nvcc.__file__)"))/*/bin/) -name ptxas -print -quit) $VIRTUAL_ENV/bin/ptxas

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

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

แมคโอเอส

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 สำเร็จแล้ว

วินโดวส์เนทีฟ

1. ข้อกำหนดของระบบ

  • Windows 7 ขึ้นไป (64 บิต)

2. ติดตั้ง Microsoft Visual C++ Redistributable

ติดตั้ง Microsoft Visual C++ Redistributable สำหรับ Visual Studio 2015, 2017 และ 2019 เริ่มต้นด้วย TensorFlow เวอร์ชัน 2.1.0 จำเป็นต้องมีไฟล์ msvcp140_1.dll จากแพ็กเกจนี้ (ซึ่งอาจไม่มีให้ในแพ็กเกจ redistributable รุ่นเก่า) แพ็กเกจ redistributable นี้มาพร้อมกับ 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

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

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

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

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

conda create --name tf python=3.9

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

conda deactivate
conda activate tf

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

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

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

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

จากนั้นติดตั้ง 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 เท่านั้น

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

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 ของแพ็กเกจ Python ของ TensorFlow ค่าที่คุณระบุจะขึ้นอยู่กับเวอร์ชัน Python ของคุณ

เวอร์ชัน URL
ลินุกซ์ x86
รองรับ GPU Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 เฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 เฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 เฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 เฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
รองรับ GPU Python 3.13 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.13 เฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow_cpu-2.20.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Linux Arm64 (เฉพาะ CPU)
ไพธอน 3.9 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/versions/2.20.0/tensorflow-2.20.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
macOS x86 (เฉพาะ CPU)
ข้อควรระวัง : TensorFlow 2.16 เป็นรุ่น TensorFlow สุดท้าย ที่รองรับ macOS x86
ไพธอน 3.9 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.16.2/tensorflow-2.16.2-cp39-cp39-macosx_10_15_x86_64.whl
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.16.2/tensorflow-2.16.2-cp310-cp310-macosx_10_15_x86_64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.16.2/tensorflow-2.16.2-cp311-cp311-macosx_10_15_x86_64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.16.2/tensorflow-2.16.2-cp312-cp312-macosx_10_15_x86_64.whl
macOS Arm64 (เฉพาะ CPU)
ไพธอน 3.9 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow-2.20.0-cp39-cp39-macosx_12_0_arm64.whl
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow-2.20.0-cp310-cp310-macosx_12_0_arm64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow-2.20.0-cp311-cp311-macosx_12_0_arm64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow-2.20.0-cp312-cp312-macosx_12_0_arm64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow-2.20.0-cp313-cp313-macosx_12_0_arm64.whl
Windows (เฉพาะ CPU)
ไพธอน 3.9 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow_cpu-2.20.0-cp39-cp39-win_amd64.whl
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow_cpu-2.20.0-cp310-cp310-win_amd64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow_cpu-2.20.0-cp311-cp311-win_amd64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow_cpu-2.20.0-cp312-cp312-win_amd64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/เวอร์ชัน/2.20.0/tensorflow_cpu-2.20.0-cp313-cp313-win_amd64.whl