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

the dynamic has a result, but the static inference shape reports an error #20218

Open
Isa-Fay opened this issue Sep 6, 2024 · 2 comments · May be fixed by #20242
Open

the dynamic has a result, but the static inference shape reports an error #20218

Isa-Fay opened this issue Sep 6, 2024 · 2 comments · May be fixed by #20242
Assignees
Labels

Comments

@Isa-Fay
Copy link

Isa-Fay commented Sep 6, 2024

In some cases, the dynamic has a result, but the static inference shape reports an error.
Here are some samples:

version: keras -- 3.5.0; Jax – 0.4.31

import os
import re
import jax
import numpy as np
os.environ['KERAS_BACKEND']='jax'
import keras

layer = keras.layers.AveragePooling2D(
    pool_size=[ 2, 3 ],
    strides=1,
    padding="valid",
    data_format="channels_first",
    trainable=True,
    autocast=True,
)

result_static = layer.compute_output_shape([1, 4, 4, 1])

result_dynamic = layer(
    inputs=np.random.rand(*[1, 4, 4, 1]),
)

This is what I got for the static result:
1
and the dynamic result:
2

version: keras -- 3.5.0; Jax – 0.4.31

import os
import re
import jax
import numpy as np
os.environ['KERAS_BACKEND']='jax'
import keras

layer = keras.layers.CategoryEncoding(
    num_tokens=3,
    output_mode="one_hot",
    trainable=True,
    dtype="int32",
    autocast=True,
)

result_static = layer.compute_output_shape([1, 1])

result_dynamic = layer(
    inputs=np.random.rand(*[1, 1]),
)

3

The problem also occurs when backend is Torch.
3.
version: keras-- 3.5.0; Torch -- 2.4.0

import os
import re
import torch
import numpy as np
os.environ['KERAS_BACKEND']='torch'
import keras

layer = keras.layers.CategoryEncoding(
    num_tokens=3,
    output_mode="one_hot",
    trainable=True,
    dtype="int32",
    autocast=True,
)

result_static = layer.compute_output_shape([1, 1])

result_dynamic = layer(
    inputs=np.random.rand(*[1, 1]),
)

4

@mehtamansi29
Copy link
Collaborator

Hi @Isa-Fay -

Thanks for reporting the issue. Both issues are reproduce in all backend(jax,tensorflow and torch).

Issue1: Getting the error ValueError: Computed output size would be negative. Received: inputs.shape=[1 4 4 1]andpool_size=[2 3]``
Here AveragePooling2D layer `data_format="channels_first".`
So for input= [1,4,4,1] here, batch_size=1, channel= 4, height= 4, width=1

For padding='valid' : (W−F+2P)/S+1:
(1-2+2 x 0)/1+1= -1
(1-3+2 x 0)/1+1= -2

So changing padding='same' in AveragePooling2D layer will resolve the error.

Issue2: Getting the error: TypeError: can only concatenate list (not "tuple") to list
change layer.compute_output_shape([1, 1]) to layer.compute_output_shape((1, 1)) will resolve the error.

Attached gist for the reference.

@Isa-Fay
Copy link
Author

Isa-Fay commented Sep 9, 2024

Thanks for you reply!
The input is indeed wrong, but that's not my point. I thought it is unreasonable that the dynamic has a result, but the static inference shape reports an error. If the results are the same( both report error or both have a result ), it would be more user friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants