Computes the mean value of each segment in a tensor.
(src, dst_idx, dst_size)
| 127 | |
| 128 | |
| 129 | def segment_mean(src, dst_idx, dst_size): |
| 130 | """Computes the mean value of each segment in a tensor.""" |
| 131 | out = torch.zeros( |
| 132 | dst_size, |
| 133 | *src.shape[1:], |
| 134 | dtype=src.dtype, |
| 135 | device=src.device, |
| 136 | ).index_add_(0, dst_idx, src) |
| 137 | denom = ( |
| 138 | torch.zeros( |
| 139 | dst_size, |
| 140 | *src.shape[1:], |
| 141 | dtype=src.dtype, |
| 142 | device=src.device, |
| 143 | ).index_add_(0, dst_idx, torch.ones_like(src)) |
| 144 | + 1e-8 |
| 145 | ) |
| 146 | return out / denom |
| 147 | |
| 148 | |
| 149 | def segment_argmin(scores, dst_idx, dst_size, randomize: bool = False) -> torch.Tensor: |
no outgoing calls
no test coverage detected