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

Model build uses incorrect input shape, TF 2.16.1 and 2.17 with keras3 #20299

Closed
ml-inr opened this issue Sep 27, 2024 · 4 comments
Closed

Model build uses incorrect input shape, TF 2.16.1 and 2.17 with keras3 #20299

ml-inr opened this issue Sep 27, 2024 · 4 comments

Comments

@ml-inr
Copy link

ml-inr commented Sep 27, 2024

Suppose that you have a model that splits its input as follows:

x = intuts[:,:,:-1]
mask = intuts[:,:,-1:]

Let further x be used in a custom layer, y=layer(x). This layer should be build using the shape of x. However, since TF version 2.16.1, the layer fails to be build correctly (using keras3, not tf-keras). See this very simple example on google collab.

It was verified that using tf-keras resolves the issue. So that the bug is in keras3 or its compatibility with TF. See also the corresponding issue at TF.

@mehtamansi29
Copy link
Collaborator

Hi @ml-inr-

There is conflict for defining name(matrix and bias) in add_weight method. In keras3, add_weight method can accept name like this name=<name of layer>.

By changing the naming declaration in add_weight method will resolve the error.

self.matrix_layer = self.add_weight(name="matrix", shape=(num_fs, self.out_dim), initializer="glorot_uniform")   
self.bias = self.add_weight(name="bias", shape=(self.out_dim,), initializer="zeros")                       

Attached gist for your reference.

@ml-inr
Copy link
Author

ml-inr commented Sep 30, 2024

Hi @mehtamansi29 !
Using keyword name in the add_weight does resolve the issue, thanks! This follows Keras documentation on Layer class, my bad.
Shouldn't, however, my initial code yield an error that I'm not properly using the Layer class (non-keyword arguments)?

@mehtamansi29
Copy link
Collaborator

Shouldn't, however, my initial code yield an error that I'm not properly using the Layer class (non-keyword arguments)?

Yes. Your initial code yielding error because declaring name without using keyword. So while building the layer it tries to take "matrix" in self.matrix_layer = self.add_weight("matrix", shape=(num_fs, self.out_dim), initializer="glorot_uniform") as shape.

Copy link

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

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