MCPcopy Create free account
hub / github.com/BioinfoMachineLearning/FlowDock / batched_sample_onehot

Function batched_sample_onehot

flowdock/utils/model_utils.py:207–216  ·  view source on GitHub ↗

Implements the Gumbel-Max trick to sample from a one-hot distribution.

(logits, dim=0, max_only=False)

Source from the content-addressed store, hash-verified

205
206
207def 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
219def topk_edge_mask_from_logits(scores, k, randomize=False):

Callers 2

Calls 1

logMethod · 0.80

Tested by

no test coverage detected