Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args:
(w)
| 106 | |
| 107 | |
| 108 | def integrate_weights(w): |
| 109 | """Compute the cumulative sum of w, assuming all weight vectors sum to 1. |
| 110 | |
| 111 | The output's size on the last dimension is one greater than that of the input, |
| 112 | because we're computing the integral corresponding to the endpoints of a step |
| 113 | function, not the integral of the interior/bin values. |
| 114 | |
| 115 | Args: |
| 116 | w: Tensor, which will be integrated along the last axis. This is assumed to |
| 117 | sum to 1 along the last axis, and this function will (silently) break if |
| 118 | that is not the case. |
| 119 | |
| 120 | Returns: |
| 121 | cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 |
| 122 | """ |
| 123 | cw = torch.cumsum(w[..., :-1], dim=-1).clamp_max(1) |
| 124 | shape = cw.shape[:-1] + (1,) |
| 125 | # Ensure that the CDF starts with exactly 0 and ends with exactly 1. |
| 126 | cw0 = torch.cat([torch.zeros(shape, device=cw.device), cw, |
| 127 | torch.ones(shape, device=cw.device)], dim=-1) |
| 128 | return cw0 |
| 129 | |
| 130 | |
| 131 | def integrate_weights_np(w): |
no outgoing calls
no test coverage detected