当前位置: 首页>>代码示例>>Python>>正文


Python dynet.lookup方法代码示例

本文整理汇总了Python中dynet.lookup方法的典型用法代码示例。如果您正苦于以下问题:Python dynet.lookup方法的具体用法?Python dynet.lookup怎么用?Python dynet.lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dynet的用法示例。


在下文中一共展示了dynet.lookup方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: word_rep

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def word_rep(self, word):
        '''
        :param word: index of word in lookup table
        '''

        if options.bigram:
            word = word[1]
            pass

        if self.use_char_rnn:
            # Note: use original casing ("word") for characters
            char_ids = self.word_to_char_ids[word]
            char_embs = [self.char_lookup[cid] for cid in char_ids]
            char_exprs = self.char_bi_lstm.transduce(char_embs)
            if self.use_we:
                wemb = dy.lookup(self.words_lookup, word, update=self.we_update)
                return dy.concatenate([wemb, char_exprs[-1]])
            else:
                return char_exprs[-1]
        else:
            wemb = dy.lookup(self.words_lookup, word, update=self.we_update)
            return wemb 
开发者ID:hankcs,项目名称:multi-criteria-cws,代码行数:24,代码来源:model.py

示例2: __init__

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def __init__(self, words, vectors=None, is_unique=False, update=True):
        # This option is to preserve order in case order is needed
        if not is_unique:
            words = list(set(words))

        # Update pre-trained vectors
        self.update = update
        # Pre-trained vectors
        self.vectors = vectors

        # Word to index dictionary (encode)
        self.w2i = {w: i for i, w in enumerate(words)}
        # Index to word dictionary (decode)
        self.i2w = {i: w for w, i in self.w2i.items()}
        # Unknown vectors
        self.UNK = None
        self.UNKNumber = None
        # Model lookup table
        self.lookup_table = None 
开发者ID:AmitMY,项目名称:chimera,代码行数:21,代码来源:dynet_model_executer.py

示例3: word_repr

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def word_repr(self, char_seq, cembs):
        # obtain the word representation when given its character sequence

        wlen = len(char_seq)
        if 'rgW%d'%wlen not in self.param_exprs:
            self.param_exprs['rgW%d'%wlen] = dy.parameter(self.params['reset_gate_W'][wlen-1])
            self.param_exprs['rgb%d'%wlen] = dy.parameter(self.params['reset_gate_b'][wlen-1])
            self.param_exprs['cW%d'%wlen] = dy.parameter(self.params['com_W'][wlen-1])
            self.param_exprs['cb%d'%wlen] = dy.parameter(self.params['com_b'][wlen-1])

        chars = dy.concatenate(cembs)
        reset_gate = dy.logistic(self.param_exprs['rgW%d'%wlen] * chars + self.param_exprs['rgb%d'%wlen])
        word = dy.tanh(self.param_exprs['cW%d'%wlen] * dy.cmult(reset_gate,chars) + self.param_exprs['cb%d'%wlen])
        if self.known_words is not None and tuple(char_seq) in self.known_words:
            return (word + dy.lookup(self.params['word_embed'],self.known_words[tuple(char_seq)]))/2.
        return word 
开发者ID:jcyk,项目名称:greedyCWS,代码行数:18,代码来源:model.py

