def test_1st_steps_functional(device_id):
    from cntk.ops.tests.ops_test_utils import cntk_device
    try_set_default_device(cntk_device(device_id))
    reset_random_seed(0)
    from LogisticRegression_FunctionalAPI import final_loss, final_metric, final_samples, test_metric
    # these are the final values from the log output
    assert np.allclose(final_loss,   0.344399, atol=1e-5)
    assert np.allclose(final_metric, 0.1258,   atol=1e-4)
    assert np.allclose(test_metric,  0.0811,   atol=1e-4)
    assert final_samples == 20000
def test_1st_steps_graph(device_id):
    from cntk.ops.tests.ops_test_utils import cntk_device
    try_set_default_device(cntk_device(device_id))
    reset_random_seed(0)
    from LogisticRegression_GraphAPI import trainer, evaluator, X_test, Y_test, data, label_one_hot
    #print(trainer.previous_minibatch_loss_average)
    assert np.allclose(trainer.previous_minibatch_loss_average, 0.1233455091714859, atol=1e-5)
    assert trainer.previous_minibatch_sample_count == 32
    # evaluator does not have a way to correctly get the aggregate, so we run one more MB on it
    i = 0
    x = X_test[i:i+32] # get one minibatch worth of data
    y = Y_test[i:i+32]
    metric = evaluator.test_minibatch({data: x, label_one_hot: y})
    #print(metric)
    assert np.allclose(metric, 0.0625, atol=1e-5)
def test_1st_steps_mnist(device_id):
    from cntk.ops.tests.ops_test_utils import cntk_device
    cntk_py.force_deterministic_algorithms()
    cntk_py.set_fixed_random_seed(1)
    try_set_default_device(cntk_device(device_id))
    reset_random_seed(0)
    from MNIST_Complex_Training import final_loss, final_metric, final_samples, test_metric
    print(final_loss, final_metric, final_samples, test_metric)
    # these are the final values from the log output
    # Since this has convolution, there is some variance.
    # Finished Epoch[36]: loss = 0.009060 * 54000, metric = 0.27% * 54000 1.971s (27397.3 samples/s);
    # Finished Evaluation [12]: Minibatch[1-24]: metric = 0.65% * 6000;
    # Learning rate 7.8125e-06 too small. Training complete.
    # Finished Evaluation [13]: Minibatch[1-313]: metric = 0.63% * 10000;
    assert np.allclose(final_loss,   0.009060, atol=1e-5)
    assert np.allclose(final_metric, 0.0027,   atol=1e-3)
    assert np.allclose(test_metric,  0.0063,   atol=1e-3)
    assert final_samples == 54000
Beispiel #4
0
def test_initializer_init(device_id):
    from cntk.ops.tests.ops_test_utils import cntk_device
    from cntk import cntk_py
    cntk_py.always_allow_setting_default_device()
    from cntk.device import try_set_default_device
    try_set_default_device(cntk_device(device_id))

    previous_random_seed = cntk_py.get_random_seed()
    cntk_py.reset_random_seed(0)

    _check(uniform(scale=1), 'uniform')
    _check(normal(scale=1, output_rank=1, filter_rank=2), 'normal')
    _check(xavier(scale=10, output_rank=1, filter_rank=2), 'xavier')
    _check(glorot_uniform(scale=10, output_rank=1, filter_rank=2), 'glorot_uniform')
    _check(glorot_normal(scale=10, output_rank=1, filter_rank=2), 'glorot_normal')
    _check(he_uniform(scale=10, output_rank=1, filter_rank=2), 'he_uniform')
    _check(he_normal(scale=10, output_rank=1, filter_rank=2), 'he_normal')

    cntk_py.reset_random_seed(previous_random_seed)
Beispiel #5
0
def test_layers_convolution_value():
    from cntk.cntk_py import reset_random_seed
    reset_random_seed(0)

    # Common parameters
    inC, inH, inW = 3, 10, 10
    in_filter_shape = (3, 3)
    out_num_filters = 1
    dat = np.ones([1, inC, inH, inW], dtype=np.float32)

    ##########################################################
    # Test convolutional layer for correctness (p=False s = 1)
    ##########################################################
    y = input((inC, inH, inW))
    zeropad = False
    in_strides = 1

    model = Convolution(in_filter_shape,
                        num_filters=out_num_filters,
                        activation=None,
                        pad=zeropad,
                        strides=in_strides,
                        name='foo')
    res = model(y).eval({y: dat})

    # Extract the W weight matrix
    expected_res = np.sum(model.foo.W.value)

    np.testing.assert_array_almost_equal(res[0][0][0][0], expected_res, decimal=7, \
        err_msg="Error in convolution computation with stride = 1 and zeropad = False")

    ##########################################################
    # Test convolutional layer for correctness (p=False s = 2)
    ##########################################################
    zeropad = False
    in_strides = 2

    model = Convolution(in_filter_shape,
                        num_filters=out_num_filters,
                        activation=None,
                        pad=zeropad,
                        strides=in_strides,
                        name='foo')
    res = model(y).eval({y: dat})

    # Extract the W weight matrix
    expected_res = np.sum(model.foo.W.value)

    np.testing.assert_array_almost_equal(res[0][0][0][0], expected_res, decimal=5, \
        err_msg="Error in convolution computation with stride = 2 and zeropad = False")

    ##########################################################
    # Test convolutional layer for correctness (p=True s = 1)
    ##########################################################
    zeropad = True
    in_strides = 1

    model = Convolution(in_filter_shape,
                        num_filters=out_num_filters,
                        activation=None,
                        pad=zeropad,
                        strides=in_strides,
                        name='foo')
    res = model(y).eval({y: dat})

    # Extract the W weight matrix
    expected_res = np.sum(model.foo.W.value)

    # Compare the center of the res with the sum of the weights
    np.testing.assert_array_almost_equal(res[0][0][1][1], expected_res, decimal=7, \
        err_msg="Error in convolution computation with stride = 1 and zeropad = True")

    ##########################################################
    # Test convolutional layer for second invocation/parameter sharing
    ##########################################################
    y1 = input((inC, inH, inW))
    res = model(y1).eval({y1: dat})  # this re-clones 'model'

    # Extract the W weight matrix
    expected_res = np.sum(model.foo.W.value)

    # Compare the center of the res with the sum of the weights
    np.testing.assert_array_almost_equal(res[0][0][1][1], expected_res, decimal=7, \
        err_msg="Error in convolution computation with stride = 1 and zeropad = True, second invocation")

    ##########################################################
    # Test convolutional layer for correctness (p=True s = 2)
    ##########################################################
    zeropad = True
    in_strides = 2

    model = Convolution(in_filter_shape,
                        num_filters=out_num_filters,
                        activation=None,
                        pad=zeropad,
                        strides=in_strides,
                        name='foo')
    res = model(y).eval({y: dat})

    # Extract the W weight matrix
    expected_res = np.sum(model.foo.W.value[0, :, 1:, 1:])

    # Compare at the top-left corner, to see the effect of zero-padding.
    np.testing.assert_array_almost_equal(
        res[0][0][0][0],
        expected_res,
        decimal=5,
        err_msg=
        "Error in convolution computation with stride = 2 and zeropad = True")