code wiki / _hdl_build / nx_ltr.nx
nx_ltr.nx source
↩ module page · 36 lines · 1740 B
1// nx_ltr.nx -- R-LTR of the onsite-search S-class ladder: SOVEREIGN learning-to-rank (LIBRARY). Learn a ranking
2// function that COMBINES features (BM25 + vector cosine + click-CTR) from labeled data, instead of trusting any
3// single signal -- the LTR idea (cited srch_ltr.raw). This is the CLASSICAL LINEAR model trained by a pairwise
4// perceptron (Lloyd/Rosenblatt): for each (relevant, irrelevant) pair, if the model scores them wrong, nudge the
5// weights toward the relevant one. Integer, deterministic, no-float, no external weights. HONEST SCOPE: linear
6// LTR; DEEP-NEURAL LTR (a trained net) is the weight-gated extension -- the same frontier as the semantic model.
7//
8// exports: vr_ltr_score, vr_ltr_train. license_tier: ORIGINAL
9import "nx_syscalls.nx"
10
11// linear score = sum_f w[f] * feat[f]
12func vr_ltr_score(w: *i64, feat: *i64, F: i64) -> i64 {
13 var s: i64 = 0; var f: i64 = 0
14 while f < F { s = s + w[f] * feat[f]; f = f + 1 }
15 return s
16}
17
18// pairwise-perceptron train: rel[p*F..] should outscore irr[p*F..] for each of `npairs` pairs. On a violation
19// (score(rel) <= score(irr)) nudge w += (rel - irr). `iters` passes. Weights w[F] updated in place.
20func vr_ltr_train(w: *i64, F: i64, rel: *i64, irr: *i64, npairs: i64, iters: i64) -> i64 {
21 var it: i64 = 0
22 while it < iters {
23 var p: i64 = 0
24 while p < npairs {
25 let rf: *i64 = ((rel as i64) + p*F*8) as *i64
26 let nf: *i64 = ((irr as i64) + p*F*8) as *i64
27 if vr_ltr_score(w, rf, F) <= vr_ltr_score(w, nf, F) {
28 var f: i64 = 0
29 while f < F { w[f] = w[f] + (rf[f] - nf[f]); f = f + 1 }
30 }
31 p = p + 1
32 }
33 it = it + 1
34 }
35 return 0
36}