Skip to content

SpectralNormalization layer fails on multiple batch dimensions #21320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stefanradev93 opened this issue May 22, 2025 · 0 comments
Open

SpectralNormalization layer fails on multiple batch dimensions #21320

stefanradev93 opened this issue May 22, 2025 · 0 comments
Assignees
Labels

Comments

@stefanradev93
Copy link

🐞 Bug Description

There are many cases where Dense layers are applied across multiple batch dimensions. However, when wrapped in a SpectralNormalization layer, the layer only works for the batch shape seen during building. The error comes from the rigid definition of the layer's InputSpec.

✅ Expected Behavior

The dense layer wrapper in a SpectralNormalization layer should behave in the same way as a Dense layer (i.e., be applicable to varying batch shapes).

🔁 Steps to Reproduce

spectral_dense = keras.layers.SpectralNormalization(keras.layers.Dense(32))
dense = keras.layers.Dense(32)

x1 = keras.random.normal((2, 4))
# Second input will have an additional batch dim
x2 = keras.random.normal((2, 5, 4))

# Fine, builds layer
dense(x1)
# Properly performs tensordot with multiple batch dims
dense(x2)

# Fine, builds layer
spectral_dense(x1)
# Fails: ValueError: Input 0 of layer "spectral_normalization_1" is incompatible with the layer: expected ndim=2, found ndim=3.
spectral_dense(x2)

🧪 Environment

The error occurs on all backends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants