Example #1
0
def test_exp_matrix_colwise():
    inpt = T.matrix()
    norm = exp(inpt, axis=0)
    f = theano.function([inpt], norm, mode='FAST_COMPILE')
    res = f(test_matrix)
    correct = roughly(res, [2.85361711, 24.90040964, 97.4061874, 1.])
    assert correct, 'exp norm colwise not working'
Example #2
0
def test_exp_matrix_rowwise():
    inpt = T.matrix()
    norm = exp(inpt, axis=1)
    f = theano.function([inpt], norm, mode='FAST_COMPILE')
    res = f(test_matrix)
    correct = roughly(res, [114.68499678, 11.47521737])
    assert correct, 'exp norm rowwise not working'
Example #3
0
def test_l1_matrix_colwise():
    inpt = T.matrix()
    norm = l1(inpt, axis=0)
    f = theano.function([inpt], norm, mode='FAST_COMPILE')
    res = f(test_matrix)
    correct = roughly(res, [3., 4.2, 6.5, 100.2])
    assert correct, 'l1 norm colwise not working'
Example #4
0
def test_squared():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X
    Y.tag.test_value = test_Y
    dist = squared(X, Y).sum()
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    assert roughly(res, 0.0097), 'squared loss not working'
Example #5
0
def test_model_function():
    pars = ParameterSet(weights=(2, 3))
    inpt = T.matrix()
    output = T.dot(inpt, pars.weights)

    model = Model()
    model.exprs = {'inpt': inpt, 'output': output}
    model.parameters = pars

    f = model.function(['inpt'], 'output')
    fx = model.function(['inpt'], 'output', explicit_pars=True)

    np.random.seed(1010)
    test_inpt = np.random.random((10, 2))
    correct = roughly(f(test_inpt), fx(pars.data, test_inpt))

    assert correct, 'implicit pars and explicit pars have different output'

    f1 = model.function(['inpt'], ['output'])
    f2 = model.function([inpt], ['output'])
    f3 = model.function([inpt], [output])
    f4 = model.function(['inpt'], [output])

    assert roughly(f1(test_inpt), f2(test_inpt)), "f1 and f2 don't agree"
    assert roughly(f1(test_inpt), f3(test_inpt)), "f1 and f3 don't agree"
    assert roughly(f1(test_inpt), f4(test_inpt)), "f1 and f4 don't agree"
    assert roughly(f2(test_inpt), f3(test_inpt)), "f2 and f3 don't agree"
    assert roughly(f2(test_inpt), f4(test_inpt)), "f2 and f4 don't agree"
    assert roughly(f3(test_inpt), f4(test_inpt)), "f3 and f4 don't agree"
Example #6
0
def test_neg_cross_entropy_rowwise():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X > 0.2
    Y.tag.test_value = test_Y
    dist = nce(X, Y).sum(axis=1)
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    correct = roughly(res, [0.85798192, 0.74838975])
    assert correct, 'nce loss rowwise not working'
Example #7
0
def test_neg_cross_entropy_colwise():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X > 0.2
    Y.tag.test_value = test_Y
    dist = nce(X, Y).sum(axis=0)
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    correct = roughly(res, [[0.49795728, 0.48932523, 0.61908916]])
    assert correct, 'nce loss colwise not working'
Example #8
0
def test_l2_matrix_colwise():
    inpt = T.matrix()
    norm = l2(inpt, axis=0)
    f = theano.function([inpt], norm, mode='FAST_COMPILE')
    res = f(test_matrix)
    correct = roughly(
            res,
            [5., 1.12400000e+01, 2.42500000e+01, 1.00400400e+04])
    assert correct, 'l2 norm colwise not working'
Example #9
0
def test_distance_matrix():
    X = T.matrix()
    D = distance_matrix(X)
    f = theano.function([X], D, mode='FAST_COMPILE')
    x = np.array([[1], [2], [3]])
    res = f(x)
    print res
    correct = roughly(res, np.array([[0, 1, 4], [1, 0, 1], [4, 1, 0]]))
    assert correct, 'distance matrix not working right'
Example #10
0
def test_neg_cross_entropy():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X > 0.2
    Y.tag.test_value = test_Y
    dist = nce(X, Y).sum()
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    correct = roughly(res, 1.6063716678910529)
    assert correct, 'nce loss not working'
