t27.aiРусский

A bend at zero

You will learn

Why a ReLU needs a test with a negative input before anyone can say it bends.

Recording pending: it waits on tri test and tri mutate plant from gHashTag/t27#7400, the two commands the recording runs, and until then the widget below is a placeholder that shows no run. A neuron passes its sum through an activation, and in gft_relu.t27 that is relu(x) = max(0, x). on_comb returns 0 for zero and for any input with the sign bit set, and x itself otherwise. The header says ReLU has an exact 0/1 gradient and that a 2-layer ReLU net solves XOR, which a linear GF-T model cannot. The browser skips all 4 tests because its runner does not know assert_eq yet; the native t27c runs all 4, all pass, none vacuous. The recording lets negative inputs through, which turns relu into the identity, a straight line. Exactly one test fails, negz; no other test has a negative input. Every byte in the recording was printed by the command; only the typing is staged.

Try it

In the recording, find the changed line and the value negz expects; then in the spec frame find the comment on that line and check whether it still describes the code after the change.

Open the interactive lesson →

gft_relu.t27: recording pending
gft_relu.t27: recording pending ↗

Recording pending: waits on tri test and tri mutate plant from gHashTag/t27#7400. Until then this page is a placeholder and shows no run.

specs/ternary/gft_relu.t27

module GftRelu;
// #1764 + GF-T: a GF-T ReLU activation -- relu(x) = max(0, x). For a signed GF-T16
// input, returns x if x > 0 else 0 (raw 0). Unlike the sign/trit quantizer, ReLU
// has an EXACT 0/1 gradient (no straight-through estimate needed), so it enables
// clean multi-layer backprop on GF-T (see tools/gft_deep_demo.py: a 2-layer ReLU
// net solves XOR, which a linear GF-T model cannot).
//
// Input: x signed GF-T16 (u32). Output: max(0,x) as GF-T16 (u32).

fn on_comb(x: u32) -> u32 {
    if (x == 0) { return 0; }
    if ((x >> 16) == 1) { return 0; }   // negative -> 0
    return x;                           // positive -> identity
}

// relu(+1.0) = +1.0 (0x5000).
test pos { assert_eq(on_comb(20480), 20480); }
// relu(-1.0) = 0.
test negz { assert_eq(on_comb(86016), 0); }
// relu(0) = 0.
test zero { assert_eq(on_comb(0), 0); }
// relu(+2.0) = +2.0 (0x5200).
test pos2 { assert_eq(on_comb(20992), 20992); }
endmodule

Open the lesson's spec in the player ↗

All lessons