Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ryan-wolbeck
Copy link
Collaborator

@ryan-wolbeck ryan-wolbeck commented Oct 7, 2025

  • Replace vectorized np.linalg.solve with sample-wise computation in scores.py
  • Add robust dimension checking and error handling
  • Handle singular matrix cases with pseudo-inverse fallback
  • Add comprehensive test suite for NumPy 2.x compatibility
  • Fixes GitHub issue Error while using Natural Gradient #384: dimension mismatch in natural gradient computation

The original issue occurred when np.linalg.solve received incompatible
dimensions between metric and gradient arrays. The new implementation:

  1. Validates dimensions before computation
  2. Solves linear systems sample-by-sample for better stability
  3. Provides clear error messages for dimension mismatches
  4. Handles singular matrices gracefully

All existing tests pass and new compatibility tests verify the fix.

closes #384

- Replace vectorized np.linalg.solve with sample-wise computation in scores.py
- Add robust dimension checking and error handling
- Handle singular matrix cases with pseudo-inverse fallback
- Add comprehensive test suite for NumPy 2.x compatibility
- Fixes GitHub issue #384: dimension mismatch in natural gradient computation

The original issue occurred when np.linalg.solve received incompatible
dimensions between metric and gradient arrays. The new implementation:
1. Validates dimensions before computation
2. Solves linear systems sample-by-sample for better stability
3. Provides clear error messages for dimension mismatches
4. Handles singular matrices gracefully

All existing tests pass and new compatibility tests verify the fix.
- Fix unbalanced tuple unpacking in make_regression call
- Replace broad Exception catching with specific ValueError and LinAlgError
- All linter checks now pass
- Remove trailing whitespace that was causing GitHub CI to fail
- All linter checks now pass on Python 3.11
…tisfy pylint unbalanced-tuple-unpacking across sklearn versions
@alejandroschuler
Copy link
Collaborator

Nice @ryan-wolbeck. Did you test speed? I'd worry that the sample-wise computation could slow things down a lot but I could be wrong.

@ryan-wolbeck
Copy link
Collaborator Author

Nice @ryan-wolbeck. Did you test speed? I'd worry that the sample-wise computation could slow things down a lot but I could be wrong.

That's a good point to test, looking at build times it looks like it goes from about 12 mins runtime for each version of python to about 22 mins.

Do you have ideas of a middle ground?

@alejandroschuler
Copy link
Collaborator

Hm... there are python libraries that precompile code that looks like this into C++ which runs very very quickly. Numba is a good example that is very easy to use.

Though honestly I'm not sure writing the code the way you did (sample-by-sample) vs the numpy version is more stable in the first place?

@ryan-wolbeck
Copy link
Collaborator Author

Hm... there are python libraries that precompile code that looks like this into C++ which runs very very quickly. Numba is a good example that is very easy to use.

Though honestly I'm not sure writing the code the way you did (sample-by-sample) vs the numpy version is more stable in the first place?

Updated the code to get past the speed issue. To your second point, I don't have a strong opinion.

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.

Error while using Natural Gradient

2 participants