Neg

y = -x

struct Neg (
T
size_t dim
) {}

Examples

test neg simple case, gradcheck and cpu/cuda equality

1 import grain.testing;
2 import std.typecons;
3 import numir;
4 import mir.ndslice;
5 
6 // simple case: 2.0 * x
7 auto xs = [[-1.0f, 2.0f, 3.0f], [1.0f, 0.1f, 0.0f]].nparray;
8 auto hfunc = Neg!(float, 2)();
9 auto _hx = xs.variable;
10 auto _hy = hfunc.forward(_hx);
11 assert(approxEqual(_hy.sliced, -xs));
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     auto dfunc = Neg!(float, 2)();
21     auto dy = dfunc.forward(hx.to!DeviceStorage);
22     assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced));
23     auto dgx = dfunc.backward(hgy.to!DeviceStorage);
24     assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced));
25 }

Meta