src/actrec.py
def lstm_cond_layer(tparams, state_below, options, prefix='lstm',
                    mask=None, init_memory=None, init_state=None,
                    trng=None, use_noise=None,
                    **kwargs):
    """
    Computation graph for the conditional LSTM.
    """
    nsteps = state_below.shape[0]
    n_samples = state_below.shape[1]
    n_annotations = state_below.shape[2]

    # mask
    if mask == None:
        mask = tensor.alloc(1., state_below.shape[0], 1)

    dim = tparams[_p(prefix, 'U')].shape[0]

    # initial/previous state
    if init_state == None:
        init_state = tensor.alloc(0., n_samples, dim)
    # initial/previous memory 
    if init_memory == None:
        init_memory = tensor.alloc(0., n_samples, dim)

    def _slice(_x, n, dim):
        if _x.ndim == 3:
            return _x[:, :, n*dim:(n+1)*dim]
        return _x[:, n*dim:(n+1)*dim]

    def _step(m_, x_, h_, c_, a_, ct_, dp_=None, dp_att_=None):
	
        # mask, xt, ht-1, ct-1, alpha, ctx
        # attention
        pstate_ = tensor.dot(h_, tparams[_p(prefix,'Wd_att')]) # pstate_
        pctx_ = tensor.dot(x_, tparams[_p(prefix,'Wc_att')]) + tparams[_p(prefix, 'b_att')]
        if options['n_layers_att'] > 1:
            for lidx in xrange(1, options['n_layers_att']):
                pctx_ = tensor.dot(pctx_, tparams[_p(prefix,'W_att_%d'%lidx)])+tparams[_p(prefix, 'b_att_%d'%lidx)]
                if lidx < options['n_layers_att'] - 1:
                    pctx_ = tanh(pctx_)
        pctx_ = pctx_ + pstate_[:,None,:]
        pctx_list = []
        pctx_list.append(pctx_)
        pctx_ = tanh(pctx_)
        alpha = tensor.dot(pctx_, tparams[_p(prefix,'U_att')])+tparams[_p(prefix, 'c_tt')]
        alpha_pre = alpha
        alpha_shp = alpha.shape
        alpha = tensor.nnet.softmax(options['temperature_inverse']*alpha.reshape([alpha_shp[0],alpha_shp[1]])) # softmax
        ctx_ = (x_ * alpha[:,:,None]).sum(1) # current context
        # print '\n\ncheck\n\n'
        if options['selector']:
            sel_ = tensor.nnet.sigmoid(tensor.dot(h_, tparams[_p(prefix, 'W_sel')])+tparams[_p(prefix,'b_sel')])
            sel_ = sel_.reshape([sel_.shape[0]])
            ctx_ = sel_[:,None] * ctx_

        preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
        preact += tensor.dot(ctx_, tparams[_p(prefix, 'W')]) + tparams[_p(prefix, 'b')]

        i = _slice(preact, 0, dim)              # z_it
        f = _slice(preact, 1, dim)              # z_ft
        o = _slice(preact, 2, dim)              # z_ot
        i = tensor.nnet.sigmoid(i)              # it = sigmoid(z_it)
        f = tensor.nnet.sigmoid(f)              # ft = sigmoid(z_ft)
        o = tensor.nnet.sigmoid(o)              # ot = sigmoid(z_ot)
        c = tensor.tanh(_slice(preact, 3, dim)) # at = tanh(z_at)
        
	# compute the new memory/hidden state
        # if the mask is 0, just copy the previous state
        c = f * c_ + i * c                      # ct = ft * ct-1 + it * at
        c = m_[:,None] * c + (1. - m_)[:,None] * c_

        h = o * tensor.tanh(c)                  # ht = ot * thanh(ct)
        h = m_[:,None] * h + (1. - m_)[:,None] * h_

        rval = [h, c, alpha, ctx_]
        if options['selector']:
            rval += [sel_]
        rval += [pstate_, pctx_, i, f, o, preact, alpha_pre]+pctx_list
        # print '\n\ncheck\n\n'
        return rval

    if options['selector']:
        _step0 = lambda m_, x_, h_, c_, a_, ct_, sel_: _step(m_, x_, h_, c_, a_, ct_)
    else:
        _step0 = lambda m_, x_, h_, c_, a_, ct_: _step(m_, x_, h_, c_, a_, ct_)

    seqs = [mask, state_below]
    outputs_info = [init_state,
                    init_memory,
                    tensor.alloc(0., n_samples, n_annotations),
                    tensor.alloc(0., n_samples, options['ctx_dim'])]
    if options['selector']:
        outputs_info += [tensor.alloc(0., n_samples)]
    outputs_info += [None,
                     None,
                     None,
                     None,
                     None,
                     None,
                     None] + [None]#*options['n_layers_att']
    rval, updates = theano.scan(_step0,
                                sequences=seqs,
                                outputs_info=outputs_info,
                                name=_p(prefix, '_layers'),
                                n_steps=nsteps, profile=False)
    return rval

    Computation graph for the conditional LSTM.
    
