Exemplo n.º 1
0
def test_apply_nets_regression():
    '''Checks only the apply_nets without prob and classify
    '''
    pm = ParticleMesh(BoxSize=32.0, Nmesh=(8, 8, 8), dtype='f8')
    engine = FastPMEngine(pm)
    code = CodeSegment(engine)

    #set up a network with 'nft' features, 'nl' hidden layers of size 'ls'
    #and data set of size 'nd'
    nft, nd = 5, 100
    nl, lhs = 2, [20, 30]
    ls = [nft] + lhs + [1]
    if nl != len(lhs):
        print('Number of layers is not the same as list of layer sizes')
    wts, bias = [], []
    nx, ny = nft, ls[0]
    for i in range(nl + 1):
        nx, ny = ls[i], ls[i + 1]
        wt = numpy.random.uniform(size=nx * ny).reshape(nx, ny).copy()
        wts.append(wt)
        bt = numpy.random.uniform(size=ny).copy()
        bias.append(bt)
    #wts[-1] = wts[-1].reshape(-1)
    bias[-1] = bias[-1].reshape(-1)

    print(nft, nl, nd)
    for i in range(nl + 1):
        print(wts[i].shape, bias[i].shape)

    acts = ['relu', 'relu']
    arch = tuple(zip(wts, bias, acts))
    features = numpy.random.uniform(size=nd * nft).reshape(nd, nft)
    features -= features.mean(axis=0)
    features /= features.std(axis=0)

    print(features.shape)

    #code.apply_nets(predict='predict', features='features', coeff=wts, \
    #                intercept=bias, Nd=nd, prob=False, classify=False)
    code.apply_nets(predict='predict', features='features', arch=arch, Nd=nd)

    eps = 1e-6
    check_grad(code,
               'predict',
               'features',
               init={'features': features},
               eps=eps,
               rtol=1e-8,
               atol=1e-8)
Exemplo n.º 2
0
def test_net_combination():
    pm = ParticleMesh(BoxSize=8.0, Nmesh=(8, 8, 8), dtype='f8')
    engine = FastPMEngine(pm)
    code = CodeSegment(engine)

    #set up a network with 'nft' features, 'nl' hidden layers of size 'ls'
    #and data set of size 'nd'
    nft, nd = 5, 100
    nl, lhs = 2, [20, 30]
    ls = [nft] + lhs + [1]
    if nl != len(lhs):
        print('Number of layers is not the same as list of layer sizes')
    wts, bias = [], []
    nx, ny = nft, ls[0]
    for i in range(nl + 1):
        nx, ny = ls[i], ls[i + 1]
        wt = numpy.random.uniform(size=nx * ny).reshape(nx, ny).copy()
        wts.append(wt)
        bt = numpy.random.uniform(size=ny).copy()
        bias.append(bt)
    #wts[-1] = wts[-1].reshape(-1)
    #bias[-1] = bias[-1].reshape(-1)

    print(nft, nl, nd)
    for i in range(nl + 1):
        print(wts[i].shape, bias[i].shape)

    features = numpy.random.uniform(size=nd * nft).reshape(nd, nft)
    features -= features.mean(axis=0)
    features /= features.std(axis=0)

    code.apply_nets(predict='predict', features='features', coeff=wts, \
                    intercept=bias, Nd=nd, prob=True, classify=True)

    eps = 1e-4
    check_grad(code,
               'predict',
               'features',
               init={'features': features},
               eps=eps,
               rtol=1e-8,
               atol=1e-12)