r""" Sample from the given discrete probability distribution with replacement. The i-th bin is assumed to have mass pmf[i]. Args: pmf: [batch_size, *shape, n_samples, 1] where (pmf.sum(dim=-2) == 1).all() n_samples: number of samples Return: indices sampled
(pmf: torch.Tensor, n_samples: int)
| 28 | |
| 29 | |
| 30 | def sample_pmf(pmf: torch.Tensor, n_samples: int) -> torch.Tensor: |
| 31 | r""" |
| 32 | Sample from the given discrete probability distribution with replacement. |
| 33 | |
| 34 | The i-th bin is assumed to have mass pmf[i]. |
| 35 | |
| 36 | Args: |
| 37 | pmf: [batch_size, *shape, n_samples, 1] where (pmf.sum(dim=-2) == 1).all() |
| 38 | n_samples: number of samples |
| 39 | |
| 40 | Return: |
| 41 | indices sampled with replacement |
| 42 | """ |
| 43 | |
| 44 | *shape, support_size, last_dim = pmf.shape |
| 45 | assert last_dim == 1 |
| 46 | |
| 47 | cdf = torch.cumsum(pmf.view(-1, support_size), dim=1) |
| 48 | inds = torch.searchsorted(cdf, torch.rand(cdf.shape[0], n_samples, device=cdf.device)) |
| 49 | |
| 50 | return inds.view(*shape, n_samples, 1).clamp(0, support_size - 1) |
| 51 | |
| 52 | |
| 53 | def posenc_nerf(x: torch.Tensor, min_deg: int = 0, max_deg: int = 15) -> torch.Tensor: |