LESSON 43 OF 52 · CHAPTER 06
Classification Gradient
One-hot target and explicit categorical-loss gradient
02-classification-gradient.srp
// One-hot target construction and explicit categorical-loss gradient.
// No automatic differentiation or training loop is implied.
bind logits = [0.0, 0.0];
bind class_id = 1;
print(onehot(3, class_id));
print(lossgrad(logits, class_id));
print(crossentropy([3.0], 0));
stdout
[0.0, 1.0, 0.0]
[0.5, -0.5]
0.0
Write what you expect this program to print. Check prediction for a line-level hint without revealing the answer, or reveal the verified output when ready. This checks displayed text only; it does not run Scarp or save your input.
UNDERSTAND THE CODE
Explanation & practice
What this example teaches
One-hot target and explicit categorical-loss gradient
Expected behavior
[0.0, 1.0, 0.0], [0.5, -0.5], 0.0
Practice: Read the source before looking at the output. Predict the values and their order, then use the pinned compiler to execute this lesson and compare the exact fixture. Identify which operation produces the result.
क्लास संकेत और स्पष्ट ग्रेडिएंट
onehot(3, class_id) तीन वर्गों में चुने हुए वर्ग का संकेत देता है। lossgrad(logits, class_id) वर्तमान कम्पाइलर का एक स्पष्ट संख्यात्मक ग्रेडिएंट फ़ंक्शन है। इससे अपने आप ट्रेनिंग लूप या autodiff उपलब्ध होने का अर्थ नहीं निकलता।
आउटपुट: पहले [0.0, 1.0, 0.0], फिर [0.5, -0.5], फिर 0.0।
अभ्यास: class_id = 1 होने पर वन-हॉट वेक्टर में 1 किस इंडेक्स पर है?
Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.
SOURCE-BACKED SELF-CHECK · स्रोत-आधारित अभ्यास
Quick checkpoint / त्वरित अभ्यास
What gradient does lossgrad([0.0, 0.0], 1) print?lossgrad([0.0, 0.0], 1) कौन-सा gradient प्रिंट करता है?
Reveal the source-backed answer / सही उत्तर देखें
[0.5, -0.5] — Equal logits imply probabilities of 0.5 for both classes. The loss gradient subtracts the one-hot target for class 1, producing [0.5, -0.5].[0.5, -0.5] — समान logits के लिए दोनों वर्गों की संभावना 0.5 होती है। class 1 के one-hot target को घटाने पर gradient [0.5, -0.5] मिलता है।
This question checks understanding of a recorded compiler output. It does not run Scarp in the browser. यह प्रश्न सत्यापित आउटपुट पर आधारित है; ब्राउज़र में Scarp नहीं चलता।
LEARNING PROGRESS
Keep your place
Mark this lesson complete when you are ready.
Only saved in this browser. No account or tracking.
TRY IT LOCALLY
Verify, then execute
Use the pinned compiler from the course root. Keep any imported _support files next to the source.
scarp verify examples/06-ai-foundations/02-classification-gradient.srp
scarp exec examples/06-ai-foundations/02-classification-gradient.srpThe website does not run code. Read the CLI guide ↗
Pinned compiler: Scarp v0.0.6-alpha.3
· ceff07d9014f. No simulated output.