LESSON 47 OF 52 · CHAPTER 06

Matrix Forward

Typed tile input, tileadd, tilescale, matvec, bias, softmax and contracts

ORIGINAL SOURCE

06-matrix-forward.srp

pure funcra forward(weights: tile, features: vector, bias: vector) -> vector
    needs width(weights) == len(features);
    needs height(weights) == len(bias);
    ensures len(result) == height(weights);
{
    return softmax(add(matvec(weights, features), bias));
}
bind identity = tile([1.0, 0.0, 0.0, 1.0], 2, 2);
bind weights = tileadd(identity, tilescale(identity, 1.0));
print(forward(weights, [1.0, 1.0], [0.0, 0.0]));
EXPECTED OUTPUT

stdout

Verified
[0.5, 0.5]

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Typed tile input, tileadd, tilescale, matvec, bias, softmax and contracts

Expected behavior

[0.5, 0.5]

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.

बायस सहित सरल फ़ॉरवर्ड गणना

pure funcra forward में matvec का परिणाम बायस से जोड़ा जाता है और softmax से सामान्यीकृत किया जाता है। tileadd और tilescale छोटे वेट बनाते हैं। needs और ensures मैट्रिक्स व वेक्टर के आकार की शर्तें जाँचते हैं।

आउटपुट: दो बराबर स्कोरों से [0.5, 0.5] मिलता है। यहाँ कोई मॉडल लोड, GPU संचालन या स्वयं वेट सीखना नहीं होता।

अभ्यास: दोनों अंतिम स्कोर बराबर होने पर softmax में दोनों परिणाम 0.5 क्यों हैं?

Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.

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/06-matrix-forward.srp scarp exec examples/06-ai-foundations/06-matrix-forward.srp

The website does not run code. Read the CLI guide ↗

Pinned compiler: Scarp v0.0.6-alpha.3 · ceff07d9014f. No simulated output.