keras icon indicating copy to clipboard operation
keras copied to clipboard

Input_length keyword parameter in the Embedding Layer of Keras 3

Open longsc2603 opened this issue 1 year ago • 6 comments

image

In this Keras 2 doc page, there is a keyword parameter named 'input_length', which is not the case for the Keras 3 doc page in the below image. image

However, the example shown below that function definition of Keras 3 also shows an argument 'input_length', maybe this is a problem. image

The link for the Keras 2 documentation page: https://keras.io/2.15/api/layers/core_layers/embedding/ and Keras 3 documentation page: https://keras.io/api/layers/core_layers/embedding/

Also, since the new Keras 3 Embedding class has no 'input_length' parameter, I am having a problem trying to use a Keras 2-trained model with Keras 3. Can anyone help? Thank you so much.

longsc2603 avatar Jan 24 '24 08:01 longsc2603

Hi @longsc2603 ,

In Keras2 implementation either you can provide sequence length to input_length parameter or you can pass input_shape as kwargs. If both provided input_shape will be considered by the API.

In Keras3 implementation the parameter input_length is removed as it is redundant as you can use input_shape as kwarg. Even if you won't provide input_shape the API can infer it from the input being passed to model.

Please refer the attached gist for demo.

Hope it helps. Thanks!

SuryanarayanaY avatar Jan 25 '24 05:01 SuryanarayanaY

I seem to have passed that problem, since you are already here, could you help me solve another problem related to keras 3 and tf 2? I got a part of the code below that is not working properly, the error is shown below image

I searched online and come to think that this maybe due to tensorflow graph as defined below image

The part of the code that is showing error: image

I don't know how exactly to overcome this, many suggestions were for Keras 2 and I am using Keras 3, can you help? Thank you

longsc2603 avatar Jan 25 '24 09:01 longsc2603

Hi @longsc2603 ,

If you are using TF1.x graph then I afraid we are not supporting it anymore. You need to migrate to TF2.x style coding which is compatible with Keras3. Thanks!

SuryanarayanaY avatar Jan 25 '24 15:01 SuryanarayanaY

@SuryanarayanaY, can you please elaborate on how I could do that effectively, I'm not very familiar with the Tensorflow coding style. If you could give a complete tutorial link, that is also okay. Thanks

longsc2603 avatar Jan 30 '24 02:01 longsc2603

Hi @longsc2603 ,

There are lot of guides for migrating from TF1.x to TF2.x. Please refer the guides here at migrate to Tf2.x.

I recommend to stick to keras2 only by setting the environment variable TF_USE_LEGACY_KERAS=1 as the migration is for TF2.x(i.e Keras2) and not sure compatibility with Keras3.

SuryanarayanaY avatar Jan 30 '24 05:01 SuryanarayanaY

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Feb 14 '24 01:02 github-actions[bot]

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

github-actions[bot] avatar Feb 28 '24 01:02 github-actions[bot]

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

google-ml-butler[bot] avatar Feb 28 '24 01:02 google-ml-butler[bot]

Hi @longsc2603 ,

In Keras2 implementation either you can provide sequence length to input_length parameter or you can pass input_shape as kwargs. If both provided input_shape will be considered by the API.

In Keras3 implementation the parameter input_length is removed as it is redundant as you can use input_shape as kwarg. Even if you won't provide input_shape the API can infer it from the input being passed to model.

Please refer the attached gist for demo.

Hope it helps. Thanks!

I think this works..

skeeperloyaltie avatar Apr 13 '24 20:04 skeeperloyaltie