-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Discovered a magic bug in keras, which is caused by incoming data #20070
Comments
Especially with the last two inference functions, you can find that the behavior of Keras 3.4.1 is opposite to that of Keras 3.3.3. |
Hi @pass-lin- Thanks for reporting this issue. I am also reproducing this issue even though tokens are in range of vocab size. And embedding layer expects input_dim and inputs are expected to be in the range [0, input_dim), which is correct in the code. Attached gist for the reference. We look into this more and update you. |
this bug also be found at jax backend |
Hi @pass-lin , The issue is that you're supposed to pass the inputs the same way as you created the functional model. In your case, it appears that the inputs were passed as an array when creating the model, so you need to pass them as an array when calling the model. If you want to pass them as a dict, you need to create the model using a dict. For instance, given this: x1 = keras.Input((10,), name='Input-Token')
x2 = keras.Input((20,), name='Input-Segment')
y = keras.layers.Concatenate()([x1, x2]) You can use arrays: model = keras.Model(inputs=[x1, x2], outputs=y)
model([keras.ops.ones((1, 10)), keras.ops.zeros((1, 20))]) Or you can use dicts: model = keras.Model(inputs={'Input-Token':x1, 'Input-Segment':x2}, outputs=y)
model({'Input-Token':keras.ops.ones((1, 10)), 'Input-Segment':keras.ops.zeros((1, 20))}) But you cannot mix them. The real bug is that it's not complaining about the fact that the structure passed as input to The reason why it swaps them is that it's ordering the dict by order of the keys to get an array. The behavior did change between 3.3.3 and 3.4.1 as a result of adding support for optional inputs. |
So will this be fixed in future versions of Keras, or should I fix it in my local code? The code runs normally in Keras versions 2.3.1 to 3.3.3, but it does not run in Keras 3.4.1, which is a bit unbelievable. |
You will need to fix this by either:
What will be fixed in Keras is that it will raise a different error instead of taking incorrect inputs. |
However, from Keras 2.3.1 to Keras 3.3.3, this behavior is correct and can be executed normally. It's really hard to accept. Especially when I'm using tf.data to read tfrecord, I can only choose dict as the model input, but most of the time I use list to create the Model. |
@pass-lin , Can you check with nightly version whether attached PR above resolved resolved your query? |
Here is an example code, where bert4keras3 is my own llm library, which can be installed through pip
The reason for the failure can be discovered in the
call
function of thebert4keras.Layers.Add.Embedding
class. If you printinputs, self.name
, you will find that the layer where 'Input-Token' should be passed has received 'Input-Segment' instead. However, if you printself.name, inputs.name
during the execution of thebuild_transformer_model
function, you can see that they are matched correctly.Further, if we do not use a dictionary as input and instead pass a list in the order of
model.inputs
, the code will run smoothly at this point.Additionally, this bug only exists in Keras 3.4.1 and not in Keras 3.3.3.
The text was updated successfully, but these errors were encountered: