seq2seq.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:PTTChatBot_DL2017 作者: thisray 项目源码 文件源码
def sequence_loss(targets,
                                    logits,
                                    weights,
                                    average_across_timesteps=True,
                                    average_across_batch=True,
                                    softmax_loss_function=None,
                                    name=None):
    """Weighted cross-entropy loss for a sequence of logits, batch-collapsed.

    Args:
        logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].
        targets: List of 1D batch-sized int32 Tensors of the same length as logits.
        weights: List of 1D batch-sized float-Tensors of the same length as logits.
        average_across_timesteps: If set, divide the returned cost by the total
            label weight.
        average_across_batch: If set, divide the returned cost by the batch size.
        softmax_loss_function: Function (labels-batch, inputs-batch) -> loss-batch
            to be used instead of the standard softmax (the default if this is None).
        name: Optional name for this operation, defaults to "sequence_loss".

    Returns:
        A scalar float Tensor: The average log-perplexity per symbol (weighted).

    Raises:
        ValueError: If len(logits) is different from len(targets) or len(weights).
    """
    with ops.name_scope(name, "sequence_loss", logits + targets + weights):
        cost = math_ops.reduce_sum(
                sequence_loss_by_example(
                        targets,
                        logits,
                        weights,
                        average_across_timesteps=average_across_timesteps,
                        softmax_loss_function=softmax_loss_function))
        if average_across_batch:
            batch_size = array_ops.shape(targets[0])[0]
            return cost / math_ops.cast(batch_size, cost.dtype)
        else:
            return cost
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号