Computation graph for the conditional LSTM.
Computation graph for the conditional LSTM .
def lstm_cond_layer(tparams, state_below, options, prefix='lstm',
                    mask=None, init_memory=None, init_state=None,
                    trng=None, use_noise=None,
                    **kwargs):
    nsteps = state_below.shape[0]
    n_samples = state_below.shape[1]
    n_annotations = state_below.shape[2]
    if mask == None:
        mask = tensor.alloc(1., state_below.shape[0], 1)
    dim = tparams[_p(prefix, 'U')].shape[0]
    if init_state == None:
        init_state = tensor.alloc(0., n_samples, dim)
    if init_memory == None:
        init_memory = tensor.alloc(0., n_samples, dim)
    def _slice(_x, n, dim):
        if _x.ndim == 3:
            return _x[:, :, n*dim:(n+1)*dim]
        return _x[:, n*dim:(n+1)*dim]
    def _step(m_, x_, h_, c_, a_, ct_, dp_=None, dp_att_=None):
        , xt, ht-1, ct-1, alpha, ctx
        pstate_ = tensor.dot(h_, tparams[_p(prefix,'Wd_att')]) 
        pctx_ = tensor.dot(x_, tparams[_p(prefix,'Wc_att')]) + tparams[_p(prefix, 'b_att')]
        if options['n_layers_att'] > 1:
            for lidx in xrange(1, options['n_layers_att']):
                pctx_ = tensor.dot(pctx_, tparams[_p(prefix,'W_att_%d'%lidx)])+tparams[_p(prefix, 'b_att_%d'%lidx)]
                if lidx < options['n_layers_att'] - 1:
                    pctx_ = tanh(pctx_)
        pctx_ = pctx_ + pstate_[:,None,:]
        pctx_list = []
        pctx_list.append(pctx_)
        pctx_ = tanh(pctx_)
        alpha = tensor.dot(pctx_, tparams[_p(prefix,'U_att')])+tparams[_p(prefix, 'c_tt')]
        alpha_pre = alpha
        alpha_shp = alpha.shape
        alpha = tensor.nnet.softmax(options['temperature_inverse']*alpha.reshape([alpha_shp[0],alpha_shp[1]])) 
        ctx_ = (x_ * alpha[:,:,None]).sum(1) 
        if options['selector']:
            sel_ = tensor.nnet.sigmoid(tensor.dot(h_, tparams[_p(prefix, 'W_sel')])+tparams[_p(prefix,'b_sel')])
            sel_ = sel_.reshape([sel_.shape[0]])
            ctx_ = sel_[:,None] * ctx_
        preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
        preact += tensor.dot(ctx_, tparams[_p(prefix, 'W')]) + tparams[_p(prefix, 'b')]
        i = _slice(preact, 0, dim)              
        f = _slice(preact, 1, dim)              
        o = _slice(preact, 2, dim)              
        i = tensor.nnet.sigmoid(i)              
        f = tensor.nnet.sigmoid(f)              
        o = tensor.nnet.sigmoid(o)              
        c = tensor.tanh(_slice(preact, 3, dim)) 
        c = f * c_ + i * c                      
        c = m_[:,None] * c + (1. - m_)[:,None] * c_
        h = o * tensor.tanh(c)                  
        h = m_[:,None] * h + (1. - m_)[:,None] * h_
        rval = [h, c, alpha, ctx_]
        if options['selector']:
            rval += [sel_]
        rval += [pstate_, pctx_, i, f, o, preact, alpha_pre]+pctx_list
        return rval
    if options['selector']:
        _step0 = lambda m_, x_, h_, c_, a_, ct_, sel_: _step(m_, x_, h_, c_, a_, ct_)
    else:
        _step0 = lambda m_, x_, h_, c_, a_, ct_: _step(m_, x_, h_, c_, a_, ct_)
    seqs = [mask, state_below]
    outputs_info = [init_state,
                    init_memory,
                    tensor.alloc(0., n_samples, n_annotations),
                    tensor.alloc(0., n_samples, options['ctx_dim'])]
    if options['selector']:
        outputs_info += [tensor.alloc(0., n_samples)]
    outputs_info += [None,
                     None,
                     None,
                     None,
                     None,
                     None,
                     None] + [None]
    rval, updates = theano.scan(_step0,
                                sequences=seqs,
                                outputs_info=outputs_info,
                                name=_p(prefix, '_layers'),
                                n_steps=nsteps, profile=False)
    return rval
