Ejemplo n.º 1
0
def test_mean_subtraction_forward_backward(seed, inshape, base_axis, ctx,
                                           func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    mean_shape = inshape[base_axis:] if base_axis >= 0 else inshape[base_axis +
                                                                    len(inshape
                                                                        ):]
    inputs = [
        np.array(rng.randn(*inshape).astype(np.float32)),
        np.zeros(mean_shape),
        np.array([1000])
    ]
    batch_stat = True

    function_tester(rng,
                    F.mean_subtraction,
                    ref_mean_subtraction,
                    inputs,
                    func_args=[base_axis, batch_stat],
                    ctx=ctx,
                    func_name=func_name,
                    dstep=1e-2,
                    backward=[True, False, False],
                    atol_b=1e-2)

    # Check if running mean works.
    vinputs = []
    for input in inputs:
        vinputs.append(nn.Variable(input.shape, True))
        vinputs[-1].d = input
    for i in range(5):
        inputs[0] = rng.randn(*inputs[0].shape)
        vinputs[0].d[...] = inputs[0]
        ref_y, rmean = ref_mean_subtraction(*(inputs +
                                              [base_axis, batch_stat]))
        with nn.auto_forward():
            y = F.mean_subtraction(*(vinputs + [base_axis, batch_stat]))
        # print('vinput[1].d', vinputs[1].d, vinputs[2].d)
        # print('inputs[1]', inputs[1], inputs[2])
        assert_allclose(vinputs[1].d, inputs[1])

    # Check if global stat mode works
    batch_stat = False
    ref_y = ref_mean_subtraction(*(inputs + [base_axis, batch_stat]))
    with nn.auto_forward():
        y = F.mean_subtraction(*(vinputs + [base_axis, batch_stat]))
    assert_allclose(ref_y, y.d)
Ejemplo n.º 2
0
def test_mean_subtraction_forward_backward(seed, inshape, base_axis, ctx, func_name):
    from nbla_test_utils import function_tester
    rng = np.random.RandomState(seed)
    mean_shape = inshape[base_axis:]
    inputs = [np.array(rng.randn(*inshape).astype(np.float32)),
              np.zeros(mean_shape),
              np.array([1000])]
    batch_stat = True

    function_tester(rng, F.mean_subtraction, ref_mean_subtraction,
                    inputs,
                    func_args=[base_axis, batch_stat],
                    ctx=ctx, func_name=func_name, dstep=1e-2,
                    backward=[True, False, False],
                    atol_b=1e-2)

    # Check if running mean works.
    vinputs = []
    for input in inputs:
        vinputs.append(nn.Variable(input.shape, True))
        vinputs[-1].d = input
    for i in range(5):
        inputs[0] = rng.randn(*inputs[0].shape)
        vinputs[0].d[...] = inputs[0]
        ref_y, rmean = ref_mean_subtraction(
            *(inputs + [base_axis, batch_stat]))
        with nn.auto_forward():
            y = F.mean_subtraction(*(vinputs + [base_axis, batch_stat]))
        # print 'vinput[1].d', vinputs[1].d, vinputs[2].d
        # print 'inputs[1]', inputs[1], inputs[2]
        assert np.allclose(vinputs[1].d, inputs[1])

    # Check if global stat mode works
    batch_stat = False
    ref_y = ref_mean_subtraction(*(inputs + [base_axis, batch_stat]))
    with nn.auto_forward():
        y = F.mean_subtraction(*(vinputs + [base_axis, batch_stat]))
    assert np.allclose(ref_y, y.d)