本文整理汇总了Python中dynet.scalarInput方法的典型用法代码示例。如果您正苦于以下问题:Python dynet.scalarInput方法的具体用法?Python dynet.scalarInput怎么用?Python dynet.scalarInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dynet
的用法示例。
在下文中一共展示了dynet.scalarInput方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _compute_guided_attention
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def _compute_guided_attention(self, att_vect, decoder_step, input_size, output_size):
if output_size <= 1 or input_size <= 1:
return dy.scalarInput(0)
target_probs = []
t1 = float(decoder_step) / output_size
for encoder_step in range(input_size):
target_probs.append(1.0 - np.exp(-((float(encoder_step) / input_size - t1) ** 2) / 0.08))
# print target_probs
target_probs = dy.inputVector(target_probs)
# print (target_probs.npvalue().shape, att_vect.npvalue().shape)
return dy.transpose(target_probs) * att_vect
示例2: _make_input
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def _make_input(self, seq):
x_list = [self.phone_lookup[self.encodings.char2int['START']]]
for pi in seq:
if pi.char not in self.encodings.char2int:
print("Unknown input: '" + pi.char + "'")
else:
char_emb = self.phone_lookup[self.encodings.char2int[pi.char]]
context = []
for feature in pi.context:
if feature in self.encodings.context2int:
context.append(self.feature_lookup[self.encodings.context2int[feature]])
if len(context) == 0:
x_list.append(char_emb)
else:
x_list.append(char_emb + dy.esum(context) * dy.scalarInput(1.0 / len(context)))
x_list.append(self.phone_lookup[self.encodings.char2int['STOP']])
return x_list
示例3: score_sentence
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def score_sentence(self, observations, tags):
if len(tags) == 0:
tags = [t2i[NONE_TAG]] * len(observations)
assert len(observations) == len(tags)
score_seq = [0]
score = dy.scalarInput(0)
tags = [t2i[START_TAG]] + tags
for i, obs in enumerate(observations):
score = score + dy.pick(self.transitions[tags[i + 1]], tags[i]) + dy.pick(obs, tags[i + 1])
score_seq.append(score.value())
score = score + dy.pick(self.transitions[t2i[END_TAG]], tags[-1])
return score
示例4: viterbi_loss
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def viterbi_loss(self, sentence, gold_tags, use_margins=True):
observations = self.build_tagging_graph(sentence)
viterbi_tags, viterbi_score = self.viterbi_decoding(observations, gold_tags, use_margins)
if viterbi_tags != gold_tags:
gold_score = self.score_sentence(observations, gold_tags)
return (viterbi_score - gold_score), viterbi_tags
else:
return dy.scalarInput(0), viterbi_tags
示例5: score_sentence
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def score_sentence(self, observations, tags):
if not len(observations) == len(tags):
raise AssertionError("len(observations) != len(tags)")
score_seq = [0]
score = dy.scalarInput(0)
tags = [self.sp_s] + tags
for i, obs in enumerate(observations):
score = score + dy.pick(self.trans[tags[i+1]], tags[i]) + dy.pick(obs, tags[i+1])
score_seq.append(score.value())
score = score + dy.pick(self.trans[self.sp_e], tags[-1])
return score
示例6: learn
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def learn(self, characters, target_mgc, guided_att=True):
num_mgc = target_mgc.shape[0]
# print num_mgc
dy.renew_cg()
for pi in characters:
if pi.char not in self.encodings.char2int:
print("Unknown input: '" + pi.char + "' - skipping file")
return 0
style_probs = self.compute_gold_style_probs(target_mgc)
output_mgc, output_stop, output_attention = self._predict(characters, target_mgc, style_probs=style_probs)
losses = []
index = 0
for mgc, real_mgc in zip(output_mgc, target_mgc):
t_mgc = dy.inputVector(real_mgc)
# losses.append(self._compute_binary_divergence(mgc, t_mgc) )
losses.append(dy.l1_distance(mgc, t_mgc))
if index % 3 == 0:
# attention loss
if guided_att:
att = output_attention[index // 3]
losses.append(self._compute_guided_attention(att, index // 3, len(characters) + 2, num_mgc // 3))
# EOS loss
stop = output_stop[index // 3]
if index >= num_mgc - 6:
losses.append(dy.l1_distance(stop, dy.scalarInput(-0.8)))
else:
losses.append(dy.l1_distance(stop, dy.scalarInput(0.8)))
index += 1
loss = dy.esum(losses)
loss_val = loss.value() / num_mgc
loss.backward()
self.trainer.update()
return loss_val
示例7: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def __call__(self, x, y):
W = dy.parameter(self.W)
w_x = dy.parameter(self.w_x)
w_y = dy.parameter(self.w_y)
b = dy.parameter(self.b)
out = dy.transpose(x) * W * y
out += dy.dot_product(w_x, x)
out += dy.dot_product(w_y, y)
out = dy.concatenate([dy.scalarInput(0)] * (self.n_out - 1) + [out])
out += b
return out
示例8: __call__
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def __call__(self, a, b, c):
enc = [dy.rectify(self.a_mlp(a)), # HOTFIX rectify here?
dy.rectify(self.b_mlp(b)),
dy.rectify(self.c_mlp(c))]
enc = [dy.concatenate([dy.scalarInput(1), x]) for x in enc]
return self.multilinear(*enc)
示例9: synthesize
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [as 别名]
def synthesize(self, mgc, batch_size, sample=True, temperature=1.0):
synth = []
total_audio_len = mgc.shape[0] * len(self.upsample_w_s)
num_batches = total_audio_len / batch_size
if total_audio_len % batch_size != 0:
num_batches + 1
last_rnn_state = None
last_sample = 127
w_index = 0
last_proc = 0
for iBatch in range(num_batches):
dy.renew_cg()
# bias=dy.inputVector([0]*self.RNN_SIZE)
# gain=dy.inputVector([1.0]*self.RNN_SIZE)
start = batch_size * iBatch
stop = batch_size * (iBatch + 1)
if stop >= total_audio_len:
stop = total_audio_len - 1
upsampled = self._upsample(mgc, start, stop)
rnn = self.rnn.initial_state()
if last_rnn_state is not None:
rnn_state = [dy.inputVector(s) for s in last_rnn_state]
rnn = rnn.set_s(rnn_state)
out_list = []
for index in range(stop - start):
w_index += 1
curr_proc = w_index * 100 / total_audio_len
if curr_proc % 5 == 0 and curr_proc != last_proc:
last_proc = curr_proc
sys.stdout.write(' ' + str(curr_proc))
sys.stdout.flush()
if self.OUTPUT_EMB_SIZE != 1:
rnn_input = dy.concatenate([self.output_lookup[last_sample], upsampled[index]])
else:
rnn_input = dy.concatenate([dy.scalarInput(float(last_sample) / 127.0 - 1.0), upsampled[index]])
rnn = rnn.add_input(rnn_input)
rnn_output = rnn.output() # dy.layer_norm(rnn.output(), gain, bias)
hidden = rnn_output
for w, b in zip(self.mlp_w, self.mlp_b):
hidden = dy.tanh(w.expr(update=True) * hidden + b.expr(update=True))
softmax_output = dy.softmax(
self.softmax_w.expr(update=True) * hidden + self.softmax_b.expr(update=True))
out_list.append(softmax_output)
if sample:
last_sample = self._pick_sample(softmax_output.npvalue(),
temperature=temperature) # np.argmax(softmax_output.npvalue())
else:
last_sample = np.argmax(softmax_output.npvalue())
# last_sample = np.argmax(softmax_output.npvalue())
synth.append(last_sample)
rnn_state = rnn.s()
last_rnn_state = [s.value() for s in rnn_state]
return synth
示例10: greedy_search
# 需要导入模块: import dynet [as 别名]
# 或者: from dynet import scalarInput [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