Skip to content

feat: Implement BatchWeightedSum and BatchWeightedMean#14

Merged
CyrilJl merged 4 commits into
mainfrom
add-weighted-stats
Aug 13, 2025
Merged

feat: Implement BatchWeightedSum and BatchWeightedMean#14
CyrilJl merged 4 commits into
mainfrom
add-weighted-stats

Conversation

@google-labs-jules

Copy link
Copy Markdown
Contributor

This change adds BatchWeightedSum and BatchWeightedMean classes to the batchstats library, allowing for batch-wise calculation of weighted sum and mean. The update_batch methods are updated to accept weights. The implementation includes tests and documentation.

google-labs-jules Bot and others added 3 commits August 13, 2025 14:29
… and merge support

This commit introduces `BatchWeightedSum` and `BatchWeightedMean` classes to compute weighted statistics over batches of data.

This final version incorporates several rounds of feedback to improve robustness and design:

- **Core Implementation**: Added `BatchWeightedSum` and `BatchWeightedMean` classes.
- **Axis Support**: The classes now support summation over any axis, including non-batch axes, by correctly handling batch-wise results.
- **Weight Broadcasting**: The `update_batch` method accepts weights that can be broadcast to the data's shape.
- **Design Refactoring**: `BatchWeightedMean` is refactored to use a `BatchWeightedSum` instance for its `sum_of_weights` calculation, improving code reuse and simplifying the design.
- **Robustness**: Added a consistency check to prevent mixing different weight shapes. Division-by-zero in `BatchWeightedMean` is handled gracefully.
- **Testing**: The test suite is comprehensive, using 3D data and parameterizing for various axes, weight shapes, and testing the merge (`+`) operation.
- **Documentation**: All new classes are included in the documentation.
@CyrilJl CyrilJl marked this pull request as ready for review August 13, 2025 14:54
@CyrilJl CyrilJl merged commit c023a2d into main Aug 13, 2025
1 check passed
@CyrilJl CyrilJl deleted the add-weighted-stats branch August 13, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants