LogSoftmax

struct LogSoftmax (
T
size_t dim = 2
) {
Variable!(T, dim, HostStorage) hy;
Variable!(T, dim, DeviceStorage) dy;
}

Examples

test logsoftmax simple case, gradcheck and cpu/cuda equality

1 import grain.testing;
2 import std.typecons;
3 import numir;
4 import mir.ndslice;
5 import mir.math;
6 auto e = log(exp(-1.0) + exp(2.0) + exp(3.0));
7 auto xs = [[-1.0f, 2.0f, 3.0f], [-1.0f, 2.0f, 3.0f], [-1.0f, 2.0f, 3.0f]].nparray;
8 LogSoftmax!float hfunc;
9 auto _hx = xs.variable;
10 auto _hy = hfunc.forward(_hx);
11 assert(approxEqual(_hy.sliced, xs - e));
12 
13 auto hx = uniform!float(2, 2).slice.variable;
14 auto hy = hfunc.forward(hx);
15 auto hgy = uniform!float(2, 2).slice.variable;
16 auto hgx = hfunc.backward(hgy);
17 gradCheck(hfunc, hx, hgy, 1e-3, 1e-3, 1e-3);
18 
19 version (grain_cuda) {
20     alias Storage = DeviceStorage;
21     auto func = LogSoftmax!float();
22     auto dx = hx.to!Storage;
23     auto dy = func.forward(dx);
24     assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced));
25     auto dgy = hgy.to!Storage;
26     auto dgx = func.backward(dgy);
27     assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced));
28 }

See Also

Meta