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

อ่านและล้างค่า .grad

เหตุใดเกรเดียนต์จึงสะสมและต้องรีเซ็ต

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

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

The .grad Attribute

After backward, every trained tensor stores its gradient in .grad. Reading it tells you how the loss responds to changes in that tensor.

print(weight.grad)

Gradients Start as None

Before any backward call, .grad is None, not zero. PyTorch only allocates it once the first set of gradients actually arrives.

Gradients Accumulate

Here is the surprise: backward adds to .grad rather than replacing it. Call backward twice without clearing and the numbers pile up.

Why Accumulation Exists

This adding behavior is on purpose. It lets you sum gradients from several mini-batches before one update, handy for simulating a larger batch.

The Hidden Bug

Forget to clear and old gradients poison your next step, so the model trains on the wrong numbers. This silent bug trips up almost everyone once.

Zero Them Each Step

The fix is to reset gradients before each backward. With an optimizer you simply call zero_grad() at the top of every training step.

optimizer.zero_grad()

The Right Order

The loop rhythm is fixed: zero_grad, forward, loss, backward, step. Zeroing first guarantees each step uses only this batch's gradients.

optimizer.zero_grad()
loss.backward()
optimizer.step()

Clearing Without an Optimizer

No optimizer yet? You can null the gradients yourself by setting each tensor's .grad back to None before the next backward call.

w.grad = None

set_to_none Is Cheaper

Newer code prefers zero_grad(set_to_none=True). Setting grads to None instead of filling zeros saves a little memory and time.

Reading Grads to Debug

Peeking at .grad is great for debugging. All zeros may mean a dead neuron, and huge values warn of exploding gradients before training blows up.

A Habit Worth Forming

Make zeroing automatic in your head. Every reliable training loop clears gradients first, so the model only ever learns from the current batch.

Quick Check

Check the accumulation gotcha.

Recap

Gradients live in .grad and accumulate across backward calls, so you must clear them each step with zero_grad. Forgetting that quietly breaks training. 🧹

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

บทเรียน “อ่านและล้างค่า .grad” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “อ่านและล้างค่า .grad”

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

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

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

บทเรียน “อ่านและล้างค่า .grad” ใช้เวลานานแค่ไหน

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

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

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

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

  1. requires_grad และกราฟการคำนวณ
  2. เรียก backward() เพื่อรับเกรเดียนต์
  3. อ่านและล้างค่า .grad
  4. torch.no_grad() สำหรับการอนุมาน
← กลับไปที่ Deep Learning Academy