rand_one_hot#
- tensordict.nn.rand_one_hot(values: Tensor, do_softmax: bool = True) Tensor#
Draws a one-hot sample along the last dimension of
values.- Parameters:
- Returns:
A
torch.longtensor with the shape ofvalues, with one1per row of the last dimension.
Examples
>>> import torch >>> from tensordict.nn import rand_one_hot >>> _ = torch.manual_seed(0) >>> rand_one_hot(torch.tensor([0.0, 2.0, 1.0])) tensor([0, 1, 0]) >>> rand_one_hot(torch.tensor([[0.1, 0.9], [0.8, 0.2]]), do_softmax=False) tensor([[0, 1], [1, 0]])