store grain.autograd.BackProp object in returned variables from forward function
type-erased version of backward function used in grain.autograd.BackProp object
test reciprocal simple case, gradcheck and cpu/cuda equality
import grain.testing; import std.typecons; import numir; import mir.ndslice; // simple case auto e = [[-1.0f, 1f / 2f, 1f / 3f], [1f, 10f, 1f / 3f]].nparray; auto xs = [[-1.0f, 2.0f, 3.0f], [1.0f, 0.1f, 3.0f]].nparray; Reciprocal!(float, 2) hfunc; auto _hx = xs.variable; auto _hy = hfunc.forward(_hx); assert(approxEqual(_hy.sliced, e)); auto hx = uniform!float(2, 2).slice.variable; auto hy = hfunc.forward(hx); auto hgy = uniform!float(2, 2).slice.variable; auto hgx = hfunc.backward(hgy); gradCheck(hfunc, hx, hgy, 1e-3, 5e-2, 5e-2); version (grain_cuda) { Reciprocal!(float, 2) dfunc; auto dy = dfunc.forward(hx.to!DeviceStorage); assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced)); auto dgx = dfunc.backward(hgy.to!DeviceStorage); assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced)); }
y = 1 / x