Ranked Batch
ranked_batch.nr95 lines
ranked_batch.nr· neuro
// Ordering a tensor axis alongside the rest of the tensor surface. Integration
// example: `.sort()`, `.argsort()` and `.topk(k:, axis:)`, named dimensions, reductions
// with `axis:`, slicing and indexing, `.t()`, in-place `+=`, and a struct with a tensor
// field. An argsort index is an ordinary integer, so it reads back into the tensor that
// produced it. That turns "which class scored highest" into a label lookup, which `.max()`
// alone cannot do: it gives the best score, never its position.
// A scoreboard owns its logits and reads them without giving them up.
struct Batch {
logits: Tensor<f64, [batch: 3, classes: 4]>
}
impl Batch {
// A selection reads its receiver, so `&self` is enough: the field is never moved.
func winners(&self) -> Tensor<i32, [batch: 3]> {
val ranked: Tensor<i32, [batch: 3, classes: 4]> = self.logits.argsort(descending: true)
// Column 0 of a descending argsort is the best class for each row.
ranked[.., 0]
}
func peak(&self) -> Tensor<f64, [batch: 3]> {
self.logits.max(axis: classes)
}
// A confidence margin: how far the best score beats the runner-up, per row. It needs
// the two best of each row, which is what `.topk` selects and what no single
// reduction can answer.
func margin(&self) -> f64 {
val (top, _) = self.logits.topk(k: 2, axis: classes)
mut total = 0.0
for row in 0..3 {
total += (top[row, 0] - top[row, 1])
}
total / 3.0
}
// A shape cast CONSUMES its receiver, so the field is cloned rather than moved out of
// the struct that owns it: transposing makes the classes the rows, and the same
// default axis then orders each class ACROSS the batch.
func by_class(&self) -> Tensor<f64, [classes: 4, batch: 3]> {
self.logits.clone().t().sort()
}
}
func main() -> i32 {
val batch = Batch {
logits: [
[0.5, 2.5, 1.0, 0.25],
[3.0, 0.5, 0.75, 1.5],
[1.25, 1.0, 0.5, 4.0]
]
}
val picks: Tensor<i32, [batch: 3]> = batch.winners()
val peaks: Tensor<f64, [batch: 3]> = batch.peak()
for row in 0..3 {
val at = row as u64
println("row {row}: class {picks[at]} at {peaks[at]:.2}")
}
println("mean margin = {batch.margin():.3}")
val sorted: Tensor<f64, [classes: 4, batch: 3]> = batch.by_class()
println("class 0 across the batch = {sorted[0, 0]:.2}, {sorted[0, 1]:.2}, {sorted[0, 2]:.2}")
// Counting down, so the last class is reported first. An argsort pick is an
// ordinary integer, so it compares against a class number directly.
for class in (0..4).rev() {
mut rows: i32 = 0
for row in 0..3 {
val at = row as u64
if picks[at] == class {
rows += 1
}
}
println("class {class}: {rows} row(s)")
}
// In-place `+=` on the reduction's own result: every peak is lifted by the same
// floor, in the buffer `.max(axis:)` already allocated.
mut lifted: Tensor<f64, [batch: 3]> = batch.peak()
val floor: Tensor<f64, [batch: 3]> = [0.5, 0.5, 0.5]
lifted += floor
println("lifted peaks = {lifted[0]:.2}, {lifted[1]:.2}, {lifted[2]:.2}")
// A whole-tensor reduction over each piece, folded into one exit code.
val total = batch.logits.sum()
val mean = batch.logits.mean()
println("total = {total:.2}, mean = {mean:.3}, lifted = {lifted.sum():.2}")
val picked = picks[0] * 100 + picks[1] * 10 + picks[2]
println("picked = {picked}")
picked + (lifted.sum() as i32) + (total as i32)
}
neuroc run ranked_batch.nr
row 0: class 1 at 2.50
row 1: class 0 at 3.00
row 2: class 3 at 4.00
mean margin = 1.917
class 0 across the batch = 0.50, 1.25, 3.00
class 3: 1 row(s)
class 2: 0 row(s)
class 1: 1 row(s)
class 0: 1 row(s)
lifted peaks = 3.00, 3.50, 4.50
total = 16.75, mean = 1.396, lifted = 11.00
picked = 103