Example #11
0
def test_squared_colwise():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X
    Y.tag.test_value = test_Y
    dist = squared(X, Y).sum(axis=0)
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    correct = roughly(res, [0.0018, 0.005, 0.0029])
    assert correct, 'squared loss colwise not working'
Example #12
0
def test_absolute_colwise():
    X, Y = T.matrix(), T.matrix()
    X.tag.test_value = test_X
    Y.tag.test_value = test_Y
    dist = absolute(X, Y).sum(axis=0)
    f = theano.function([X, Y], dist, mode='FAST_COMPILE')
    res = f(test_X, test_Y)
    correct = roughly(res, [0.06, 0.08, 0.07])
    assert correct, 'absolute loss colwise not working'
def test_tanh():
    inpt = T.matrix()
    expr = tanh(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
        [-0.96402758, 0.9966824, 0.99975321, -1.],
        [0.76159416, -0.76159416, 0.96402758, 0.]])
    correct = roughly(result, desired)
    assert correct, 'tanh not working'
def test_tanhplus():
    inpt = T.matrix()
    expr = tanhplus(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
        [-2.96402758, 4.1966824 , 5.49975321, -101.2],
        [ 1.76159416,-1.76159416, 2.96402758, 0.]])
    correct = roughly(result, desired)
    assert correct, 'tanh plus not working'
def test_sigmoid():
    inpt = T.matrix()
    expr = sigmoid(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
        [1.19202922e-01, 9.60834277e-01, 9.89013057e-01, 3.04574061e-44],
        [7.31058579e-01, 2.68941421e-01, 8.80797078e-01, 5.00000000e-01]])

    correct = roughly(result, desired)
    assert correct, 'sigmoid not working'
def test_rectifier():
    inpt = T.matrix()
    expr = rectifier(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
       [0., 3.2, 4.5, 0.],
       [1., 0., 2., 0.]])

    correct = roughly(result, desired)
    assert correct, 'relu not working'
def test_soft():
    inpt = T.matrix()
    expr = softplus(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
       [0.12692801, 3.23995333, 4.51104774,  0.],
       [1.31326169, 0.31326169, 2.12692801,  0.69314718]])

    correct = roughly(result, desired)
    assert correct, 'soft relu not working'
def test_log_product_of_t():
    inpt = T.matrix()
    expr = logproduct_of_t(inpt)
    f = theano.function([inpt], expr)
    result = f(test_matrix)
    desired = np.array([
       [1.60943791, 2.41947884, 3.0563569 , 9.21443597],
       [0.69314718, 0.69314718, 1.60943791, 0.]])

    correct = roughly(result, desired)
    assert correct, 'pot not working'
Example #19
0
def test_nominal_neg_cross_entropy():
    Xc, Y = T.ivector(), T.matrix()
    Xc.tag.test_value = (test_X > 0.2).argmax(axis=1).astype('uint8')
    Y.tag.test_value = test_Y
    dist = nnce(Xc, Y).sum()
    f = theano.function([Xc, Y], dist, mode='FAST_COMPILE')
    res = f(test_Xc, test_Y)
    desired = -np.log(test_Y)[np.arange(test_Xc.shape[0]), test_Xc].sum()
    correct = roughly(res, desired)
    print res
    print desired
    assert correct, 'nnce loss not working'
Example #20
0
def test_theano_function_with_nested_exprs():

    def expr_generator(a, b):
        ra = [T.pow(a[i], i) for i in range(len(a))]
        return ra, T.exp(b)

    a = [T.scalar('a%d' % i) for i in range(5)]
    b = T.scalar('b')

    f = breze.arch.util.theano_function_with_nested_exprs(
            [a, b], expr_generator(a, b))

    va = [2 for _ in a]
    vb = 3
    resa, resb = f(va, vb)

    print "va:   ", va
    print "vb:   ", vb
    print "resa: ", resa
    print "resb: ", resb

    for i in range(len(va)):
        assert roughly(resa[i], math.pow(va[i], i))
    assert roughly(resb, math.exp(vb))
