python - 使用采样解码器输出实现 seq2seq

标签 python tensorflow deep-learning

我正在尝试实现具有从解码器生成采样输出的功能的 seq2seq,即在每一步,而不是从以前的状态中获取输出 logits 的 argmax,它应该根据 logit 分布从它们中采样并使用作为下一步的输入。

四处寻找后,我发现 seq2seq.py 中的 loop_function 是一个很有前途的起点。看起来我必须编写一个如下所示的循环函数(从提取 argmax+embedding 的文件中的函数修改而来):

def _extract_sample_and_embed(embedding, output_projection=None,
                          update_embedding=True):
    def loop_function(prev, _):
        if output_projection is not None:
            prev = nn_ops.xw_plus_b(prev, output_projection[0], output_projection[1])
        prev_symbol = math_ops.sample(prev)  #<------- Need this op but it does not exist ?
        emb_prev = embedding_ops.embedding_lookup(embedding, prev_symbol)
        if not update_embedding:
           emb_prev = array_ops.stop_gradient(emb_prev)
        return emb_prev
    return loop_function

然后我在 seq2seq_embedding_with_attention 模型中使用这个循环函数生成器。但是,tensorflow 中不存在我需要的浮点张量样本操作,所以我需要自己编写吗?我该怎么做?

  1. 在寻找指导时,我发现在 tensorflow/tensorflow/python/ops/candidate_sampling_ops 中有一个引用:

            from tensorflow.python.ops import gen_candidate_sampling_ops
    

    但是我找不到这个文件。我猜它是从某个地方自动生成的。在哪里?

最佳答案

目前,您还可以使用 the gumbel max trick for directe distributions 执行以下操作:

def batch_gumbel_max_sample(a, max_gumbel_noise = 1.0):
    matrix_U = -1.0*tf.log(-1.0*tf.log(tf.random_uniform(tf.shape(a),
                            minval = 0.0, maxval = max_gumbel_noise)))
    return tf.argmax(tf.sub(a, matrix_U), dimension = 1)

目前在 Tensorflows 问题跟踪器上也有一个关于此的 discussion。我猜迟早会将多项式样本函数添加到 Tensorflow 中。 LeavesBreathe 还在该 Github 页面上发布了一个解决方法,我认为这并不完全正确:

def batch_sample_with_temperature(a, temperature=1.0):
'''this function is like sample_with_temperature except it can handle batch input a of [batch_size x logits] 
    this function takes logits input, and produces a specific number from the array. This is all done on the gpu
    because this function uses tensorflow
    As you increase the temperature, you will get more diversified output but with more errors (usually gramatical if you're 
        doing text)
args: 
    Logits -- this must be a 2d array [batch_size x logits]
    Temperature -- how much variance you want in output
returns:
    Selected number from distribution
'''

'''
Equation can be found here: https://en.wikipedia.org/wiki/Softmax_function (under reinforcement learning)
    Karpathy did it here as well: https://github.com/karpathy/char-rnn/blob/4297a9bf69726823d944ad971555e91204f12ca8/sample.lua'''
'''a is [batch_size x logits]'''
with tf.op_scope([a,temperature], "batch_sample_with_temperature"):

    exponent_raised = tf.exp(tf.div(a, temperature)) #start by reduction of temperature, and get rid of negative numbers with exponent
    matrix_X = tf.div(exponent_raised, tf.reduce_sum(exponent_raised, reduction_indices = 1)) #this will yield probabilities!
    matrix_U = tf.random_uniform(tf.shape(a), minval = 0, maxval = 1)
    final_number = tf.argmax(tf.sub(matrix_X, matrix_U), dimension = 1) #you want dimension = 1 because you are argmaxing across rows.

return final_number

关于python - 使用采样解码器输出实现 seq2seq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36228723/

相关文章:

python - 将元素添加到数组并将其分配给同一个对象

python - Django 1.11 order by field on related model duplicate results 解决方法

python - 停止 Pandas group by 中的处理循环

tensorflow - 如何在 Keras 中使 LSTM 的初始状态可训练?

machine-learning - tensorflow 中embedding_rnn_seq2seq模型中的output_projection参数是什么?

python - 南 : run a migration for a column that is both unique and not null

tensorflow - TF2.0 中 Keras 损失中 `sample_weight` 参数的奇怪形状要求

tensorflow - Tensorflow中的Tensor和Variable有什么区别

python - 属性错误 : module 'tensorflow.models.embedding.gen_word2vec' has no attribute 'skipgram_word2vec'

python - tensorflow:您的输入数据不足