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

Raise ValueError when number of dims evaluate to zero #1198

Merged

Conversation

sampathweb
Copy link
Collaborator

When TransformerEncoder and TransformerDecoder is used in a Model with insufficient embedding_size for EmbeddingLayer, it can create divide by zero error.

For example -

model = Sequential(layers=[
    Embedding(50, embedding_size=2, input_length=10, name='embedding'),
    TransformerEncoder(intermediate_dim=64, num_heads=8),
    Flatten(),
    Dense(16, activation="relu"),
    Dropout(0.2),
    Dense(1, activation="linear")
])

Fails with divide by zero error since embedding_size of 2 // num_heads results in zero. This PR will provide a more meaning ful message on why this happens.

@sampathweb sampathweb requested a review from mattdangerw August 7, 2023 23:59
@jbischof
Copy link
Contributor

jbischof commented Aug 8, 2023

Thanks @sampathweb! Is there an issue for this?

@sampathweb
Copy link
Collaborator Author

Thanks @sampathweb! Is there an issue for this?

Yes, a user who tried to do this and this code snippet is from that user's question. I will DM the issue.

Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@mattdangerw
Copy link
Member

/gcbrun

@mattdangerw mattdangerw merged commit f75c9b6 into keras-team:master Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants