rand.py 文件源码

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

项目:paysage 作者: drckf 项目源码 文件源码
def rand_softmax(phi: T.FloatTensor) -> T.FloatTensor:
    """
    Draw random 1-hot samples according to softmax probabilities.

    Given an effective field vector v,
    the softmax probabilities are p = exp(v) / sum(exp(v))

    A 1-hot vector x is sampled according to p.

    Args:
        phi (tensor (batch_size, num_units)): the effective field

    Returns:
        tensor (batch_size, num_units): random 1-hot samples
            from the softmax distribution.

    """
    max_index = matrix.shape(phi)[1]-1
    probs = nl.softmax(phi)
    cum_probs = torch.cumsum(probs, 1)
    ref_probs = rand((len(phi), 1))
    on_units = matrix.int_tensor(matrix.tsum(cum_probs < ref_probs, axis=1, keepdims=True))
    matrix.clip_inplace(on_units, a_min=0, a_max=max_index)
    return matrix.zeros_like(phi).scatter_(1, on_units, 1)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号