Example #21
0
def test_lds_values():
    # The following test values were generated with David Barber's LDS
    # implementation coming along with his book 'Bayesian Reasoning and
    # machine learning'.

    l = LinearDynamicalSystem(2, 3)

    l.parameters['transition'] = np.arange(1, 10).reshape((3, 3)).T
    l.parameters['emission'] = np.arange(1, 7).reshape((3, 2))

    l.parameters['visible_noise_mean'] = np.array((-1, 1))
    cov_block = np.arange(1, 5).reshape((2, 2))
    l.parameters['visible_noise_cov'] = (
        np.dot(cov_block, cov_block.T) * 0.2 + np.eye(2) * 100)

    l.parameters['hidden_noise_mean'] = np.array((1, 2, 3))
    cov_block = np.arange(1, 10).reshape((3, 3))
    l.parameters['hidden_noise_cov'] = (
        np.dot(cov_block, cov_block.T) + np.eye(3) * 10)

    l.parameters['hidden_mean_initial'] = -np.array((.1, .2, .3))
    l.parameters['hidden_cov_initial'] = (
        np.dot(cov_block, cov_block.T) * 0.2 + np.eye(3) * 10)

    f_forward = l.function(
        ['inpt'],
        ['filtered_means', 'filtered_covs', 'log_likelihood'],
        mode='FAST_COMPILE')
    f_backward = l.function(
        ['filtered_means', 'filtered_covs'],
        ['smoothed_means', 'smoothed_covs'],
        mode='FAST_COMPILE')

    X = np.array([
        [1, 1.5],
        [2, 2.5],
        [3, 3.5],
        [4, 4.5]])
    X.shape = 4, 1, 2

    f, F, ll = f_forward(X)

    f_desired = np.array([
        [[-0.0395, 0.0649, 0.1694]],
        [[0.5087, 0.2911, 0.0734]],
        [[0.5593, 0.3754, 0.1914]],
        [[0.8622, 0.5272, 0.1921]]])

    F_desired = np.zeros((4, 1, 3, 3))

    F_desired[0, 0] = [
        [9.4992, -1.0806, -1.6604],
        [-1.0806, 7.5570, -3.8054],
        [-1.6604, -3.8054, 4.0497],
    ]

    F_desired[1, 0] = [
        [16.0466, 0.6834, -4.6797],
        [0.6834, 8.0361, -4.6112],
        [-4.6797, -4.6112, 5.4574],
    ]

    F_desired[2, 0] = [
        [18.8713, 1.4414, -5.9885],
        [1.4414, 8.2395, -4.9624],
        [-5.9885, -4.9624, 6.0637],
    ]

    F_desired[3, 0] = [
        [19.7744, 1.6837, -6.4070],
        [1.6837, 8.3045, -5.0747],
        [-6.4070, -5.0747, 6.2577],
    ]

    assert roughly(f, f_desired, 1E-4), 'filtered means not correct'
    assert roughly(F, F_desired, 1E-4), 'filtered covs not correct'
    assert roughly(ll, [-38.3636], 1E-4), 'log likelihood calculated wrong: %f' % ll

    s, S = f_backward(f, F)

    s_desired = np.array([
        [-0.2173, 0.1706, -0.5076, 0.8622],
        [-0.0597, 0.0528, -0.0275, 0.5272],
        [0.0978, -0.0651, 0.4526, 0.1921]]).T

    S_desired = np.zeros((4, 1, 3, 3))
    S_desired[0, 0] = np.array([[5.5498, -2.5166, -0.583],
                                [-2.5166, 6.9683, -3.5468],
                                [-0.583, -3.5468, 3.4894]])

    S_desired[1, 0] = np.array([[7.3348, -2.1811, -1.697],
                                [-2.1811, 7.0324, -3.7542],
                                [-1.697, -3.7542, 4.1886]])

    S_desired[2, 0] = np.array([[9.8391, -1.7404, -3.3199],
                                [-1.7404, 7.11, -4.0396],
                                [-3.3199, -4.0396, 5.2407]])

    S_desired[3, 0] = np.array([[19.7744, 1.6837, -6.407],
                                [1.6837, 8.3045, -5.0747],
                                [-6.407, -5.0747, 6.2577]])

    assert roughly(s[:, 0, :], s_desired, 1E-4), 'smooooothed means not correct'
    assert roughly(S, S_desired, 1E-4), 'smooooothed covs not correct'