예제 #1
0
파일: test_util.py 프로젝트: Wiebke/breze
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)
예제 #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)
예제 #3
0
파일: test_util.py 프로젝트: Wiebke/breze
def test_parameter_set_init_overwrite():
    pars = ParameterSet()

    matrix = pars.declare((10, 10))
    pars.alloc()

    pars[matrix] = np.eye(10)
    assert np.allclose(pars.data.reshape(pars[matrix].shape), pars[matrix])
예제 #4
0
def test_parameter_set_init_overwrite():
    pars = ParameterSet()

    matrix = pars.declare((10, 10))
    pars.alloc()

    pars[matrix] = np.eye(10)
    assert np.allclose(pars.data.reshape(pars[matrix].shape), pars[matrix])
예제 #5
0
파일: test_util.py 프로젝트: Wiebke/breze
def test_parameter_set_init_declare():
    pars = ParameterSet()

    matrix = pars.declare((10, 10))
    vector = pars.declare((10,))
    pars.alloc()

    assert pars.data.shape == (110,), 'wrong size for flat pars allocated'
    assert (pars[matrix].shape == (10, 10)), ('wrong size for 2d array in pars '
                                                'allocated')
    assert (pars[vector].shape == (10,)), ('wrong size for 1d array in pars '
                                             'allocated')
예제 #6
0
def test_parameter_set_init_declare():
    pars = ParameterSet()

    matrix = pars.declare((10, 10))
    vector = pars.declare((10, ))
    pars.alloc()

    assert pars.data.shape == (110, ), 'wrong size for flat pars allocated'
    assert (pars[matrix].shape == (10,
                                   10)), ('wrong size for 2d array in pars '
                                          'allocated')
    assert (pars[vector].shape == (10, )), ('wrong size for 1d array in pars '
                                            'allocated')
예제 #7
0
파일: test_util.py 프로젝트: Wiebke/breze
def test_parameter_set_data_change():
    pars = ParameterSet()
    matrix = pars.declare((10, 10))
    vector = pars.declare((10,))
    pars.alloc()
    pars[matrix] = 0
    pars[vector] = 0
    assert (pars.data == 0).all(), repr(pars.data)

    pars[matrix] += 1
    assert pars.data.sum() == 100

    pars[vector] += 2
    assert pars.data.sum() == 120

    pars.data *= 0.5
    assert pars.data.sum() == 60
예제 #8
0
def test_transfer_stateful():
    inpt = T.tensor3('inpt')

    def t(s, x):
        return T.zeros_like(s) + 1, x
    t.stateful = True

    P = ParameterSet()
    r = Recurrent(inpt, 4, t, P.declare)
    P.alloc()

    assert hasattr(r, 'state')

    f = theano.function([P.flat, inpt], r.state)

    s = f(P.data, np.zeros((3, 1, 4)))
    assert (s == 1).all(), 'hidden state has wrong value'
예제 #9
0
def test_transfer_insize_outsize():
    inpt = T.tensor3('inpt')

    def t(x):
        return x
    t.in_size = 2
    t.out_size = 3

    P = ParameterSet()
    r = Recurrent(inpt, 4, t, P.declare)
    P.alloc()

    s = P[r.weights].shape
    assert  s == (12, 8), 'Shape is %s' % str(s)

    s = P[r.initial].shape
    assert s == (12,),  'Shape is %s' % str(s)
예제 #10
0
def test_parameter_set_data_change():
    pars = ParameterSet()
    matrix = pars.declare((10, 10))
    vector = pars.declare((10, ))
    pars.alloc()
    pars[matrix] = 0
    pars[vector] = 0
    assert (pars.data == 0).all(), repr(pars.data)

    pars[matrix] += 1
    assert pars.data.sum() == 100

    pars[vector] += 2
    assert pars.data.sum() == 120

    pars.data *= 0.5
    assert pars.data.sum() == 60
예제 #11
0
def test_transfer_stateful():
    inpt = T.tensor3('inpt')

    def t(s, x):
        return T.zeros_like(s) + 1, x

    t.stateful = True

    P = ParameterSet()
    r = Recurrent(inpt, 4, t, P.declare)
    P.alloc()

    assert hasattr(r, 'state')

    f = theano.function([P.flat, inpt], r.state)

    s = f(P.data, np.zeros((3, 1, 4)))
    assert (s == 1).all(), 'hidden state has wrong value'
예제 #12
0
def test_transfer_insize_outsize():
    inpt = T.tensor3('inpt')

    def t(x):
        return x

    t.in_size = 2
    t.out_size = 3

    P = ParameterSet()
    r = Recurrent(inpt, 4, t, P.declare)
    P.alloc()

    s = P[r.weights].shape
    assert s == (12, 8), 'Shape is %s' % str(s)

    s = P[r.initial].shape
    assert s == (12, ), 'Shape is %s' % str(s)
예제 #13
0
파일: test_util.py 프로젝트: Wiebke/breze
def test_model_function():
    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

    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"
예제 #14
0
def test_model_function():
    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

    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"