示例4: word_rep

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def word_rep(self, word, char_ids):
        '''
        :param word: index of word in lookup table
        '''
        wemb = dy.lookup(self.words_lookup, word, update=self.we_update)
        if char_ids is None:
            return wemb

        # add character representation
        char_embs = [self.char_lookup[cid] for cid in char_ids]
        char_exprs = self.char_bi_lstm.transduce(char_embs)
        #char_exprs[-1] contains the final forward hidden state,
        #but the initial backward hidden state
        forward = char_exprs[-1][:self.char_hidden_dim // 2]
        backward = char_exprs[0][self.char_hidden_dim // 2:]
        return dy.concatenate([wemb, forward, backward]) 
开发者ID:yuvalpinter,项目名称:Mimick,代码行数:18,代码来源:model.py

示例5: build_tagging_graph

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def build_tagging_graph(self, sentence):
        dy.renew_cg()

        embeddings = [self.word_rep(w) for w in sentence]

        lstm_out = self.bi_lstm.transduce(embeddings)

        H = dy.parameter(self.lstm_to_tags_params)
        Hb = dy.parameter(self.lstm_to_tags_bias)
        O = dy.parameter(self.mlp_out)
        Ob = dy.parameter(self.mlp_out_bias)
        scores = []
        if options.bigram:
            for rep, word in zip(lstm_out, sentence):
                bi1 = dy.lookup(self.bigram_lookup, word[0], update=self.we_update)
                bi2 = dy.lookup(self.bigram_lookup, word[1], update=self.we_update)
                if self.dropout is not None:
                    bi1 = dy.dropout(bi1, self.dropout)
                    bi2 = dy.dropout(bi2, self.dropout)
                score_t = O * dy.tanh(H * dy.concatenate(
                    [bi1,
                     rep,
                     bi2]) + Hb) + Ob
                scores.append(score_t)
        else:
            for rep in lstm_out:
                score_t = O * dy.tanh(H * rep + Hb) + Ob
                scores.append(score_t)

        return scores 
开发者ID:hankcs,项目名称:multi-criteria-cws,代码行数:32,代码来源:model.py

示例6: lookup

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def lookup(self, word):
        if word in self.w2i:
            return dy.lookup(self.lookup_table, self.w2i[word], update=self.update)

        try:
            float(word)
            return self.UNKNumber
        except ValueError:
            return self.UNK 
开发者ID:AmitMY,项目名称:chimera,代码行数:11,代码来源:dynet_model_executer.py

示例7: __call__

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def __call__(self, query, options, gold, lengths, query_no):
        if len(options) == 1:
            return None, 0

        final = []
        if args.word_vectors:
            qvecs = [dy.lookup(self.pEmbedding, w) for w in query]
            qvec_max = dy.emax(qvecs)
            qvec_mean = dy.average(qvecs)
        for otext, features in options:
            inputs = dy.inputTensor(features)
            if args.word_vectors:
                ovecs = [dy.lookup(self.pEmbedding, w) for w in otext]
                ovec_max = dy.emax(ovecs)
                ovec_mean = dy.average(ovecs)
                inputs = dy.concatenate([inputs, qvec_max, qvec_mean, ovec_max, ovec_mean])
            if args.drop > 0:
                inputs = dy.dropout(inputs, args.drop)
            h = inputs
            for pH, pB in zip(self.hidden, self.bias):
                h = dy.affine_transform([pB, pH, h])
                if args.nonlin == "linear":
                    pass
                elif args.nonlin == "tanh":
                    h = dy.tanh(h)
                elif args.nonlin == "cube":
                    h = dy.cube(h)
                elif args.nonlin == "logistic":
                    h = dy.logistic(h)
                elif args.nonlin == "relu":
                    h = dy.rectify(h)
                elif args.nonlin == "elu":
                    h = dy.elu(h)
                elif args.nonlin == "selu":
                    h = dy.selu(h)
                elif args.nonlin == "softsign":
                    h = dy.softsign(h)
                elif args.nonlin == "swish":
                    h = dy.cmult(h, dy.logistic(h))
            final.append(dy.sum_dim(h, [0]))

        final = dy.concatenate(final)
        nll = -dy.log_softmax(final)
        dense_gold = []
        for i in range(len(options)):
            dense_gold.append(1.0 / len(gold) if i in gold else 0.0)
        answer = dy.inputTensor(dense_gold)
        loss = dy.transpose(answer) * nll
        predicted_link = np.argmax(final.npvalue())

        return loss, predicted_link 
开发者ID:dstc8-track2,项目名称:NOESIS-II,代码行数:53,代码来源:disentangle.py

示例8: greedy_search

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def greedy_search(self, char_seq, truth = None, mu =0.):
        init_state = self.params['lstm'].initial_state().add_input(self.param_exprs['<bos>'])
        init_y = dy.tanh(self.param_exprs['pW'] * init_state.output() + self.param_exprs['pb'])
        init_score = dy.scalarInput(0.)
        init_sentence = Sentence(score=init_score.scalar_value(),score_expr=init_score,LSTMState =init_state, y= init_y , prevState = None, wlen=None, golden=True)
        
        if truth is not None:
            cembs = [ dy.dropout(dy.lookup(self.params['embed'],char),self.options['dropout_rate']) for char in char_seq ]
        else:
            cembs = [dy.lookup(self.params['embed'],char) for char in char_seq ]

        start_agenda = init_sentence
        agenda = [start_agenda]

        for idx, _ in enumerate(char_seq,1): # from left to right, character by character
            now = None
            for wlen in xrange(1,min(idx,self.options['max_word_len'])+1): # generate word candidate vectors
                # join segmentation sent + word
                word = self.word_repr(char_seq[idx-wlen:idx], cembs[idx-wlen:idx])
                sent = agenda[idx-wlen]

                if truth is not None:
                    word = dy.dropout(word,self.options['dropout_rate'])
                
                word_score = dy.dot_product(word,self.param_exprs['U'])

                if truth is not None:
                    golden =  sent.golden and truth[idx-1]==wlen
                    margin = dy.scalarInput(mu*wlen if truth[idx-1]!=wlen else 0.)
                    score = margin + sent.score_expr + dy.dot_product(sent.y, word) + word_score
                else:
                    golden = False
                    score = sent.score_expr + dy.dot_product(sent.y, word) + word_score


                good = (now is None or now.score < score.scalar_value())
                if golden or good:
                    new_state = sent.LSTMState.add_input(word)
                    new_y = dy.tanh(self.param_exprs['pW'] * new_state.output() + self.param_exprs['pb'])
                    new_sent = Sentence(score=score.scalar_value(),score_expr=score,LSTMState=new_state,y=new_y, prevState=sent, wlen=wlen, golden=golden)
                    if good:
                        now = new_sent
                    if golden:
                        golden_sent = new_sent

            agenda.append(now)
            if truth is not None and truth[idx-1]>0 and (not now.golden):
                return (now.score_expr - golden_sent.score_expr)

        if truth is not None:
            return (now.score_expr - golden_sent.score_expr)

        return agenda 
开发者ID:jcyk,项目名称:greedyCWS,代码行数:55,代码来源:model.py

示例9: evaluate_recurrent

# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import lookup [as 别名]
def evaluate_recurrent(self, word_inds, tag_inds, test=False):

        fwd1 = self.fwd_lstm1.initial_state()
        back1 = self.back_lstm1.initial_state()

        fwd2 = self.fwd_lstm2.initial_state()
        back2 = self.back_lstm2.initial_state()

        sentence = []

        for (w, t) in zip(word_inds, tag_inds):
            wordvec = dynet.lookup(self.word_embed, w)
            tagvec = dynet.lookup(self.tag_embed, t)
            vec = dynet.concatenate([wordvec, tagvec])
            sentence.append(vec)

        fwd1_out = []
        for vec in sentence:
            fwd1 = fwd1.add_input(vec)
            fwd_vec = fwd1.output()
            fwd1_out.append(fwd_vec)

        back1_out = []
        for vec in reversed(sentence):
            back1 = back1.add_input(vec)
            back_vec = back1.output()
            back1_out.append(back_vec)

        lstm2_input = []
        for (f, b) in zip(fwd1_out, reversed(back1_out)):
            lstm2_input.append(dynet.concatenate([f, b]))

        fwd2_out = []
        for vec in lstm2_input:
            if self.droprate > 0 and not test:
                vec = dynet.dropout(vec, self.droprate)
            fwd2 = fwd2.add_input(vec)
            fwd_vec = fwd2.output()
            fwd2_out.append(fwd_vec)

        back2_out = []
        for vec in reversed(lstm2_input):
            if self.droprate > 0 and not test:
                vec = dynet.dropout(vec, self.droprate)
            back2 = back2.add_input(vec)
            back_vec = back2.output()
            back2_out.append(back_vec)

        fwd_out = [dynet.concatenate([f1, f2]) for (f1, f2) in zip(fwd1_out, fwd2_out)]
        back_out = [dynet.concatenate([b1, b2]) for (b1, b2) in zip(back1_out, back2_out)]

        return fwd_out, back_out[::-1] 
开发者ID:jhcross,项目名称:span-parser,代码行数:54,代码来源:network.py


注:本文中的dynet.lookup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。