本文整理汇总了Python中attention_decoder.attention_decoder方法的典型用法代码示例。如果您正苦于以下问题:Python attention_decoder.attention_decoder方法的具体用法?Python attention_decoder.attention_decoder怎么用?Python attention_decoder.attention_decoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attention_decoder
的用法示例。
在下文中一共展示了attention_decoder.attention_decoder方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_decoder
# 需要导入模块: import attention_decoder [as 别名]
# 或者: from attention_decoder import attention_decoder [as 别名]
def _add_decoder(self, inputs):
"""Add attention decoder to the graph. In train or eval mode, you call this once to get output on ALL steps. In decode (beam search) mode, you call this once for EACH decoder step.
Args:
inputs: inputs to the decoder (word embeddings). A list of tensors shape (batch_size, emb_dim)
Returns:
outputs: List of tensors; the outputs of the decoder
out_state: The final state of the decoder
attn_dists: A list of tensors; the attention distributions
p_gens: A list of scalar tensors; the generation probabilities
coverage: A tensor, the current coverage vector
"""
hps = self._hps
cell = tf.contrib.rnn.LSTMCell(hps.hidden_dim, state_is_tuple=True, initializer=self.rand_unif_init)
prev_coverage = self.prev_coverage if hps.mode == "decode" and hps.coverage else None # In decode mode, we run attention_decoder one step at a time and so need to pass in the previous step's coverage vector each time
outputs, out_state, attn_dists, p_gens, coverage = attention_decoder(inputs, self._dec_in_state,
self._enc_states, self._enc_padding_mask,
cell, initial_state_attention=(
hps.mode == "decode"), pointer_gen=hps.pointer_gen, use_coverage=hps.coverage,
prev_coverage=prev_coverage)
return outputs, out_state, attn_dists, p_gens, coverage
示例2: _add_decoder
# 需要导入模块: import attention_decoder [as 别名]
# 或者: from attention_decoder import attention_decoder [as 别名]
def _add_decoder(self, inputs):
"""Add attention decoder to the graph. In train or eval mode, you call this once to get output on ALL steps. In decode (beam search) mode, you call this once for EACH decoder step.
Args:
inputs: inputs to the decoder (word embeddings). A list of tensors shape (batch_size, emb_dim)
Returns:
outputs: List of tensors; the outputs of the decoder
out_state: The final state of the decoder
attn_dists: A list of tensors; the attention distributions
p_gens: A list of tensors shape (batch_size, 1); the generation probabilities
coverage: A tensor, the current coverage vector
"""
hps = self._hps
cell = tf.contrib.rnn.LSTMCell(hps.hidden_dim, state_is_tuple=True, initializer=self.rand_unif_init)
prev_coverage = self.prev_coverage if hps.mode=="decode" and hps.coverage else None # In decode mode, we run attention_decoder one step at a time and so need to pass in the previous step's coverage vector each time
outputs, out_state, attn_dists, p_gens, coverage = attention_decoder(inputs, self._dec_in_state, self._enc_states, self._enc_padding_mask, cell, initial_state_attention=(hps.mode=="decode"), pointer_gen=hps.pointer_gen, use_coverage=hps.coverage, prev_coverage=prev_coverage)
return outputs, out_state, attn_dists, p_gens, coverage
示例3: _add_decoder
# 需要导入模块: import attention_decoder [as 别名]
# 或者: from attention_decoder import attention_decoder [as 别名]
def _add_decoder(self, emb_dec_inputs, embedding):
"""Add attention decoder to the graph. In train or eval mode, you call this once to get output on ALL steps. In decode (beam search) mode, you call this once for EACH decoder step.
Args:
emb_dec_inputs: inputs to the decoder (word embeddings). A list of tensors shape (batch_size, emb_dim)
embedding: embedding matrix (vocab_size, emb_dim)
Returns:
outputs: List of tensors; the outputs of the decoder
out_state: The final state of the decoder
attn_dists: A list of tensors; the attention distributions
p_gens: A list of tensors shape (batch_size, 1); the generation probabilities
coverage: A tensor, the current coverage vector
"""
hps = self._hps
cell = tf.contrib.rnn.LSTMCell(hps.dec_hidden_dim, state_is_tuple=True, initializer=self.rand_unif_init)
prev_coverage = self.prev_coverage if (hps.mode=="decode" and hps.coverage) else None # In decode mode, we run attention_decoder one step at a time and so need to pass in the previous step's coverage vector each time
prev_decoder_outputs = self.prev_decoder_outputs if (hps.intradecoder and hps.mode=="decode") else tf.stack([],axis=0)
prev_encoder_es = self.prev_encoder_es if (hps.use_temporal_attention and hps.mode=="decode") else tf.stack([],axis=0)
return attention_decoder(_hps=hps,
v_size=self._vocab.size(),
_max_art_oovs=self._max_art_oovs,
_enc_batch_extend_vocab=self._enc_batch_extend_vocab,
emb_dec_inputs=emb_dec_inputs,
target_batch=self._target_batch,
_dec_in_state=self._dec_in_state,
_enc_states=self._enc_states,
enc_padding_mask=self._enc_padding_mask,
dec_padding_mask=self._dec_padding_mask,
cell=cell,
embedding=embedding,
sampling_probability=self._sampling_probability if FLAGS.scheduled_sampling else 0,
alpha=self._alpha if FLAGS.E2EBackProp else 0,
unk_id=self._vocab.word2id(data.UNKNOWN_TOKEN),
initial_state_attention=(hps.mode=="decode"),
pointer_gen=hps.pointer_gen,
use_coverage=hps.coverage,
prev_coverage=prev_coverage,
prev_decoder_outputs=prev_decoder_outputs,
prev_encoder_es = prev_encoder_es)
示例4: _add_decoder
# 需要导入模块: import attention_decoder [as 别名]
# 或者: from attention_decoder import attention_decoder [as 别名]
def _add_decoder(self, emb_dec_inputs, embedding):
"""Add attention decoder to the graph. In train or eval mode, you call this once to get output on ALL steps. In decode (beam search) mode, you call this once for EACH decoder step.
Args:
emb_dec_inputs: inputs to the decoder (word embeddings). A list of tensors shape (batch_size, emb_dim)
embedding: embedding matrix (vocab_size, emb_dim)
Returns:
outputs: List of tensors; the outputs of the decoder
out_state: The final state of the decoder
attn_dists: A list of tensors; the attention distributions
p_gens: A list of tensors shape (batch_size, 1); the generation probabilities
coverage: A tensor, the current coverage vector
"""
hps = self._hps
cell = tf.contrib.rnn.LSTMCell(hps.dec_hidden_dim, state_is_tuple=True, initializer=self.rand_unif_init)
prev_coverage = self.prev_coverage if (hps.mode=="decode" and hps.coverage) else None # In decode mode, we run attention_decoder one step at a time and so need to pass in the previous step's coverage vector each time
prev_decoder_outputs = self.prev_decoder_outputs if (hps.intradecoder and hps.mode=="decode") else tf.stack([],axis=0)
prev_encoder_es = self.prev_encoder_es if (hps.use_temporal_attention and hps.mode=="decode") else tf.stack([],axis=0)
return attention_decoder(hps,
self._vocab.size(),
self._max_art_oovs,
self._enc_batch_extend_vocab,
emb_dec_inputs,
self._target_batch,
self._dec_in_state,
self._enc_states,
self._enc_padding_mask,
self._dec_padding_mask,
cell,
embedding,
self._sampling_probability if FLAGS.scheduled_sampling else 0,
self._alpha if FLAGS.E2EBackProp else 0,
self._vocab.word2id(data.UNKNOWN_TOKEN),
initial_state_attention=(hps.mode=="decode"),
pointer_gen=hps.pointer_gen,
use_coverage=hps.coverage,
prev_coverage=prev_coverage,
prev_decoder_outputs=prev_decoder_outputs,
prev_encoder_es = prev_encoder_es)
示例5: _add_decoder
# 需要导入模块: import attention_decoder [as 别名]
# 或者: from attention_decoder import attention_decoder [as 别名]
def _add_decoder(self, inputs):
"""Add attention decoder to the graph. In train or eval mode, you call this once to get output on ALL steps. In decode (beam search) mode, you call this once for EACH decoder step.
Args:
inputs: inputs to the decoder (word embeddings). A list of tensors shape (batch_size, emb_dim)
Returns:
outputs: List of tensors; the outputs of the decoder
out_state: The final state of the decoder
attn_dists: A list of tensors; the attention distributions
p_gens: A list of scalar tensors; the generation probabilities
coverage: A tensor, the current coverage vector
"""
hps = self._hps
if self._isrum in ['all', 'dec']:
cell = RUMCell(
self._hps.hidden_dim,
eta_=self._time_norm,
lambda_=self._lambda,
update_gate=self._update_gate,
use_layer_norm=self._layer_norm,
use_zoneout=self._zoneout
)
else:
cell = tf.contrib.rnn.LSTMCell(
hps.hidden_dim, state_is_tuple=True, initializer=self.rand_unif_init)
# In decode mode, we run attention_decoder one step at a time and so
# need to pass in the previous step's coverage vector each time
prev_coverage = self.prev_coverage if hps.mode == "decode" and hps.coverage else None
outputs, out_state, attn_dists, p_gens, coverage = attention_decoder(inputs, self._dec_in_state, self._enc_states, self._enc_padding_mask, cell, initial_state_attention=(
hps.mode == "decode"), pointer_gen=hps.pointer_gen, use_coverage=hps.coverage, prev_coverage=prev_coverage, isrum=self._isrum, lambda_=self._lambda)
if self._isgrad:
if self._isrum in ['none', 'enc']:
tf.summary.scalar('out_state_c', tf.norm(out_state.c))
tf.summary.scalar('out_state_h', tf.norm(out_state.h))
else:
tf.summary.scalar('out_state', tf.norm(out_state))
return outputs, out_state, attn_dists, p_gens, coverage