GreedyEmbeddingHelper

    GreedyEmbeddingHelper是 DecodeHelper 的子类。作为解码helper,它使用 argmax 进行采样,并将采样结果送入embedding层,以此作为下一解码步的输入。

    代码示例

    initialize()

    GreedyEmbeddingHelper初始化,其使用构造函数中的 start_tokens 作为第一个解码步的输入,并给出每个序列是否结束的初始标识。这是 BasicDecoder 初始化的一部分。

    返回:(initial_inputs, initial_finished) 的二元组, initial_inputs 同构造函数中的 start_tokensinitial_finished 是一个bool类型、值为False的tensor,其形状和 start_tokens 相同。

    返回类型:tuple

    使用 根据 outputs 进行采样。

    参数:

    • time (Variable) - 调用者提供的形状为[1]的tensor,表示当前解码的时间步长。其数据类型为int64。
    • outputs (Variable) - tensor变量,通常其数据类型为float32或float64,形状为

      ,表示当前解码步预测产生的logit(未归一化的概率),和由 BasicDecoder.output_fn(BasicDecoder.cell.call()) 返回的 outputs 是同一内容。

    返回:数据类型为int64形状为

    GreedyEmbeddingHelper - 图4

    的tensor,表示采样得到的id。

    返回类型:Variable

    next_inputs(time, outputs, states, sample_ids)

    sample_ids 使用 embedding_fn ,以此作为下一解码步的输入;同时直接使用输入参数中的 states 作为下一解码步的状态;并通过判别 sample_ids 是否得到 end_token,依此产生每个序列是否结束的标识。

    • time (Variable) - 调用者提供的形状为[1]的tensor,表示当前解码的时间步长。其数据类型为int64。
    • outputs (Variable) - tensor变量,通常其数据类型为float32或float64,形状为

      ,表示当前解码步预测产生的logit(未归一化的概率),和由 返回的 outputs 是同一内容。

    • states (Variable) - 单个tensor变量或tensor变量组成的嵌套结构,和由 BasicDecoder.cell.call() 返回的 new_states 是同一内容。

    返回: (finished, next_inputs, next_states) 的三元组。 next_inputs, next_states 均是单个tensor变量或tensor变量组成的嵌套结构,tensor的形状是

    GreedyEmbeddingHelper - 图7

    next_states 和输入参数中的 states 相同; finished 是一个bool类型且形状为

    的tensor。