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)
评论列表
文章目录