Fix gradient-aggregation bias in GradientMode parameter updates#56
Merged
Conversation
… existing compat)
…-01-01-29-23-620-03873782497 CompatHelper: bump compat for ParametricOptInterface to 0.10, (keep existing compat)
…-12-01-17-24-142-02444460756 CompatHelper: add new compat entry for Statistics at version 1, (keep existing compat)
…-12-01-17-18-775-00042309767 CompatHelper: bump compat for Zygote to 0.7, (keep existing compat)
add aggregate arg on compute_cost funcion for allowing detailed cost …
apply best param on GradientMPIMode and improve epochs print
Version 0.1.4
Version 0.1.5
update documentation
add docs examples
Feature/improve in out map - version 0.1.6
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #56 +/- ##
==========================================
- 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.
Summary
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=trueitreturns 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 contractseach sample's gradient with its own forecast:
loss = sum(dCdy' .* m(X')) / T.gradient_mpi.jl: the MPI path had the same aggregation; per-samplegradients are now stacked into a row-aligned matrix, and the
g_tolstop rule/
apply_gradient!call updated accordingly.GradientMode Multi-Sample Gradient Correctness, which checksthe gradient the framework applies against a central finite-difference
reference of the true cost (it fails on the old behavior: applied
-2.0vstrue
-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. JuliaFormatter (v1) clean.