Example #1
0
def test_model_function():
    pars = ParameterSet(weights=(2, 3))
    inpt = T.matrix()
    output = T.dot(inpt, pars.weights)
    pars.data[...] = np.random.standard_normal(pars.data.shape)

    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)).astype(theano.config.floatX)
    r1 = f(test_inpt)
    r2 = fx(pars.data, test_inpt)
    print r1
    print r2
    correct = np.allclose(r1, r2)

    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 np.allclose(f1(test_inpt), f2(test_inpt)), "f1 and f2 don't agree"
    assert np.allclose(f1(test_inpt), f3(test_inpt)), "f1 and f3 don't agree"
    assert np.allclose(f1(test_inpt), f4(test_inpt)), "f1 and f4 don't agree"
    assert np.allclose(f2(test_inpt), f3(test_inpt)), "f2 and f3 don't agree"
    assert np.allclose(f2(test_inpt), f4(test_inpt)), "f2 and f4 don't agree"
    assert np.allclose(f3(test_inpt), f4(test_inpt)), "f3 and f4 don't agree"
Example #2
0
def test_model_function_mode():
    pars = ParameterSet()
    weights = pars.declare((2, 3))
    pars.alloc()
    inpt = T.matrix()
    output = T.dot(inpt, weights)
    pars.data[...] = np.random.standard_normal(pars.data.shape)

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

    mode = theano.Mode()

    f = model.function(['inpt'], 'output', mode=mode)
    actual_mode = f.theano_func.maker.mode
    assert actual_mode is mode, 'wrong mode: %s' % actual_mode

    model.mode = theano.Mode()
    f = model.function(['inpt'], 'output')
    actual_mode = f.theano_func.maker.mode

    # Maybe a weird way to compare modes, but it seems to get the job done.
    equal = actual_mode.__dict__ == mode.__dict__
    assert equal, 'wrong mode: (%s != %s)' % (actual_mode, mode)
Example #3
0
def test_pickling_models():
    ma = T.matrix()
    m = Model()
    m.parameters = ParameterSet(bla=2)
    m.exprs = {'m_sqrd': m.parameters.bla.sum() * ma.sum()}
    m.f = m.function([ma], 'm_sqrd', explicit_pars=False)

    cPickle.dumps(m)
Example #4
0
def test_nested_exprs():
    ma = T.matrix()
    m = Model()
    m.parameters = ParameterSet()
    bla = m.parameters.declare(2)
    m.parameters.alloc()
    m.parameters[bla] = 1, 2
    m.exprs = {
        'norms': {
            'l1': abs(ma).sum(),
            'l2': T.sqrt((ma**2).sum()),
        },
        'ma_multiplied': [ma, 2 * ma],
        'bla': bla,
        'blubb': 1,
    }

    f = m.function([], 'bla', explicit_pars=False, on_unused_input='ignore')
    assert np.allclose(f(), [1, 2])

    f = m.function([ma], ('norms', 'l1'),
                   explicit_pars=False,
                   on_unused_input='ignore')

    assert f([[-1, 1]]) == 2

    f = m.function([ma], ('norms', 'l2'),
                   explicit_pars=False,
                   on_unused_input='ignore')

    assert np.allclose(f([[-1, 1]]), np.sqrt(2.))

    f = m.function([ma], ('ma_multiplied', 0),
                   explicit_pars=False,
                   on_unused_input='ignore')
    assert np.allclose(f([[-1, 1]]), [-1, 1])

    f = m.function([ma], ('ma_multiplied', 1),
                   explicit_pars=False,
                   on_unused_input='ignore')
    assert np.allclose(f([[-1, 1]]), [-2, 2])