MCPcopy Create free account
hub / github.com/google-research/language / beam_decoder

Function beam_decoder

language/labs/exemplar_decoding/models/hypernet.py:478–628  ·  view source on GitHub ↗

Beam search decoder. Args: features: Dictionary of input Tensors. mode: train or eval. Keys from tf.estimator.ModeKeys. vocab: A list of strings of words in the vocabulary. encoder_outputs: output tensors from the encoder hps: Hyperparams. Returns: Decoder outputs

(features, mode, vocab, encoder_outputs, hps)

Source from the content-addressed store, hash-verified

476
477
478def beam_decoder(features, mode, vocab, encoder_outputs, hps):
479 """Beam search decoder.
480
481 Args:
482 features: Dictionary of input Tensors.
483 mode: train or eval. Keys from tf.estimator.ModeKeys.
484 vocab: A list of strings of words in the vocabulary.
485 encoder_outputs: output tensors from the encoder
486 hps: Hyperparams.
487
488 Returns:
489 Decoder outputs
490 """
491 assert mode is not tf_estimator.ModeKeys.TRAIN, "Not using beam in training."
492 embeddings = encoder_outputs.embeddings
493 mem_input = encoder_outputs.mem_input
494 batch_size = tensor_utils.shape(features["src_len"], 0)
495
496 src_len, src_inputs = features["src_len"], features["src_inputs"]
497 src_mask = tf.sequence_mask(src_len, tf.shape(src_inputs)[1])
498
499 if hps.att_neighbor:
500 neighbor_len, neighbor_inputs \
501 = features["neighbor_len"], features["neighbor_inputs"]
502 neighbor_mask = tf.sequence_mask(
503 neighbor_len, tf.shape(neighbor_inputs)[1])
504 inputs = tf.concat([src_inputs, neighbor_inputs], 1)
505 # lens = src_len + neighbor_len
506 mask = tf.concat([src_mask, neighbor_mask], axis=1)
507 lens = tf.shape(mask)[1] * tf.ones([batch_size], tf.int32)
508 else:
509 inputs = features["src_inputs"]
510 lens = features["src_len"]
511 mask = src_mask
512
513 sparse_inputs = None
514 float_mask = tf.cast(mask, dtype=tf.float32)
515 if hps.use_copy:
516 sparse_inputs = sparse_map(
517 tf.expand_dims(inputs, axis=2),
518 tf.expand_dims(float_mask, axis=2),
519 vocab.size())
520 sparse_inputs = sparse_tile_batch(sparse_inputs, multiplier=hps.beam_width)
521
522 tiled_mask = tf.contrib.seq2seq.tile_batch(mask, multiplier=hps.beam_width)
523 inputs = tf.contrib.seq2seq.tile_batch(inputs, multiplier=hps.beam_width)
524 lens = tf.contrib.seq2seq.tile_batch(lens, multiplier=hps.beam_width)
525
526 def _beam_decode(cell):
527 """Beam decode."""
528 with tf.variable_scope("beam_decoder"):
529
530 initial_state = cell.zero_state(
531 batch_size=batch_size*hps.beam_width, dtype=tf.float32)
532 if hps.use_bridge:
533 h_state, c_state = encoder_outputs.states
534 h_state = tf.contrib.seq2seq.tile_batch(
535 h_state, multiplier=hps.beam_width)

Callers

nothing calls this directly

Calls 8

sparse_mapFunction · 0.90
sparse_tile_batchFunction · 0.90
HyperAttentionClass · 0.90
MyAttentionClass · 0.90
get_rnn_cellFunction · 0.90
OutputWrapperClass · 0.90
_beam_decodeFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…