Exemplo n.º 1
0
def test_arange():
    for test_value in (-20, 0, 1, 10):
        t_a = BNP.arange(test_value)
        a = BNP.eval(t_a)
        assert np.array_equal(a, np.arange(test_value))
        t_b = BTH.arange(test_value)
        b = BTH.eval(t_b)
        assert np.array_equal(b, np.arange(test_value))
        assert np.array_equal(a, b)
        assert BNP.dtype(t_a) == BTH.dtype(t_b)
    for start, stop, step in ((0, 5, 1), (-5, 5, 2), (0, 1, 2)):
        a = BNP.eval(BNP.arange(start, stop, step))
        assert np.array_equal(a, np.arange(start, stop, step))
        b = BTH.eval(BTH.arange(start, stop, step))
        assert np.array_equal(b, np.arange(start, stop, step))
        assert np.array_equal(a, b)
    for dtype in ('int32', 'int64', 'float32', 'float64'):
        for backend in (BNP, BTH):
            t = backend.arange(10, dtype=dtype)
            assert backend.dtype(t) == dtype
Exemplo n.º 2
0
def test_tile():
    shape = (3, 4)
    arr = np.arange(np.prod(shape)).reshape(shape)
    arr_th = BTH.variable(arr)
    arr_np = BNP.variable(arr)

    n = (2, 1)
    th_z = BTH.tile(arr_th, n)
    th_rep = BTH.eval(th_z)
    np_rep = BNP.eval(BNP.tile(arr_np, n))
    assert_allclose(np_rep, th_rep, atol=1e-05)
Exemplo n.º 3
0
def check_single_tensor_operation(function_name, input_shape, **kwargs):
    val = np.random.random(input_shape) - 0.5
    xth = BTH.variable(val)
    xnp = BNP.variable(val)

    _zth = getattr(BTH, function_name)(xth, **kwargs)
    zth = BTH.eval(_zth)
    znp = BNP.eval(getattr(BNP, function_name)(xnp, **kwargs))

    assert zth.shape == znp.shape
    assert_allclose(zth, znp, atol=1e-05)
Exemplo n.º 4
0
def test_random_normal():
    mean = 0.
    std = 1.
    rand = BNP.eval(BNP.random_normal((1000, 1000), mean=mean, stddev=std))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand) - mean) < 0.01
    assert np.abs(np.std(rand) - std) < 0.01

    rand = BTH.eval(BTH.random_normal((1000, 1000), mean=mean, stddev=std))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand) - mean) < 0.01
    assert np.abs(np.std(rand) - std) < 0.01
Exemplo n.º 5
0
def test_random_binomial():
    p = 0.5
    rand = BNP.eval(BNP.random_binomial((1000, 1000), p))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand) - p) < 0.01
    assert np.max(rand) == 1
    assert np.min(rand) == 0

    rand = BTH.eval(BTH.random_binomial((1000, 1000), p))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand) - p) < 0.01
    assert np.max(rand) == 1
    assert np.min(rand) == 0
Exemplo n.º 6
0
def test_switch():
    val = np.random.random()
    xth = BTH.variable(val)
    xth = BTH.ifelse(xth >= 0.5, xth * 0.1, xth * 0.2)

    xnp = BNP.variable(val)
    xnp = BNP.ifelse(xnp >= 0.5, xnp * 0.1, xnp * 0.2)

    zth = BTH.eval(xth)
    znp = BNP.eval(xnp)

    assert zth.shape == znp.shape
    assert_allclose(zth, znp, atol=1e-05)
Exemplo n.º 7
0
def test_random_uniform():
    min_val = -1.
    max_val = 1.
    rand = BNP.eval(BNP.random_uniform((1000, 1000), min_val, max_val))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand)) < 0.01
    assert np.max(rand) <= max_val
    assert np.min(rand) >= min_val

    rand = BTH.eval(BTH.random_uniform((1000, 1000), min_val, max_val))
    assert rand.shape == (1000, 1000)
    assert np.abs(np.mean(rand)) < 0.01
    assert np.max(rand) <= max_val
    assert np.min(rand) >= min_val
