1 import grain.testing; 2 import numir; 3 import mir.ndslice; 4 import std.meta; 5 import std.stdio; 6 import std.math; 7 8 auto hx = [1f, 2f, 3f, 4f].sliced(2, 2).variable(true); 9 auto hy = dropout(hx); 10 hy.sum.backward(); 11 auto ghx = hx.gradSliced; 12 foreach (i; 0 .. hx.shape[0]) { 13 foreach (j; 0 .. hx.shape[1]) { 14 if (approxEqual(hy.sliced[i, j], 2.0 * hx.sliced[i, j])) { 15 assert(ghx[i, j] == 2.0f); 16 } 17 else { 18 assert(hy.sliced[i, j] == 0.0f); 19 assert(ghx[i, j] == 0.0f); 20 } 21 } 22 }
dropout : apply random mask