dropout

dropout : apply random mask

dropout
(
T
size_t dim
alias Storage
)
(
Variable!(T, dim, Storage) x
,
float ratio = 0.5
,
bool isTrain = true
)

Examples

import grain.testing;
import numir;
import mir.ndslice;
import std.meta;
import std.stdio;
import std.math;

auto hx = [1f, 2f, 3f, 4f].sliced(2, 2).variable(true);
auto hy = dropout(hx);
hy.sum.backward();
auto ghx = hx.gradSliced;
foreach (i; 0 .. hx.shape[0]) {
    foreach (j; 0 .. hx.shape[1]) {
        if (approxEqual(hy.sliced[i, j], 2.0 * hx.sliced[i, j])) {
            assert(ghx[i, j] == 2.0f);
        }
        else {
            assert(hy.sliced[i, j] == 0.0f);
            assert(ghx[i, j] == 0.0f);
        }
    }
}

Meta