-
Notifications
You must be signed in to change notification settings - Fork 116
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
Returning non-tensors from keras.layer.Layer #516
Comments
Hey @DavidLandup0 can you post a full repro for this? I am trying to reproduce like this
And I'm not seeing any errors. |
That's odd. Here's a clean Google Colab with the exact same code: https://colab.research.google.com/drive/196JtnB1joCRNWyMbDIX4aXvf0_a7MhKI?usp=sharing |
Ahh interesting -- looks like it's working with the |
Thanks! It looks like there's a check that explicitly checks whether all returns are tensors and raises the error: keras-core/keras_core/ops/node.py Line 47 in f2c16f7
Without digging too much into it, it looks like a design choice, but I don't know if it was purposefully set 😅 |
Indeed -- I think this was intentional. Maybe @fchollet you can shed some light on why this is the case? In the meantime, we can pass the resulting shapes as symbolic tensors as long as we specify the output spec of our layer. With this definition of the layer, this is now working for me across all backends:
|
In all likelihood you don't actually need to do this and you can find an alternative solution. But if you really want to return shapes, then they must be tensors. The correct way to do this would be to use |
I think we can close this as WAI. my comment above shows how we can return constants from layers for use cases like this, but as a general rule this is an anti-pattern which should be avoided where possible. |
Thank you both for the replies! @fchollet @ianstenbit |
In Keras 2.0, it was allowed to return tensors and non-tensors from a
keras.layers.Layer
.Something like:
In the new API, this throws an error:
Attempted a naive fix:
But this seems to "unbuild" the layer:
Using
keras.ops.shape
falls back to the backend implementation ofshape()
. In this case, it's equivalent totf.shape()
and results in the same error.Is there a supported way to return non-tensors beside tensors from a
call()
within aLayer
? If not, how can tensors be returned in a multi-backend context?Thanks!
The text was updated successfully, but these errors were encountered: