test scale in 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 e = [[-2.0f, 4.0f, 6.0f], [2.0f, 0.2f, 0.0f]].nparray; 8 auto xs = [[-1.0f, 2.0f, 3.0f], [1.0f, 0.1f, 0.0f]].nparray; 9 auto hfunc = Scale!(float, 2)(2f); 10 auto _hx = xs.variable; 11 auto _hy = hfunc.forward(_hx); 12 assert(approxEqual(_hy.sliced, e)); 13 14 auto hx = uniform!float(2, 2).slice.variable; 15 auto hy = hfunc.forward(hx); 16 auto hgy = uniform!float(2, 2).slice.variable; 17 auto hgx = hfunc.backward(hgy); 18 gradCheck(hfunc, hx, hgy); // , 1e-3, 1e-3, 1e-3); 19 20 version (grain_cuda) { 21 auto dfunc = Scale!(float, 2)(2f); 22 auto dy = dfunc.forward(hx.to!DeviceStorage); 23 assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced)); 24 auto dgx = dfunc.backward(hgy.to!DeviceStorage); 25 assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced)); 26 }
y = alpha * x