Skip to content
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

Keras 3 expand dims not fully backend-agnostic #19350

Closed
MathiesW opened this issue Mar 21, 2024 · 3 comments
Closed

Keras 3 expand dims not fully backend-agnostic #19350

MathiesW opened this issue Mar 21, 2024 · 3 comments

Comments

@MathiesW
Copy link

The function keras.ops.expand_dims(x, axis=(0, -1)) does not work with Tensorflow backend if more than one axis to expand along is defined. It raises InvalidArgumentError: 'dim' must be a tensor with a single value [Op:ExpandDims]. I guess it internally calls the function tensorflow.expand_dims() which only supports a single axis at a time. JAX backend works fine, Pytorch I did not try.

@james77777778
Copy link
Contributor

The newest version should support this functionality (in all backends), which was actually introduced by me 😃

You can refer to this colab

from keras import backend
from keras import ops

print(backend.backend())  # tensorflow
x = ops.ones([2, 3, 4, 5])
print(x.shape)  # (2, 3, 4, 5)
x = ops.expand_dims(x, [0, -1])
print(x.shape)  # (1, 2, 3, 4, 5, 1)

You can also find the test for this:
https://github.com/keras-team/keras/blob/master/keras/ops/numpy_test.py#L3332-L3361

@SuryanarayanaY
Copy link
Contributor

Hi @MathiesW ,

Referring to @james77777778 comment could you please confirm woth what Keras version you have tested? Please try with latest version and still problem persists let us know with repro code snippet. Thanks!

@MathiesW
Copy link
Author

Hi @SuryanarayanaY and @james77777778
I was using Keras 3.0.5. Just updated to 3.1.1 and now it works as intended. Thank you!

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

No branches or pull requests

3 participants