torch.nn.functional.gumbel_softmax#
- torch.nn.functional.gumbel_softmax(logits, tau=1, hard=False, eps=1e-10, dim=-1)[源代码]#
从 Gumbel-Softmax 分布 (链接 1 链接 2) 中采样,并可选择性地离散化。
- 参数
- 返回
从 Gumbel-Softmax 分布中采样的张量,其形状与 logits 相同。如果
hard=True
,则返回的样本将是 one-hot 的,否则它们将是概率分布,在 dim 维度上求和为 1。- 返回类型
注意
此函数出于历史原因而保留,将来可能会从 nn.Functional 中移除。
注意
对于 hard 的主要技巧是执行 y_hard - y_soft.detach() + y_soft
这实现了两个目的:- 使输出值精确为 one-hot(因为我们添加然后减去 y_soft 值)- 使梯度等于 y_soft 的梯度(因为我们去除了所有其他梯度)
- 示例:
>>> logits = torch.randn(20, 32) >>> # Sample soft categorical using reparametrization trick: >>> F.gumbel_softmax(logits, tau=1, hard=False) >>> # Sample hard categorical using "Straight-through" trick: >>> F.gumbel_softmax(logits, tau=1, hard=True)