(tparams, options, use_noise, trng, sampling=True)
return f_init, f_next
Builds a sampler used for generating from the model
    Parameters
    ----------
        See build_model function above
    Returns
    -------
    f_init : theano function
        Input: annotation, Output: initial lstm state and memory
        (also performs transformation on ctx0 if using lstm_encoder)
    f_next: theano function
        Takes the previous word/state/memory + ctx0 and runs ne
        step through the lstm (used for beam search)
Builds a sampler used for generating from the model
    Parameters
    ----------
        See build_model function above
    Returns
    -------
    f_init : theano function
        Input: annotation, Output: initial lstm state and memory
        (also performs transformation on ctx0 if using lstm_encoder)
    f_next: theano function
        Takes the previous word/state/memory + ctx0 and runs ne
        step through the lstm (used for beam search)
Builds a sampler used for generating from the model Parameters ---------- See build_model function above Returns ------- f_init : theano function Input : annotation Output : initial lstm state and memory ( also performs transformation on ctx0 if using lstm_encoder ) f_next : theano function Takes the previous word / state / memory + ctx0 and runs ne step through the lstm ( used for beam search )
def build_sampler(tparams, options, use_noise, trng, sampling=True):
    """ Builds a sampler used for generating from the model
    Parameters
    ----------
        See build_model function above
    Returns
    -------
    f_init : theano function
        Input: annotation, Output: initial lstm state and memory
        (also performs transformation on ctx0 if using lstm_encoder)
    f_next: theano function
        Takes the previous word/state/memory + ctx0 and runs ne
        step through the lstm (used for beam search)
    """
    # context: #annotations x dim
    ctx = tensor.matrix('ctx_sampler', dtype='float32')
    if options['lstm_encoder']:
        # encoder
        ctx_fwd = get_layer('lstm')[1](tparams, ctx,
                                       options, prefix='encoder')[0]
        ctx_rev = get_layer('lstm')[1](tparams, ctx[::-1,:],
                                       options, prefix='encoder_rev')[0][::-1,:]
        ctx = tensor.concatenate((ctx_fwd, ctx_rev), axis=1)

    # initial state/cell
    ctx_mean = ctx.mean(0)
    for lidx in xrange(1, options['n_layers_init']):
        ctx_mean = get_layer('ff')[1](tparams, ctx_mean, options,
                                      prefix='ff_init_%d'%lidx, activ='rectifier')
        if options['use_dropout']:
            ctx_mean = dropout_layer(ctx_mean, use_noise, trng)
    init_state = [get_layer('ff')[1](tparams, ctx_mean, options, prefix='ff_state', activ='tanh')]
    init_memory = [get_layer('ff')[1](tparams, ctx_mean, options, prefix='ff_memory', activ='tanh')]
    if options['n_layers_lstm'] > 1:
        for lidx in xrange(1, options['n_layers_lstm']):
            init_state.append(get_layer('ff')[1](tparams, ctx_mean, options, prefix='ff_state_%d'%lidx, activ='tanh'))
            init_memory.append(get_layer('ff')[1](tparams, ctx_mean, options, prefix='ff_memory_%d'%lidx, activ='tanh'))

    print 'Building f_init...',
    f_init = theano.function([ctx], [ctx]+init_state+init_memory, name='f_init', profile=False)
    print 'Done'

    # build f_next
    ctx = tensor.matrix('ctx_sampler', dtype='float32')
    x = tensor.vector('x_sampler', dtype='int64')
    init_state = [tensor.matrix('init_state', dtype='float32')]
    init_memory = [tensor.matrix('init_memory', dtype='float32')]
    if options['n_layers_lstm'] > 1:
        for lidx in xrange(1, options['n_layers_lstm']):
            init_state.append(tensor.matrix('init_state', dtype='float32'))
            init_memory.append(tensor.matrix('init_memory', dtype='float32'))

    # for the first word (which is coded with -1), emb should be all zero
    emb = tensor.switch(x[:,None] < 0, tensor.alloc(0., 1, tparams['Wemb'].shape[1]),
                        tparams['Wemb'][x])

    proj = get_layer('lstm_cond')[1](tparams, emb, options,
                                     prefix='decoder',
                                     mask=None, context=ctx,
                                     one_step=True,
                                     init_state=init_state[0],
                                     init_memory=init_memory[0],
                                     trng=trng,
                                     use_noise=use_noise,
                                     sampling=sampling)

    next_state, next_memory, ctxs = [proj[0]], [proj[1]], [proj[4]]
    proj_h = proj[0]
    if options['n_layers_lstm'] > 1:
        for lidx in xrange(1, options['n_layers_lstm']):
            proj = get_layer('lstm_cond')[1](tparams, proj_h, options,
                                             prefix='decoder_%d'%lidx,
                                             context=ctx,
                                             one_step=True,
                                             init_state=init_state[lidx],
                                             init_memory=init_memory[lidx],
                                             trng=trng,
                                             use_noise=use_noise,
                                             sampling=sampling)
            next_state.append(proj[0])
            next_memory.append(proj[1])
            ctxs.append(proj[4])
            proj_h = proj[0]

    if options['use_dropout']:
        proj_h = dropout_layer(proj[0], use_noise, trng)
    else:
        proj_h = proj[0]
    logit = get_layer('ff')[1](tparams, proj_h, options, prefix='ff_logit_lstm', activ='linear')
    if options['prev2out']:
        logit += emb
    if options['ctx2out']:
        logit += get_layer('ff')[1](tparams, ctxs[-1], options, prefix='ff_logit_ctx', activ='linear')
    logit = tanh(logit)
    if options['use_dropout']:
        logit = dropout_layer(logit, use_noise, trng)
    if options['n_layers_out'] > 1:
        for lidx in xrange(1, options['n_layers_out']):
            logit = get_layer('ff')[1](tparams, logit, options, prefix='ff_logit_h%d'%lidx, activ='rectifier')
            if options['use_dropout']:
                logit = dropout_layer(logit, use_noise, trng)
    logit = get_layer('ff')[1](tparams, logit, options, prefix='ff_logit', activ='linear')
    logit_shp = logit.shape
    next_probs = tensor.nnet.softmax(logit)
    next_sample = trng.multinomial(pvals=next_probs).argmax(1)

    # next word probability
    f_next = theano.function([x, ctx]+init_state+init_memory, [next_probs, next_sample]+next_state+next_memory, name='f_next', profile=False)

    return f_init, f_next