Exemplo n.º 8
0
def check_two_tensor_operation(function_name, x_input_shape, y_input_shape,
                               **kwargs):
    xval = np.random.random(x_input_shape) - 0.5

    xth = BTH.variable(xval)
    xnp = BNP.variable(xval)

    yval = np.random.random(y_input_shape) - 0.5

    yth = BTH.variable(yval)
    ynp = BNP.variable(yval)

    _zth = getattr(BTH, function_name)(xth, yth, **kwargs)
    zth = BTH.eval(_zth)
    znp = BNP.eval(getattr(BNP, function_name)(xnp, ynp, **kwargs))

    assert zth.shape == znp.shape
    assert_allclose(zth, znp, atol=1e-05)
Exemplo n.º 9
0
def test_repeat_elements():
    reps = 3
    for ndims in [1, 2, 3]:
        shape = np.arange(2, 2 + ndims)
        arr = np.arange(np.prod(shape)).reshape(shape)
        arr_th = BTH.variable(arr)
        arr_np = BNP.variable(arr)

        for rep_axis in range(ndims):
            np_rep = np.repeat(arr, reps, axis=rep_axis)
            th_z = BTH.repeat_elements(arr_th, reps, axis=rep_axis)
            th_rep = BTH.eval(th_z)
            bnp_rep = BNP.eval(BNP.repeat_elements(arr_np, reps,
                                                   axis=rep_axis))

            assert th_rep.shape == np_rep.shape
            assert bnp_rep.shape == np_rep.shape
            assert_allclose(np_rep, th_rep, atol=1e-05)
            assert_allclose(np_rep, bnp_rep, atol=1e-05)
Exemplo n.º 10
0
def check_composed_tensor_operations(first_function_name, first_function_args,
                                     second_function_name,
                                     second_function_args, input_shape):
    ''' Creates a random tensor t0 with shape input_shape and compute
                 t1 = first_function_name(t0, **first_function_args)
                 t2 = second_function_name(t1, **second_function_args)
        with both Theano and TensorFlow backends and ensures the answers match.
    '''
    val = np.random.random(input_shape) - 0.5
    xth = BTH.variable(val)
    xnp = BNP.variable(val)

    yth = getattr(BTH, first_function_name)(xth, **first_function_args)
    ynp = getattr(BNP, first_function_name)(xnp, **first_function_args)

    zth = BTH.eval(
        getattr(BTH, second_function_name)(yth, **second_function_args))
    znp = BNP.eval(
        getattr(BNP, second_function_name)(ynp, **second_function_args))

    assert zth.shape == znp.shape
    assert_allclose(zth, znp, atol=1e-05)
Exemplo n.º 11
0
def test_shape_operations():
    # concatenate
    xval = np.random.random((4, 3))
    xth = BTH.variable(xval)
    xnp = BNP.variable(xval)
    yval = np.random.random((4, 2))
    yth = BTH.variable(yval)
    ynp = BNP.variable(yval)
    zth = BTH.eval(BTH.concatenate([xth, yth], axis=-1))
    znp = BNP.eval(BNP.concatenate([xnp, ynp], axis=-1))
    assert zth.shape == znp.shape
    assert_allclose(zth, znp, atol=1e-05)

    check_single_tensor_operation('reshape', (4, 2), shape=(8, 1))
    check_single_tensor_operation('permute_dimensions', (4, 2, 3),
                                  pattern=(2, 0, 1))
    check_single_tensor_operation('repeat', (4, 1), n=3)
    check_single_tensor_operation('flatten', (4, 1))
    check_single_tensor_operation('squeeze', (4, 3, 1), axis=2)
    check_single_tensor_operation('squeeze', (4, 1, 1), axis=1)
    check_composed_tensor_operations('reshape', {'shape': (4, 3, 1, 1)},
                                     'squeeze', {'axis': 2}, (4, 3, 1, 1))