Skip to content

fix: make SqrtInit work with Keras 2 and 3 - #40

Open
kudala-bharani wants to merge 2 commits into
JesperDramsch:mainfrom
kudala-bharani:fix/sqrt-init-scalar
Open

kudala-bharani wants to merge 2 commits into
JesperDramsch:mainfrom
kudala-bharani:fix/sqrt-init-scalar

Conversation

@kudala-bharani

@kudala-bharani kudala-bharani commented Oct 12, 2026 •

Copy link
Copy Markdown

Summary

SqrtInit passes the Python integer 2 to the Keras backend's sqrt, which expects a tensor and raises before the initializer can return anything. Compute the scalar with NumPy instead, keeping the requested shape and dtype in K.constant.

The tests cover scalar/vector/matrix shapes, the default dtype and float16/32/64, tf.function, and use as a Dense layer initializer. The README no longer lists SqrtInit as broken. The separate complexnn.bn.sqrt_init function is unchanged.

Fixes #37.

Testing

Confirmed the new tests fail on the original code with AttributeError: 'int' object has no attribute 'dtype'.

On Python 3.11.15 with TensorFlow 2.21.0:

  • Keras 3.15.1: pytest -q — 84 passed, 1 xfailed, 416 subtests passed.
  • Keras 2 via tf-keras 2.21.0: TF_USE_LEGACY_KERAS=1 pytest -q — 83 passed, 2 skipped, 416 subtests passed.
  • ruff check . and ruff format --check --diff . — passed with the repository's pinned Ruff 0.17.0.
  • pre-commit run --all-files — all six checks passed.
  • uv build — wheel and source distribution built successfully (existing setuptools license-metadata deprecation warnings).

No dependency changes.

@JesperDramsch

Copy link
Copy Markdown
Owner

Thanks for the fix @kudala-bharani . Could you drop the Changelog for now? I don't intend maintaining a changelog for the time being. After that it's good to go form my side.

@kudala-bharani

Copy link
Copy Markdown
Author

Dropped the changelog. Both Keras test suites still pass. Thanks for reviewing!

mikeng07 pushed a commit to mikeng07/ai301-coursework that referenced this pull request Nov 2, 2026
Built pr-monitor + playbook.md this week. Its first real run on PR #43
found JesperDramsch/keras-complex#40 -- another contributor's PR for
the same bug, opened before my claim and already maintainer-approved.
Traced this to a real gap in issue-scout's unit-6 "Nobody is already on
it" grading (never queried linked PRs, only assignees/comments).
Disclosed the overlap on PR #43 and closed it rather than leave two
PRs competing for an already-decided outcome. Filled in Learnings and
Reflections, Implementation Notes, and the unit-8 Toolkit entry.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqrtInit raises AttributeError when called on Keras 2 and Keras 3

2 participants