0Pricing
Deep Learning Academy · บทเรียน

สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่

จำลองแบตช์ขนาดใหญ่บน GPU ขนาดเล็ก

สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่ เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Big-Batch Problem

Large batches often train more smoothly, but they also need lots of GPU memory. A small card simply cannot hold a giant batch at once.

The Core Trick

Gradient accumulation splits one big batch into small chunks. You add up their gradients and update once, as if the whole batch ran together.

Gradients Already Accumulate

PyTorch adds each backward pass into .grad rather than replacing it. This default behavior is exactly what accumulation relies on.

Pick an Accumulation Count

Choose how many mini-batches make one update. With accum_steps of four, four small batches behave like one batch four times larger.

accum_steps = 4

Do Not Zero Every Step

The key change is timing: zero_grad only at the start of an accumulation cycle, not after every single mini-batch.

if step % accum_steps == 0:
    optimizer.zero_grad()

Scale the Loss

Divide each mini-batch loss by accum_steps before backward. This keeps the average gradient identical to running the full batch at once.

loss = loss_fn(model(x), y) / accum_steps
loss.backward()

Step Only When Full

After enough mini-batches pile up, call optimizer.step. The accumulated gradients now reflect the whole large batch.

if (step + 1) % accum_steps == 0:
    optimizer.step()

The Full Pattern

Put it together: scale the loss, backward every step, but only step and zero once per cycle. The loop stays simple.

for step, (x, y) in enumerate(loader):
    loss = loss_fn(model(x), y) / accum_steps
    loss.backward()
    if (step + 1) % accum_steps == 0:
        optimizer.step()
        optimizer.zero_grad()

Memory Stays Small

You only ever hold one mini-batch in memory at a time. That is why a modest GPU can mimic a batch many times its real capacity.

The Trade-Off

Accumulation trades time for memory: more forward and backward passes per update mean each effective batch takes a little longer to finish.

Mind Batch Norm

Batch norm still sees only the small mini-batch, so its statistics are noisier than a true large batch would produce. Keep that in mind.

Quick Check

You accumulate over 4 mini-batches. When should you call optimizer.step()?

Recap

Split a big batch into chunks, divide the loss by accum_steps, backward every chunk, and step only once per cycle to fake a large batch on small memory. 🧮

คำถามที่พบบ่อย

บทเรียน “สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่”

จำลองแบตช์ขนาดใหญ่บน GPU ขนาดเล็ก คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม

ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ความแม่นยำผสมด้วย autocast และ GradScaler
  2. สะสมเกรเดียนต์สำหรับแบตช์ขนาดใหญ่
  3. วิเคราะห์คอขวด
  4. ลดการใช้หน่วยความจำของ GPU
← กลับไปที่ Deep Learning Academy