LESSON 32 OF 52 · CHAPTER 04
Vector Probabilities
Explicit weight normalization, softmax, and probability checks
08-vector-probabilities.srp
pure funcra normalize_weights(values: vector) -> vector
needs len(values) > 0;
needs values == clamp(values, 0.0, 1e308);
needs sum(values) > 0.0;
ensures sum(result) > 0.999999999999 and sum(result) < 1.000000000001;
{
return scale(values, 1.0 / sum(values));
}
print(normalize_weights([2.0, 3.0, 5.0]));
print(softmax([1000.0, 1000.0, 1000.0, 1000.0]));
print(softmax([-1e308, 1e308]));
bind probabilities = softmax([1.0, 2.0, 3.0]);
print(sum(probabilities) > 0.999999999999 and sum(probabilities) < 1.000000000001);
print(probabilities[0] < probabilities[1] and probabilities[1] < probabilities[2]);
stdout
[0.2, 0.30000000000000004, 0.5]
[0.25, 0.25, 0.25, 0.25]
[0.0, 1.0]
true
true
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
Explicit weight normalization, softmax, and probability checks
Expected behavior
Two probability vectors, stable extreme-score results and true tests
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.
संभाव्यता जैसा वेट सामान्यीकरण
normalize_weights सकारात्मक वज़न का योग लेकर उन्हें विभाजित करता है और ensures से कुल योग लगभग 1 होने की शर्त रखता है। softmax स्कोर को संख्यात्मक संभाव्यता-वेक्टर में बदलता है; बहुत बड़े स्कोर पर स्थिर गणना भी जाँची गई है।
आउटपुट: पहली पंक्ति में [0.2, 0.30000000000000004, 0.5] है। 0.3 के बदले लंबा दशमलव floating-point प्रदर्शन के कारण है; उसके बाद चार बराबर स्कोरों के लिए चार 0.25 मान और कुछ सत्य-जाँचें हैं।
अभ्यास: योग लगभग 1 जाँचने के लिए एक छोटा tolerance क्यों प्रयुक्त है, बिल्कुल == 1.0 क्यों नहीं?
Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.
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/04-collections/08-vector-probabilities.srp
scarp exec examples/04-collections/08-vector-probabilities.srpThe website does not run code. Read the CLI guide ↗
Pinned compiler: Scarp v0.0.6-alpha.3
· ceff07d9014f. No simulated output.