fix: use y.shape[1] in get_n_cols for Keras 3 compatibility (v0.1.4) - #24
Merged
Merged
Conversation
y.get_shape().as_list()[1] is old TF1-era API. Under Keras 3, the tensor flowing through the nested CausalLoss -> OutcomeLoss call chain during graph-mode loss tracing is a KerasTensor, which has no get_shape() method at all, raising AttributeError and crashing training with the default (posterior-weighted) loss. Fixed by using y.shape[1] unconditionally, which works for np.ndarray, eager tf.Tensor, graph-traced tensors, and KerasTensor alike -- so the isinstance branch is no longer needed either. Added regression tests covering all four tensor types plus an integration test that fits the toy model with run_eagerly True and False. Verified against the pre-fix code that the KerasTensor case fails with the exact reported AttributeError. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
get_n_cols(pypsps/utils.py) used the old TF1-eray.get_shape().as_list()[1]for non-np.ndarrayinputs. Under Keras 3, the tensor flowing through the nestedCausalLoss->OutcomeLosscall chain during graph-mode loss tracing is aKerasTensor, which has noget_shape()method at all -- raisingAttributeErrorand crashing training with the default (posterior-weighted) loss.y.shape[1]unconditionally, which works uniformly fornp.ndarray, eagertf.Tensor, graph-traced tensors, andKerasTensor, so theisinstancebranch is no longer needed.pyproject.tomlto0.1.4and added aCHANGELOG.mdentry.Test plan
test_get_n_cols_np_array,test_get_n_cols_eager_tensor,test_get_n_cols_graph_mode_tensor,test_get_n_cols_keras_symbolic_tensorcovering each tensor typeget_n_colsneeds to handle.test_toy_model_fits_in_eager_and_graph_mode, which fitsbuild_toy_modelwithrun_eagerly=Trueandrun_eagerly=False.test_get_n_cols_keras_symbolic_tensorfails withAttributeError: 'KerasTensor' object has no attribute 'get_shape', confirming this reproduces the reported crash.pytest pypsps/tests/-> 89 passed.🤖 Generated with Claude Code