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.0.5 - MultiHeadAttention(... return_attention_scores=True) does not work #19303

Closed
lbortolotti opened this issue Mar 13, 2024 · 5 comments
Assignees

Comments

@lbortolotti
Copy link

lbortolotti commented Mar 13, 2024

With keras==3.0.5, the following snippet:

import keras
from keras import layers

mha = layers.MultiHeadAttention(num_heads=2, key_dim=2)

x = keras.Input(shape=(3, 5))
y = keras.Input(shape=(3, 5))

out_attn, out_attn_scores = mha(x, y, return_attention_scores=True)

Throws:

Traceback (most recent call last):
  File "mha_repro.py", line 10, in <module>
    out_attn, out_attn_scores = mha(x, y, return_attention_scores=True)
  File "venv\lib\site-packages\keras\src\backend\common\keras_tensor.py", line 121, in __iter__
    raise NotImplementedError(
NotImplementedError: Iterating over a symbolic KerasTensor is not supported.

Because return_attention_scores seems to have no effect, and the second output is not returned. The mha.call() method seems right, it's just that the __call__ doesn't seem to construct the output tuple correctly.

@fchollet
Copy link
Collaborator

Thanks for the report. The issue was specifically with calling MultiHeadAttention on a KerasTensor with return_attention_scores=True. Calling it on a concrete tensor worked.

I have fixed the issue at HEAD.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@sachinprasadhs
Copy link
Collaborator

Reference commit: dca1d8a

@Furkan-rgb
Copy link
Contributor

Guys, I am experiencing the exact same issue with the Attention layer (not mha).

@Furkan-rgb
Copy link
Contributor

Fixed it in this PR: #20684

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

4 participants