Draw N samples from multinomial :param N: number of samples :return: samples
(self, N)
| 47 | self.alias = self.alias.cuda() |
| 48 | |
| 49 | def draw(self, N): |
| 50 | """ |
| 51 | Draw N samples from multinomial |
| 52 | :param N: number of samples |
| 53 | :return: samples |
| 54 | """ |
| 55 | K = self.alias.size(0) |
| 56 | |
| 57 | kk = torch.zeros(N, dtype=torch.long, device=self.prob.device).random_(0, K) |
| 58 | prob = self.prob.index_select(0, kk) |
| 59 | alias = self.alias.index_select(0, kk) |
| 60 | # b is whether a random number is greater than q |
| 61 | b = torch.bernoulli(prob) |
| 62 | oq = kk.mul(b.long()) |
| 63 | oj = alias.mul((1-b).long()) |
| 64 | |
| 65 | return oq + oj |