Piecewise-Constant PDF sampling from a step function. Args: rand: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the numbe
(rand,
t,
w_logits,
num_samples,
single_jitter=False,
deterministic_center=False)
| 173 | |
| 174 | |
| 175 | def sample(rand, |
| 176 | t, |
| 177 | w_logits, |
| 178 | num_samples, |
| 179 | single_jitter=False, |
| 180 | deterministic_center=False): |
| 181 | """Piecewise-Constant PDF sampling from a step function. |
| 182 | |
| 183 | Args: |
| 184 | rand: random number generator (or None for `linspace` sampling). |
| 185 | t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) |
| 186 | w_logits: [..., num_bins], logits corresponding to bin weights |
| 187 | num_samples: int, the number of samples. |
| 188 | single_jitter: bool, if True, jitter every sample along each ray by the same |
| 189 | amount in the inverse CDF. Otherwise, jitter each sample independently. |
| 190 | deterministic_center: bool, if False, when `rand` is None return samples that |
| 191 | linspace the entire PDF. If True, skip the front and back of the linspace |
| 192 | so that the centers of each PDF interval are returned. |
| 193 | |
| 194 | Returns: |
| 195 | t_samples: [batch_size, num_samples]. |
| 196 | """ |
| 197 | eps = torch.finfo(t.dtype).eps |
| 198 | # eps = 1e-3 |
| 199 | |
| 200 | device = t.device |
| 201 | |
| 202 | # Draw uniform samples. |
| 203 | if not rand: |
| 204 | if deterministic_center: |
| 205 | pad = 1 / (2 * num_samples) |
| 206 | u = torch.linspace(pad, 1. - pad - eps, num_samples, device=device) |
| 207 | else: |
| 208 | u = torch.linspace(0, 1. - eps, num_samples, device=device) |
| 209 | u = torch.broadcast_to(u, t.shape[:-1] + (num_samples,)) |
| 210 | else: |
| 211 | # `u` is in [0, 1) --- it can be zero, but it can never be 1. |
| 212 | u_max = eps + (1 - eps) / num_samples |
| 213 | max_jitter = (1 - u_max) / (num_samples - 1) - eps |
| 214 | d = 1 if single_jitter else num_samples |
| 215 | u = torch.linspace(0, 1 - u_max, num_samples, device=device) + \ |
| 216 | torch.rand(t.shape[:-1] + (d,), device=device) * max_jitter |
| 217 | |
| 218 | return invert_cdf(u, t, w_logits) |
| 219 | |
| 220 | |
| 221 | def sample_np(rand, |
no test coverage detected