Fix gradient-aggregation bias in GradientMode parameter updates#57
Merged
Conversation
Fix gradient-aggregation bias in GradientMode parameter updates
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #57 +/- ##
==========================================
- Coverage 67.45% 67.40% -0.06%
==========================================
Files 12 12
Lines 590 589 -1
==========================================
- Hits 398 397 -1
Misses 192 192 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GradientMode(andGradientMPIMode) computed a biased parameter gradient for any batch with more than one distinct sample.The optimization layer (JuMP + DiffOpt) returns
dCdy, the gradient of the assessment cost w.r.t. the forecasts. To update the network parameters we propagate these through the forecast model with a linear surrogate loss whose θ-gradient equals the chain-rule term. The bug:dCdywas aggregated across samples before building the surrogate, so the update computedmean_t(dCdy_t) · mean_t(∂ŷ_t/∂θ)instead of the correct empirical-cost gradient
(1/T) Σ_t dCdy_t · ∂ŷ_t/∂θThe two differ by the sample covariance between the per-sample cost gradient and the prediction Jacobian — zero only when all samples are identical or
T = 1. Every existing test usedT = 1, so the bug was latent.Changes
compute_cost: no longer aggregatesdC. Withwith_gradients=trueit returns the full per-sample gradient matrix of size(T, output_size). Theaggregateflag now affects only the cost.apply_gradient!: signature changed fromdCdy::AbstractVectortodCdy::AbstractMatrix(T, output_size); the surrogate loss now contracts each sample's gradient with its own forecast:loss = sum(dCdy' .* m(X')) / T.gradient_mpi.jl: the MPI path had the same aggregation; per-sample gradients are now stacked into a row-aligned matrix, and theg_tolstop rule /apply_gradient!call updated accordingly.GradientMode Multi-Sample Gradient Correctness, which checks the gradient the framework applies against a central finite-difference reference of the true cost (it fails on the old behavior: applied-2.0vs true-1.0). Updatedtest_predictive_model.jlcalls to the new matrix API.g_toldescription inmodes.md.0.1.6→0.1.7.Compatibility
compute_cost(...; with_gradients=true)now returns a matrix instead of an aggregated vector, andapply_gradient!takes a matrix. Released as a patch (0.1.7) given the limited public footprint.Testing
Full suite passes locally (Julia 1.11), including the new regression test.