1 // test CPU 2 import grain.testing; 3 import std.math : tanh; 4 import numir; 5 6 auto func = new Sigmoid!(float, 1); 7 auto hx = [-1.0f, 1.0f, 0.0f].variable; 8 gradCheck(func, hx, [0.1f, 0.1f, 0.1f].variable); 9 10 auto hy = func.forward(hx); 11 // assert(hy.data == [tanh(-1.0f), tanh(1.0f), tanh(0.0f)]); 12 auto hgy = [1.0f, 2.0f, 3.0f].variable; 13 auto hgx = func.backward(hgy); 14 15 // test CUDA 16 version (grain_cuda) { 17 auto dfunc = new Sigmoid!(float, 1); 18 auto dx = hx.to!DeviceStorage; 19 auto dy = dfunc.forward(dx); 20 assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced)); 21 auto dgx = dfunc.backward(hgy.to!DeviceStorage); 22 assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced)); 23 }
sigmoid function