예제 #1
0
def test_op_dropout(shape, dropout_rate, device_id, precision):
    from cntk import dropout

    count = 10
    resulted_non_zeros = 0

    # As the dropout node is stochastic, we run it a couple times and aggregate
    # over the results to get more stable tests.
    for i in range(count):
        value = np.ones(shape=shape, dtype=PRECISION_TO_TYPE[precision])

        a = I(shape=value.shape,
              dtype=sanitize_dtype_cntk(PRECISION_TO_TYPE[precision]),
              needs_gradient=True,
              name='a')

        dropout_node = dropout(a, dropout_rate=dropout_rate)

        value.shape = (1, 1) + value.shape
        forward_input = {a: value}

        forward, backward = cntk_eval(dropout_node,
                                      forward_input,
                                      precision,
                                      cntk_device(device_id),
                                      backward_pass=True)

        resulted_non_zeros += np.count_nonzero(forward[dropout_node.output])

    resulted_non_zeros /= count
    num_elements = np.multiply.reduce(shape)
    expected_non_zeros = num_elements * (1 - dropout_rate)
    max_off = 0.2 * num_elements

    assert (abs(resulted_non_zeros - expected_non_zeros) < max_off)
예제 #2
0
def test_op_dropout(shape, dropout_rate, device_id, precision):
    from cntk import dropout

    count = 10
    resulted_non_zeros = 0

    # As the dropout node is stochastic, we run it a couple times and aggregate
    # over the results to get more stable tests.
    for i in range(count):
        value = np.ones(shape=shape, dtype=PRECISION_TO_TYPE[precision])

        a = I(shape=value.shape,
              dtype=sanitize_dtype_cntk(PRECISION_TO_TYPE[precision]),
              needs_gradient=True,
              name='a')

        dropout_node = dropout(a, dropout_rate=dropout_rate)

        value.shape = (1, 1) + value.shape
        forward_input = {a: value}

        forward, backward = cntk_eval(dropout_node,
                                 forward_input,
                                 precision,
                                 cntk_device(device_id),
                                 backward_pass=True)

        resulted_non_zeros += np.count_nonzero(forward[dropout_node.output])

    resulted_non_zeros /= count
    num_elements = np.multiply.reduce(shape)
    expected_non_zeros = num_elements * (1 - dropout_rate)
    max_off = 0.2 * num_elements

    assert(abs(resulted_non_zeros - expected_non_zeros) <
           max_off)