CPU กับ GPU กับ MPS: เลือกอุปกรณ์
ตรวจหา CUDA, Apple MPS หรือย้อนกลับไปใช้ CPU
CPU กับ GPU กับ MPS: เลือกอุปกรณ์ เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Where Math Happens
Every tensor lives on a device: the CPU, a GPU, or Apple's MPS. The device decides how fast your model trains. ⚡
The CPU Always Works
Your CPU is the safe default. It runs every PyTorch operation, just more slowly on the heavy matrix math that big networks demand.
The GPU Goes Fast
A GPU does thousands of multiplications in parallel, making it far faster for deep learning. PyTorch reaches NVIDIA GPUs through CUDA.
Apple Silicon Has MPS
On a Mac with Apple Silicon, MPS taps the built-in GPU. It is the fast path for M-series chips without any NVIDIA hardware.
Detect CUDA
Ask PyTorch whether an NVIDIA GPU is available before you use one. cuda.is_available returns True or False so you never assume.
torch.cuda.is_available()Detect MPS
On a Mac, check the Apple backend the same way. mps.is_available tells you if the M-series GPU is ready to use.
torch.backends.mps.is_available()Pick the Best Device
A clean pattern tries CUDA first, then MPS, then falls back to CPU. This device string adapts to whatever machine runs your code.
device = 'cuda' if torch.cuda.is_available() else 'cpu'Move a Tensor
Send data to the chosen device with .to(device). The tensor's numbers are unchanged, but its math now runs there.
x = torch.tensor([1.0, 2.0]).to(device)Keep Everything Together
Your model and your data must share one device. Mixing CPU and GPU tensors throws a device mismatch error during the forward pass.
Check Where a Tensor Lives
Not sure where data sits? Read the .device attribute. It prints cpu, cuda, or mps so you can confirm placement at a glance.
print(x.device)When to Use Which
Use the GPU for real training, and the CPU for quick tests or tiny data. The fastest device is the one your hardware actually has.
Quick Check
Pick the right call for a Mac with Apple Silicon.
Recap
You learned to detect CUDA, MPS, or CPU, pick the best one, and move tensors there. Same code, fastest hardware available. 🚀
คำถามที่พบบ่อย
บทเรียน “CPU กับ GPU กับ MPS: เลือกอุปกรณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “CPU กับ GPU กับ MPS: เลือกอุปกรณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “CPU กับ GPU กับ MPS: เลือกอุปกรณ์”
ตรวจหา CUDA, Apple MPS หรือย้อนกลับไปใช้ CPU คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “CPU กับ GPU กับ MPS: เลือกอุปกรณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ติดตั้ง PyTorch และตรวจสอบการนำเข้า
- CPU กับ GPU กับ MPS: เลือกอุปกรณ์
- โน้ตบุ๊ก สคริปต์ และค่าเริ่มต้นที่ทำซ้ำได้
- torch.tensor แรกของคุณ