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

เรียก backward() เพื่อรับเกรเดียนต์

เติมค่า .grad ด้วยโค้ดหนึ่งบรรทัด

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

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

One Line to Differentiate

Once your graph is built, a single call does all the calculus. Run backward() on your output and gradients flow back to every input.

Start From a Scalar

You call backward on the final number you want to minimize, usually the loss. It must be a single scalar so autograd knows where to start.

y = x ** 2
y.backward()

Gradients Land in .grad

After backward runs, each leaf tensor's .grad holds the derivative of the output with respect to that tensor. The math is already done.

print(x.grad)

A Quick Check by Hand

For y equal to x squared, calculus says the derivative is 2x. At x equal to 2 that is 4, and PyTorch fills x.grad with exactly 4.0.

Backward Walks the Graph

backward() travels the graph from output to inputs, applying the chain rule at every node. You wrote only the forward math and got all of this free.

Gradients for Many Inputs

If your output depends on several tracked tensors, one backward call fills the .grad of each of them at once. That is how whole models update.

loss = (a * b + c).sum()
loss.backward()

The Graph Is Spent

By default the graph is freed after backward to save memory, so calling it twice errors. You build a fresh graph on the next forward pass.

Keep It If You Must

Need to call backward again on the same graph? Pass retain_graph equals true. You rarely need this, so skip it unless an error tells you to.

loss.backward(retain_graph=True)

Non-Scalar Outputs

If your output is a vector, backward needs a gradient argument saying how to weight each element. Most of the time you sum to a scalar instead.

This Is the Backward Pass

That single backward call is the famous backward pass of training. Forward computes predictions, backward computes how to fix the weights.

From Gradients to Learning

The numbers in .grad tell each weight which way to move. An optimizer reads them and nudges the weights to lower the loss a little.

Quick Check

Make sure backward is clear.

Recap

Call backward() on a scalar loss and autograd applies the chain rule across the graph, filling each tensor's .grad with its derivative. That is the backward pass. 🔁

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

บทเรียน “เรียก backward() เพื่อรับเกรเดียนต์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “เรียก backward() เพื่อรับเกรเดียนต์”

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

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

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

บทเรียน “เรียก backward() เพื่อรับเกรเดียนต์” ใช้เวลานานแค่ไหน

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

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

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

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

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