Esempio n. 1
0
def test_set_floatx():
    """
    Make sure that changes to the global floatx are effectively
    taken into account by the backend.
    """
    # Keep track of the old value
    old_floatx = floatx()

    set_floatx('float16')
    var = BTH.variable([10])
    check_dtype(var, 'float16')

    set_floatx('float64')
    var = BTH.variable([10])
    check_dtype(var, 'float64')

    # Restore old value
    set_floatx(old_floatx)
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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))
Esempio n. 7
0
def test_value_manipulation():
    val = np.random.random((4, 2))
    xth = BTH.variable(val)
    xnp = BNP.variable(val)

    # get_value
    valth = BTH.get_value(xth)
    valnp = BNP.get_value(xnp)
    assert valnp.shape == valth.shape
    assert_allclose(valth, valnp, atol=1e-05)

    # set_value
    BTH.set_value(xth, val)

    valth = BTH.get_value(xth)
    assert valnp.shape == val.shape
    assert_allclose(valth, val, atol=1e-05)
Esempio n. 8
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)
Esempio n. 9
0
def test_svd():
    input_shape = (4, 4)

    val = np.random.random(input_shape) - 0.5
    xth = BTH.variable(val)
    xnp = BNP.variable(val)

    Uth, Sth, Vth = BTH.svd(xth)
    Unp, Snp, Vnp = BNP.svd(xnp)

    Uth = Uth.eval()
    Sth = Sth.eval()
    Vth = Vth.eval()

    assert Uth.shape == Unp.shape
    assert Sth.shape == Snp.shape
    assert Vth.shape == Vnp.shape
    assert_allclose(Uth, Unp, atol=1e-05)
    assert_allclose(Sth, Snp, atol=1e-05)
    assert_allclose(Vth, Vnp, atol=1e-05)
Esempio 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)