AddBias

TODO generalize to broadcastable addition

struct AddBias (
T
) {}

Examples

test variable.backward

1 import std.typecons;
2 import grain.testing;
3 import mir.ndslice;
4 
5 grain.autograd.backprop = true;
6 
7 NegativeLogLikelihood!(float, int) func;
8 auto hx = [[0.2f, 0.4f, 0.4f], [0.1f, 0.5f, 0.4f], [0.1f, 0.5f, 0.4f]].variable;
9 hx.requiresGrad = true;
10 auto ht = [1, 0, func.ignoreIndex].variable;
11 auto hl = func.applyForward(hx, ht);
12 // hl.bprop.writeln;
13 assert(func._normalize == 0.5);
14 assert(hl.sliced == [-(0.4f + 0.1f + 0.0f) / 2]);
15 auto u = UntypedVariable(1.0f.variable);
16 hl.backward(&u);
17 // hl.bprop.inputs[0].writeln;
18 assert(hx.grad[].sliced(3, 3) == [[0.0, -0.5, 0.0], [-0.5, 0.0, 0.0], [0.0, 0.0, 0.0]]);
19 // assert(!hgx[1].defined);

Meta