【问题标题】:Implementing seq2seq with sampled decoder outputs使用采样解码器输出实现 seq2seq
【发布时间】:2016-03-25 21:59:22
【问题描述】:

我正在尝试使用 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 模型中使用这个循环函数生成器。但是,我需要从浮点张量中采样的操作在张量流中不存在,所以我需要自己编写吗?我该怎么做?

  1. 在寻找指导的过程中,我发现在tensorflow/tensorflow/python/ops/candidate_sampling_ops中有一个参考:

            from tensorflow.python.ops import gen_candidate_sampling_ops
    

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

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    目前,您也可以使用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
    

    【讨论】:

    • 是的,我也让它在 seq2seq 中工作,忘记了你还需要将你选择的令牌传播回 output_feed。该函数在选择中引入了随机性——但我仍然不相信它正确地实现了 Gumbel 技巧。 Gumbel 噪声不应该直接应用于“a”吗?不应该是 -log(-log(Uniform(0,1)) 吗?
    • 我已经用我最终使用的代码更新了我的答案。在我看来,这是正确的 Gumbel max 技巧。它还可以产生更好的结果。您可以使用 max_gumbel_noise 参数来控制引入的随机量(类似于温度)。
    【解决方案2】:

    我今天遇到同样的问题,我的解决方法是:
    换行 prev_symbol = math_ops.sample(prev) prev_symbol = squeeze(multinomial(prev, 1), axis=1)

    函数 tf.multinomial() 从多项分布中抽取样本。它采用形状为 [batch_size, num_classes] 的二维张量“logits”和一个 0-D 标量“num_samples”作为输入。并输出一个形状为 [batch_size, num_samples] 的绘制样本。

    同时,math_ops.sample() 输出形状为 [batch_size] 的样本,因此我们需要 tf.squeeze() 来降维。

    这个实现更简单。

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 2017-10-26
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多