Sample *intervals* (rather than points) 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, t
(rand,
t,
w_logits,
num_samples,
single_jitter=False,
domain=(-torch.inf, torch.inf))
| 249 | |
| 250 | |
| 251 | def sample_intervals(rand, |
| 252 | t, |
| 253 | w_logits, |
| 254 | num_samples, |
| 255 | single_jitter=False, |
| 256 | domain=(-torch.inf, torch.inf)): |
| 257 | """Sample *intervals* (rather than points) from a step function. |
| 258 | |
| 259 | Args: |
| 260 | rand: random number generator (or None for `linspace` sampling). |
| 261 | t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) |
| 262 | w_logits: [..., num_bins], logits corresponding to bin weights |
| 263 | num_samples: int, the number of intervals to sample. |
| 264 | single_jitter: bool, if True, jitter every sample along each ray by the same |
| 265 | amount in the inverse CDF. Otherwise, jitter each sample independently. |
| 266 | domain: (minval, maxval), the range of valid values for `t`. |
| 267 | |
| 268 | Returns: |
| 269 | t_samples: [batch_size, num_samples]. |
| 270 | """ |
| 271 | if num_samples <= 1: |
| 272 | raise ValueError(f'num_samples must be > 1, is {num_samples}.') |
| 273 | |
| 274 | # Sample a set of points from the step function. |
| 275 | centers = sample( |
| 276 | rand, |
| 277 | t, |
| 278 | w_logits, |
| 279 | num_samples, |
| 280 | single_jitter, |
| 281 | deterministic_center=True) |
| 282 | |
| 283 | # The intervals we return will span the midpoints of each adjacent sample. |
| 284 | mid = (centers[..., 1:] + centers[..., :-1]) / 2 |
| 285 | |
| 286 | # Each first/last fencepost is the reflection of the first/last midpoint |
| 287 | # around the first/last sampled center. We clamp to the limits of the input |
| 288 | # domain, provided by the caller. |
| 289 | minval, maxval = domain |
| 290 | first = (2 * centers[..., :1] - mid[..., :1]).clamp_min(minval) |
| 291 | last = (2 * centers[..., -1:] - mid[..., -1:]).clamp_max(maxval) |
| 292 | |
| 293 | t_samples = torch.cat([first, mid, last], dim=-1) |
| 294 | return t_samples |
| 295 | |
| 296 | |
| 297 | def lossfun_distortion(t, w): |