def sample_and_embed(embedding, temperature, output_list=None,
output_projection=None):
"""Returns a callable (usable as a loop_fn for seq2seq) which takes a
sample from a batch of outputs and embeds them. Optionally applies a
projection first.
Args:
embedding: an embedding matrix to lookup symbols in.
temperature: temperature to control the pointiness of the softmax.
output_list (Optional): a list in which to collect the samples.
Default None means don't collect them at all.
output_proj (Optional): tuple (weight, biases) used to project outputs.
If None (default), no projection is performed.
Returns:
embedding from embedding.
"""
def _sample_embed(prev, _):
var = _maybe_project(prev, output_projection)
var /= temperature
next_ = tf.multinomial(var, 1)
# get rid of the num_samples dimension
next_ = tf.squeeze(next_)
# maybe store it
if output_list is not None:
output_list.append(next_)
# look up the embedding
next_ = tf.nn.embedding_lookup(
embedding, next_)
return next_
return _sample_embed
评论列表
文章目录