def build_sampler ( tparams , options , use_noise , trng , sampling = True ) : # context: #annotations x dim ctx = tensor . matrix ( 'ctx_sampler' , dtype = 'float32' ) if options [ 'lstm_encoder' ] : # encoder ctx_fwd = get_layer ( 'lstm' ) [ 1 ] ( tparams , ctx , options , prefix = 'encoder' ) [ 0 ] ctx_rev = get_layer ( 'lstm' ) [ 1 ] ( tparams , ctx [ : : - 1 , : ] , options , prefix = 'encoder_rev' ) [ 0 ] [ : : - 1 , : ] ctx = tensor . concatenate ( ( ctx_fwd , ctx_rev ) , axis = 1 ) # initial state/cell ctx_mean = ctx . mean ( 0 ) for lidx in xrange ( 1 , options [ 'n_layers_init' ] ) : ctx_mean = get_layer ( 'ff' ) [ 1 ] ( tparams , ctx_mean , options , prefix = 'ff_init_%d' % lidx , activ = 'rectifier' ) if options [ 'use_dropout' ] : ctx_mean = dropout_layer ( ctx_mean , use_noise , trng ) init_state = [ get_layer ( 'ff' ) [ 1 ] ( tparams , ctx_mean , options , prefix = 'ff_state' , activ = 'tanh' ) ] init_memory = [ get_layer ( 'ff' ) [ 1 ] ( tparams , ctx_mean , options , prefix = 'ff_memory' , activ = 'tanh' ) ] if options [ 'n_layers_lstm' ] > 1 : for lidx in xrange ( 1 , options [ 'n_layers_lstm' ] ) : init_state . append ( get_layer ( 'ff' ) [ 1 ] ( tparams , ctx_mean , options , prefix = 'ff_state_%d' % lidx , activ = 'tanh' ) ) init_memory . append ( get_layer ( 'ff' ) [ 1 ] ( tparams , ctx_mean , options , prefix = 'ff_memory_%d' % lidx , activ = 'tanh' ) ) print 'Building f_init...' , f_init = theano . function ( [ ctx ] , [ ctx ] + init_state + init_memory , name = 'f_init' , profile = False ) print 'Done' # build f_next ctx = tensor . matrix ( 'ctx_sampler' , dtype = 'float32' ) x = tensor . vector ( 'x_sampler' , dtype = 'int64' ) init_state = [ tensor . matrix ( 'init_state' , dtype = 'float32' ) ] init_memory = [ tensor . matrix ( 'init_memory' , dtype = 'float32' ) ] if options [ 'n_layers_lstm' ] > 1 : for lidx in xrange ( 1 , options [ 'n_layers_lstm' ] ) : init_state . append ( tensor . matrix ( 'init_state' , dtype = 'float32' ) ) init_memory . append ( tensor . matrix ( 'init_memory' , dtype = 'float32' ) ) # for the first word (which is coded with -1), emb should be all zero emb = tensor . switch ( x [ : , None ] < 0 , tensor . alloc ( 0. , 1 , tparams [ 'Wemb' ] . shape [ 1 ] ) , tparams [ 'Wemb' ] [ x ] ) proj = get_layer ( 'lstm_cond' ) [ 1 ] ( tparams , emb , options , prefix = 'decoder' , mask = None , context = ctx , one_step = True , init_state = init_state [ 0 ] , init_memory = init_memory [ 0 ] , trng = trng , use_noise = use_noise , sampling = sampling ) next_state , next_memory , ctxs = [ proj [ 0 ] ] , [ proj [ 1 ] ] , [ proj [ 4 ] ] proj_h = proj [ 0 ] if options [ 'n_layers_lstm' ] > 1 : for lidx in xrange ( 1 , options [ 'n_layers_lstm' ] ) : proj = get_layer ( 'lstm_cond' ) [ 1 ] ( tparams , proj_h , options , prefix = 'decoder_%d' % lidx , context = ctx , one_step = True , init_state = init_state [ lidx ] , init_memory = init_memory [ lidx ] , trng = trng , use_noise = use_noise , sampling = sampling ) next_state . append ( proj [ 0 ] ) next_memory . append ( proj [ 1 ] ) ctxs . append ( proj [ 4 ] ) proj_h = proj [ 0 ] if options [ 'use_dropout' ] : proj_h = dropout_layer ( proj [ 0 ] , use_noise , trng ) else : proj_h = proj [ 0 ] logit = get_layer ( 'ff' ) [ 1 ] ( tparams , proj_h , options , prefix = 'ff_logit_lstm' , activ = 'linear' ) if options [ 'prev2out' ] : logit += emb if options [ 'ctx2out' ] : logit += get_layer ( 'ff' ) [ 1 ] ( tparams , ctxs [ - 1 ] , options , prefix = 'ff_logit_ctx' , activ = 'linear' ) logit = tanh ( logit ) if options [ 'use_dropout' ] : logit = dropout_layer ( logit , use_noise , trng ) if options [ 'n_layers_out' ] > 1 : for lidx in xrange ( 1 , options [ 'n_layers_out' ] ) : logit = get_layer ( 'ff' ) [ 1 ] ( tparams , logit , options , prefix = 'ff_logit_h%d' % lidx , activ = 'rectifier' ) if options [ 'use_dropout' ] : logit = dropout_layer ( logit , use_noise , trng ) logit = get_layer ( 'ff' ) [ 1 ] ( tparams , logit , options , prefix = 'ff_logit' , activ = 'linear' ) logit_shp = logit . shape next_probs = tensor . nnet . softmax ( logit ) next_sample = trng . multinomial ( pvals = next_probs ) . argmax ( 1 ) # next word probability f_next = theano . function ( [ x , ctx ] + init_state + init_memory , [ next_probs , next_sample ] + next_state + next_memory , name = 'f_next' , profile = False ) return f_init , f_next