ติดตั้ง 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'))"

MacOS

# 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 มีดังต่อไปนี้:

  • การ์ด 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 7 หรือสูงกว่า (64 บิต) (ไม่รองรับ GPU หลัง TF เวอร์ชัน 2.10)
  • Windows WSL2 - Windows 10 เวอร์ชัน 19044 หรือสูงกว่า (64 บิต)

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

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

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

ลินุกซ์

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

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

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

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

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

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

nvidia-smi

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

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

ไปยังไดเร็กทอรีสภาพแวดล้อมเสมือนที่คุณต้องการ แล้วสร้างสภาพแวดล้อมเสมือนใหม่ชื่อ 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'))"

MacOS

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++ 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

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

conda create --name tf python=3.9

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

conda deactivate
conda activate tf

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

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

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

ก่อนอื่น โปรดติดตั้ง ไดรเวอร์การ์ดจอ 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 เท่านั้น

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

การรองรับเวอร์ชัน Python

เวอร์ชั่น URL
ลินุกซ์ x86
การรองรับ GPU ใน Python 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-manylinux_2_27_x86_64.whl
Python 3.10 ใช้งานเฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp310-cp310-manylinux_2_27_x86_64.whl
การรองรับ GPU ใน Python 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-manylinux_2_27_x86_64.whl
Python 3.11 ทำงานเฉพาะบน CPU เท่านั้น https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp311-cp311-manylinux_2_27_x86_64.whl
การรองรับ GPU ใน Python 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-manylinux_2_27_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/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-manylinux_2_27_x86_64.whl
Python 3.13 ทำงานเฉพาะ CPU https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow_cpu-2.21.0-cp313-cp313-manylinux_2_27_x86_64.whl
ลินุกซ์ อาร์ม64 (เฉพาะซีพียู)
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-manylinux_2_27_aarch64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-manylinux_2_27_aarch64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-manylinux_2_27_aarch64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-manylinux_2_27_aarch64.whl
macOS x86 (เฉพาะ CPU)
ข้อควรระวัง : TensorFlow 2.16 เป็นเวอร์ชัน สุดท้าย ของ TensorFlow ที่รองรับ macOS x86
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp310-cp310-macosx_10_15_x86_64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp311-cp311-macosx_10_15_x86_64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.2/tensorflow-2.16.2-cp312-cp312-macosx_10_15_x86_64.whl
macOS Arm64 (เฉพาะ CPU)
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-macosx_12_0_arm64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-macosx_12_0_arm64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-macosx_12_0_arm64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-macosx_12_0_arm64.whl
ระบบปฏิบัติการ Windows (เฉพาะ CPU)
ไพธอน 3.10 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp310-cp310-win_amd64.whl
ไพธอน 3.11 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp311-cp311-win_amd64.whl
ไพธอน 3.12 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp312-cp312-win_amd64.whl
ไพธอน 3.13 https://storage.googleapis.com/tensorflow/versions/%222.21.0%22/tensorflow-2.21.0-cp313-cp313-win_amd64.whl