def lstm_cond_layer ( tparams , state_below , options , prefix = 'lstm' , mask = None , init_memory = None , init_state = None , trng = None , use_noise = None , ** kwargs ) : nsteps = state_below . shape [ 0 ] n_samples = state_below . shape [ 1 ] n_annotations = state_below . shape [ 2 ] if mask == None : mask = tensor . alloc ( 1. , state_below . shape [ 0 ] , 1 ) dim = tparams [ _p ( prefix , 'U' ) ] . shape [ 0 ] if init_state == None : init_state = tensor . alloc ( 0. , n_samples , dim ) if init_memory == None : init_memory = tensor . alloc ( 0. , n_samples , dim ) def _slice ( _x , n , dim ) : if _x . ndim == 3 : return _x [ : , : , n * dim : ( n + 1 ) * dim ] return _x [ : , n * dim : ( n + 1 ) * dim ] def _step ( m_ , x_ , h_ , c_ , a_ , ct_ , dp_ = None , dp_att_ = None ) : pstate_ = tensor . dot ( h_ , tparams [ _p ( prefix , 'Wd_att' ) ] ) pctx_ = tensor . dot ( x_ , tparams [ _p ( prefix , 'Wc_att' ) ] ) + tparams [ _p ( prefix , 'b_att' ) ] if options [ 'n_layers_att' ] > 1 : for lidx in xrange ( 1 , options [ 'n_layers_att' ] ) : pctx_ = tensor . dot ( pctx_ , tparams [ _p ( prefix , 'W_att_%d' % lidx ) ] ) + tparams [ _p ( prefix , 'b_att_%d' % lidx ) ] if lidx < options [ 'n_layers_att' ] - 1 : pctx_ = tanh ( pctx_ ) pctx_ = pctx_ + pstate_ [ : , None , : ] pctx_list = [ ] pctx_list . append ( pctx_ ) pctx_ = tanh ( pctx_ ) alpha = tensor . dot ( pctx_ , tparams [ _p ( prefix , 'U_att' ) ] ) + tparams [ _p ( prefix , 'c_tt' ) ] alpha_pre = alpha alpha_shp = alpha . shape alpha = tensor . nnet . softmax ( options [ 'temperature_inverse' ] * alpha . reshape ( [ alpha_shp [ 0 ] , alpha_shp [ 1 ] ] ) ) ctx_ = ( x_ * alpha [ : , : , None ] ) . sum ( 1 ) if options [ 'selector' ] : sel_ = tensor . nnet . sigmoid ( tensor . dot ( h_ , tparams [ _p ( prefix , 'W_sel' ) ] ) + tparams [ _p ( prefix , 'b_sel' ) ] ) sel_ = sel_ . reshape ( [ sel_ . shape [ 0 ] ] ) ctx_ = sel_ [ : , None ] * ctx_ preact = tensor . dot ( h_ , tparams [ _p ( prefix , 'U' ) ] ) preact += tensor . dot ( ctx_ , tparams [ _p ( prefix , 'W' ) ] ) + tparams [ _p ( prefix , 'b' ) ] i = _slice ( preact , 0 , dim ) f = _slice ( preact , 1 , dim ) o = _slice ( preact , 2 , dim ) i = tensor . nnet . sigmoid ( i ) f = tensor . nnet . sigmoid ( f ) o = tensor . nnet . sigmoid ( o ) c = tensor . tanh ( _slice ( preact , 3 , dim ) ) c = f * c_ + i * c c = m_ [ : , None ] * c + ( 1. - m_ ) [ : , None ] * c_ h = o * tensor . tanh ( c ) h = m_ [ : , None ] * h + ( 1. - m_ ) [ : , None ] * h_ rval = [ h , c , alpha , ctx_ ] if options [ 'selector' ] : rval += [ sel_ ] rval += [ pstate_ , pctx_ , i , f , o , preact , alpha_pre ] + pctx_list return rval if options [ 'selector' ] : _step0 = lambda m_ , x_ , h_ , c_ , a_ , ct_ , sel_ : _step ( m_ , x_ , h_ , c_ , a_ , ct_ ) else : _step0 = lambda m_ , x_ , h_ , c_ , a_ , ct_ : _step ( m_ , x_ , h_ , c_ , a_ , ct_ ) seqs = [ mask , state_below ] outputs_info = [ init_state , init_memory , tensor . alloc ( 0. , n_samples , n_annotations ) , tensor . alloc ( 0. , n_samples , options [ 'ctx_dim' ] ) ] if options [ 'selector' ] : outputs_info += [ tensor . alloc ( 0. , n_samples ) ] outputs_info += [ None , None , None , None , None , None , None ] + [ None ] rval , updates = theano . scan ( _step0 , sequences = seqs , outputs_info = outputs_info , name = _p ( prefix , '_layers' ) , n_steps = nsteps , profile = False ) return rval
Computation graph for the conditional LSTM.
Computation graph for the conditional LSTM .
"""
    Computation graph for the conditional LSTM.
    """ # mask # initial/previous state # initial/previous memory  # mask, xt, ht-1, ct-1, alpha, ctx # attention # pstate_ # softmax # current context # print '\n\ncheck\n\n' # z_it # z_ft # z_ot # it = sigmoid(z_it) # ft = sigmoid(z_ft) # ot = sigmoid(z_ot) # at = tanh(z_at) # compute the new memory/hidden state # if the mask is 0, just copy the previous state # ct = ft * ct-1 + it * at # ht = ot * thanh(ct) # print '\n\ncheck\n\n' #*options['n_layers_att']
tparams
state_below
options
prefix
mask
init_memory
init_state
trng
use_noise