Ejemplo n.º 1
0
def buildGFNN(dim, **options):
    opt = {
        'name': 'gfnn',
        'oscParams': None,
        'freqDist': None,
        'fs': 40.,
        'adaptive': False,
        'learnParams': None,
        'c0': None,
        'outConn': RealIdentityConnection,
        'outDim': 1,
    }
    for key in options:
        if key not in opt.keys():
            raise NetworkError('buildGFNN unknown option: %s' % key)
        opt[key] = options[key]

    n = GFNN(opt['name'], fs = opt['fs'])
    i = LinearLayer(1, name = 'i')

    if opt['adaptive']:
        gfnnClass = AFNNLayer
    else:
        gfnnClass = GFNNLayer
    h = gfnnClass(dim,
        oscParams = opt['oscParams'], freqDist = opt['freqDist'], name = 'h')

    if issubclass(opt['outConn'], RealMeanFieldConnection) or issubclass(opt['outConn'], AbsPhaseIdentityConnection):
        outDim = opt['outDim']
    else:
        outDim = dim
    o = LinearLayer(outDim, name = 'o')

    n.addInputModule(i)
    n.addOutputModule(o)
    n.addModule(h)
    n.addConnection(GFNNExtConnection(i, h, name = 'ic'))

    if opt['learnParams'] is not None:
        n.addRecurrentConnection(GFNNIntConnection(h, h,
            learnParams = opt['learnParams'], c0 = opt['c0'],
            name = 'rc'))


    if issubclass(opt['outConn'], RealMeanFieldConnection):
        oTo = 0
        connPerInter = int(round(float(dim)/outDim))
        for x in range(outDim):
            hFrom = x * connPerInter
            hTo = min(hFrom+connPerInter, dim)
            c = RealMeanFieldConnection(h, o, name='gxc'+`x`,
                inSliceFrom=hFrom, inSliceTo=hTo,
                outSliceFrom=oTo, outSliceTo=oTo+1)
            n.addConnection(c)
            oTo += 1
    else:
        n.addConnection(opt['outConn'](h, o, name = 'oc'))
    n.sortModules()
    return n
Ejemplo n.º 2
0
def buildSimpleGFNN(dim=5, mean=False, conn=False):
    N = GFNN('simpleGFNN')
    i = LinearLayer(1, name = 'i')
    h = GFNNLayer(dim, name = 'gfnn')
    if mean:
        outdim = 1
    else:
        outdim = dim
    o = LinearLayer(outdim, name = 'o')
    N.addOutputModule(o)
    N.addInputModule(i)
    N.addModule(h)
    N.addConnection(FullConnection(i, h, name = 'f1'))
    if conn:
        N.addRecurrentConnection(FullNotSelfConnection(h, h, name = 'r1'))
    if mean:
        N.addConnection(RealMeanFieldConnection(h, o, name = 'm1'))
    else:
        N.addConnection(RealIdentityConnection(h, o, name = 'i1'))
    N.sortModules()
    return N
Ejemplo n.º 3
0
def buildGFNNLSTM(gfnnDim, lstmDim, **options):
    opt = {
        'oscParams': None,
        'freqDist': None,
        'fs': 40.,
        'learnParams': None,
        'c0': None,
        'interConn': RealMeanFieldConnection,
        'gain': 100.0
    }
    for key in options:
        if key not in opt.keys():
            raise NetworkError('buildGFNN unknown option: %s' % key)
        opt[key] = options[key]

    # make GFNN-LSTM - important to use the GFNN class
    n = GFNN('gfnn-lstm', fs = opt['fs'])

    # input layer
    i = LinearLayer(1, name = 'i')
    n.addInputModule(i)

    # output
    o = LinearLayer(1, name = 'o')
    n.addOutputModule(o)

    # bias
    b = BiasUnit(name='b')
    n.addModule(b)

    # hidden
    gfnn = GFNNLayer(gfnnDim, oscParams = opt['oscParams'],
        freqDist = opt['freqDist'], name = 'gfnn')
    n.addModule(gfnn)

    if issubclass(opt['interConn'], MeanFieldConnection):
        interDim = 1
    else:
        interDim = gfnnDim
    inter = LinearLayer(interDim, name = 'inter')
    n.addModule(inter)

    lstm = LSTMLayer(lstmDim, peepholes = True, name = 'lstm')
    n.addModule(lstm)

    # input -> gfnn
    n.addConnection(GFNNExtConnection(i, gfnn, name = 'igc'))

    # gfnn -> gfnn
    if opt['learnParams'] is not None:
        n.addRecurrentConnection(GFNNIntConnection(gfnn, gfnn,
            learnParams = opt['learnParams'], c0 = opt['c0'],
            name = 'grc'))

    # gfnn -> inter
    if issubclass(opt['interConn'], MeanFieldConnection):
        n.addConnection(opt['interConn'](gfnn, inter, gain = opt['gain'], name = 'gxc'))
    else:
        n.addConnection(opt['interConn'](gfnn, inter, name = 'gxc'))

    # inter -> lstm
    n.addConnection(FullConnection(inter, lstm, name = 'xlc'))

    # lstm -> lstm
    n.addRecurrentConnection(FullConnection(lstm, lstm, name = 'lrc'))

    # bias -> lstm
    n.addConnection(FullConnection(b, lstm, name = 'blc'))

    # lstm -> output
    n.addConnection(FullConnection(lstm, o, name = 'loc'))

    # bias -> output
    n.addConnection(FullConnection(b, lstm))

    n.sortModules()
    return n