Implements the Gumbel-Max trick to sample from a one-hot distribution.
(logits, dim=0, max_only=False)
| 205 | |
| 206 | |
| 207 | def batched_sample_onehot(logits, dim=0, max_only=False): |
| 208 | """Implements the Gumbel-Max trick to sample from a one-hot distribution.""" |
| 209 | if max_only: |
| 210 | sampled_idx = torch.argmax(logits, dim=dim, keepdim=True) |
| 211 | else: |
| 212 | noise = torch.rand_like(logits) |
| 213 | sampled_idx = torch.argmax(logits - torch.log(-torch.log(noise)), dim=dim, keepdim=True) |
| 214 | out_onehot = torch.zeros_like(logits, dtype=torch.bool) |
| 215 | out_onehot.scatter_(dim=dim, index=sampled_idx, value=1) |
| 216 | return out_onehot |
| 217 | |
| 218 | |
| 219 | def topk_edge_mask_from_logits(scores, k, randomize=False): |
no test coverage detected