本文整理汇总了Python中dynet.concatenate方法的典型用法代码示例。如果您正苦于以下问题:Python dynet.concatenate方法的具体用法?Python dynet.concatenate怎么用?Python dynet.concatenate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dynet
的用法示例。
在下文中一共展示了dynet.concatenate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: word_rep
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [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
示例2: forward
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def forward(self, observations):
def log_sum_exp(scores):
npval = scores.npvalue()
argmax_score = np.argmax(npval)
max_score_expr = dy.pick(scores, argmax_score)
max_score_expr_broadcast = dy.concatenate([max_score_expr] * self.tagset_size)
return max_score_expr + dy.log(dy.sum_dim(dy.transpose(dy.exp(scores - max_score_expr_broadcast)),[1]))
init_alphas = [-1e10] * self.tagset_size
init_alphas[t2i[START_TAG]] = 0
for_expr = dy.inputVector(init_alphas)
for obs in observations:
alphas_t = []
for next_tag in range(self.tagset_size):
obs_broadcast = dy.concatenate([dy.pick(obs, next_tag)] * self.tagset_size)
next_tag_expr = for_expr + self.transitions[next_tag] + obs_broadcast
alphas_t.append(log_sum_exp(next_tag_expr))
for_expr = dy.concatenate(alphas_t)
terminal_expr = for_expr + self.transitions[t2i["<STOP>"]]
alpha = log_sum_exp(terminal_expr)
return alpha
示例3: flatten_utterances
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def flatten_utterances(utterances):
""" Gets a flat sequence from a sequence of utterances.
Inputs:
utterances (list of list of str): Utterances to concatenate.
Returns:
list of str, representing the flattened sequence with separating
delimiter tokens.
"""
sequence = []
for i, utterance in enumerate(utterances):
sequence.extend(utterance)
if i < len(utterances) - 1:
sequence.append(DEL_TOK)
return sequence
示例4: _encode_with_discourse_lstm
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def _encode_with_discourse_lstm(self, utterances):
""" Encodes the utterances using a discourse-level LSTM, instead of concatenating.
Inputs:
utterances (list of list of str): Utterances.
"""
hidden_states = []
discourse_state, discourse_lstm_states = self._initialize_discourse_states()
final_state = None
for utterance in utterances:
final_state, utterance_states = self.utterance_encoder(
utterance,
lambda token: dy.concatenate([self.input_embedder(token), discourse_state]),
dropout_amount=self.dropout)
hidden_states.extend(utterance_states)
_, discourse_state, discourse_lstm_states = du.forward_one_multilayer(
final_state, discourse_lstm_states, self.dropout)
return final_state, hidden_states
示例5: score_snippets
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def score_snippets(snippets, scorer):
""" Scores snippets given a scorer.
Inputs:
snippets (list of Snippet): The snippets to score.
scorer (dy.Expression): Dynet vector against which to score the snippets.
Returns:
dy.Expression, list of str, where the first is the scores and the second
is the names of the snippets that were scored.
"""
snippet_expressions = [snippet.embedding for snippet in snippets]
all_snippet_embeddings = dy.concatenate(snippet_expressions, d=1)
if du.is_vector(scorer):
scorer = du.add_dim(scorer)
scores = dy.transpose(dy.transpose(scorer) * all_snippet_embeddings)
if scores.dim()[0][0] != len(snippets):
raise ValueError("Got " + str(scores.dim()[0][0]) + " scores for "
+ str(len(snippets)) + " snippets")
return scores, [snippet.name for snippet in snippets]
示例6: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def __call__(self,
prediction_input,
dropout_amount=0.):
decoder_state = prediction_input.decoder_state
input_hidden_states = prediction_input.input_hidden_states
attention_results = self.attention_module(decoder_state,
input_hidden_states)
state_and_attn = dy.concatenate(
[decoder_state, attention_results.vector])
intermediate_state = self._get_intermediate_state(
state_and_attn, dropout_amount=dropout_amount)
vocab_scores, vocab_tokens = self._score_vocabulary_tokens(
intermediate_state)
return TokenPrediction(vocab_scores, vocab_tokens, attention_results, decoder_state)
示例7: _score_anonymized_tokens
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def _score_anonymized_tokens(self,
input_sequence,
attention_scores):
scores = []
tokens = []
for i, token in enumerate(input_sequence):
if self.anonymizer.is_anon_tok(token):
scores.append(attention_scores[i])
tokens.append(token)
if len(scores) > 0:
if len(scores) != len(tokens):
raise ValueError("Got " + str(len(scores)) + " scores for "
+ str(len(tokens)) + " anonymized tokens")
return dy.concatenate(scores), tokens
else:
return None, []
示例8: forward
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def forward(self, observations):
def log_sum_exp(scores):
npval = scores.npvalue()
argmax_score = np.argmax(npval)
max_score_expr = dy.pick(scores, argmax_score)
max_score_expr_broadcast = dy.concatenate([max_score_expr] * self.dim_output)
return max_score_expr + dy.log(dy.sum_elems(dy.transpose(dy.exp(scores - max_score_expr_broadcast))))
init_alphas = [-1e10] * self.dim_output
init_alphas[self.sp_s] = 0
for_expr = dy.inputVector(init_alphas)
for obs in observations:
alphas_t = []
for next_tag in range(self.dim_output):
obs_broadcast = dy.concatenate([dy.pick(obs, next_tag)] * self.dim_output)
next_tag_expr = for_expr + self.trans[next_tag] + obs_broadcast
alphas_t.append(log_sum_exp(next_tag_expr))
for_expr = dy.concatenate(alphas_t)
terminal_expr = for_expr + self.trans[self.sp_e]
alpha = log_sum_exp(terminal_expr)
return alpha
示例9: _attend
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def _attend(self, input_vectors, decoder):
w1 = self.att_w1.expr(update=True)
w2 = self.att_w2.expr(update=True)
v = self.att_v.expr(update=True)
attention_weights = []
w2dt = w2 * decoder.s()[-1]
for input_vector in input_vectors:
attention_weight = v * dy.tanh(w1 * input_vector + w2dt)
attention_weights.append(attention_weight)
attention_weights = dy.softmax(dy.concatenate(attention_weights))
output_vectors = dy.esum(
[vector * attention_weight for vector, attention_weight in zip(input_vectors, attention_weights)])
return output_vectors, attention_weights
示例10: get_sequence_vector
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def get_sequence_vector(self,sequence,dropout):
"""
Pass a sequence of vectors through the BiLSTM. Return the sequence
vector.
@param: sequence is a list of vectors
dropout is a boolean
"""
if dropout:
self.surfaceBuilders[0].set_dropout(self.dropout_rate)
self.surfaceBuilders[1].set_dropout(self.dropout_rate)
else:
self.surfaceBuilders[0].set_dropout(0)
self.surfaceBuilders[1].set_dropout(0)
forward = self.surfaceBuilders[0].initial_state()
backward = self.surfaceBuilders[1].initial_state()
for ftoken, rtoken in zip(sequence, reversed(sequence)):
forward = forward.add_input( ftoken )
backward = backward.add_input( rtoken )
return dy.concatenate([forward.output(), backward.output()])
示例11: Init
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def Init(self,options):
paddingWordVec = self.word_lookup[1] if options.word_emb_size > 0 else None
paddingElmoVec = dy.zeros(self.elmo.emb_dim) if self.elmo else None
paddingPosVec = self.pos_lookup[1] if options.pos_emb_size > 0 else None
paddingCharVec = self.charPadding.expr() if options.char_emb_size > 0 else None
paddingTbankVec = self.treebank_lookup[0] if options.tbank_emb_size > 0 else None
self.paddingVec = dy.tanh(self.word2lstm.expr() *\
dy.concatenate(list(filter(None,[paddingWordVec,
paddingElmoVec,
paddingPosVec,
paddingCharVec,
paddingTbankVec]))) + self.word2lstmbias.expr())
self.empty = self.paddingVec if self.nnvecs == 1 else\
dy.concatenate([self.paddingVec for _ in range(self.nnvecs)])
示例12: word_repr
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [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
示例13: add_input
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def add_input(self, input_vec):
"""
Note that this function updates the existing State object!
"""
x = dynet.concatenate([input_vec, self.h])
i = dynet.logistic(self.W_i * x + self.b_i)
f = dynet.logistic(self.W_f * x + self.b_f)
g = dynet.tanh(self.W_c * x + self.b_c)
o = dynet.logistic(self.W_o * x + self.b_o)
c = dynet.cmult(f, self.c) + dynet.cmult(i, g)
h = dynet.cmult(o, dynet.tanh(c))
self.c = c
self.h = h
self.outputs.append(h)
return self
示例14: evaluate_struct
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def evaluate_struct(self, fwd_out, back_out, lefts, rights, test=False):
fwd_span_out = []
for left_index, right_index in zip(lefts, rights):
fwd_span_out.append(fwd_out[right_index] - fwd_out[left_index - 1])
fwd_span_vec = dynet.concatenate(fwd_span_out)
back_span_out = []
for left_index, right_index in zip(lefts, rights):
back_span_out.append(back_out[left_index] - back_out[right_index + 1])
back_span_vec = dynet.concatenate(back_span_out)
hidden_input = dynet.concatenate([fwd_span_vec, back_span_vec])
if self.droprate > 0 and not test:
hidden_input = dynet.dropout(hidden_input, self.droprate)
hidden_output = self.activation(self.W1_struct * hidden_input + self.b1_struct)
scores = (self.W2_struct * hidden_output + self.b2_struct)
return scores
示例15: evaluate_label
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import concatenate [as 别名]
def evaluate_label(self, fwd_out, back_out, lefts, rights, test=False):
fwd_span_out = []
for left_index, right_index in zip(lefts, rights):
fwd_span_out.append(fwd_out[right_index] - fwd_out[left_index - 1])
fwd_span_vec = dynet.concatenate(fwd_span_out)
back_span_out = []
for left_index, right_index in zip(lefts, rights):
back_span_out.append(back_out[left_index] - back_out[right_index + 1])
back_span_vec = dynet.concatenate(back_span_out)
hidden_input = dynet.concatenate([fwd_span_vec, back_span_vec])
if self.droprate > 0 and not test:
hidden_input = dynet.dropout(hidden_input, self.droprate)
hidden_output = self.activation(self.W1_label * hidden_input + self.b1_label)
scores = (self.W2_label * hidden_output + self.b2_label)
return scores