ความแม่นยำผสมด้วย autocast และ GradScaler
การคำนวณความแม่นยำครึ่งหนึ่งเพื่อเพิ่มความเร็วอย่างมาก
ความแม่นยำผสมด้วย autocast และ GradScaler เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Mixed Precision Means
By default PyTorch does math in 32-bit floats. Mixed precision runs many operations in 16-bit instead, which is faster and uses far less memory.
Why Half Precision Is Faster
Modern GPUs have special tensor cores tuned for 16-bit math. Feeding them half-precision data can speed up training two or three times with little accuracy loss.
The Catch With 16-Bit
Half precision has a tiny range, so very small gradient values can round down to zero. That silent underflow stalls learning if you do nothing about it.
Meet autocast
Wrap your forward pass in autocast and PyTorch picks a safe precision per operation automatically. You never cast tensors by hand.
with torch.autocast(device_type='cuda'):
out = model(x)What autocast Wraps
Put only the forward pass and loss inside autocast. The backward call stays outside the block, where PyTorch handles precision for you.
with torch.autocast(device_type='cuda'):
out = model(x)
loss = loss_fn(out, y)Meet GradScaler
A GradScaler fixes underflow by multiplying the loss by a large factor before backward, so small gradients stay big enough to survive in 16-bit.
scaler = torch.cuda.amp.GradScaler()Scale the Loss
Instead of loss.backward(), call scaler.scale(loss).backward(). The scaler inflates the loss first so the resulting gradients do not vanish.
scaler.scale(loss).backward()Step Through the Scaler
Run the optimizer with scaler.step, which quietly unscales the gradients back to normal size before applying the update.
scaler.step(optimizer)Update the Scale Factor
Finish each step with scaler.update(). It grows the scale when things are stable and shrinks it if it ever spots an overflow.
scaler.update()The Full AMP Step
Together these calls form one clean AMP iteration: zero grads, autocast forward, scaled backward, scaler step, then update.
optimizer.zero_grad()
with torch.autocast(device_type='cuda'):
loss = loss_fn(model(x), y)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()When To Reach For It
Mixed precision shines on a recent NVIDIA GPU with large batches. On a plain CPU it gives little benefit, so save it for real training runs.
Quick Check
You enabled autocast but tiny gradients keep vanishing. What tool fixes that?
Recap
Wrap the forward in autocast for fast 16-bit math, then use a GradScaler to scale, step, and update so tiny gradients survive. ⚡
คำถามที่พบบ่อย
บทเรียน “ความแม่นยำผสมด้วย autocast และ GradScaler” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความแม่นยำผสมด้วย autocast และ GradScaler” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความแม่นยำผสมด้วย autocast และ GradScaler”
การคำนวณความแม่นยำครึ่งหนึ่งเพื่อเพิ่มความเร็วอย่างมาก คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ความแม่นยำผสมด้วย autocast และ GradScaler” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความแม่นยำผสมด้วย autocast และ GradScaler
- สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่
- วิเคราะห์คอขวด
- ลดการใช้หน่วยความจำของ GPU