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

Irregular Tensors as output from Generator class with batch size = 1 #19925

Closed
hansschaa opened this issue Jun 26, 2024 · 5 comments
Closed

Irregular Tensors as output from Generator class with batch size = 1 #19925

hansschaa opened this issue Jun 26, 2024 · 5 comments
Assignees
Labels

Comments

@hansschaa
Copy link

hansschaa commented Jun 26, 2024

Hello, I have a problem creating a generator and expecting it to work with tensors of different dimensions. I am using Keras 3.4.0 and I am getting the error message: TypeError: generator yielded an element of shape (1, 3, 3, 7) where an element of shape (None, 4, 4, 7) was expected.

You can try the following and you will obtain the difference error between the dimensions of the tensors for independent batches.

def __getitem__(self, index):

        height_X = np.random.randint(3, 6)
        width_X = np.random.randint(3, 6)   
        X = np.random.rand(1, height_X, width_X, 7)
        Y = np.array([0])

        return X, Y

Thanks for help!

@heydaari
Copy link

heydaari commented Jul 2, 2024

Hello there
because of the randomness you are adding to your code , the "generator" you are calling is simply returning this error, it expects a fixed input shape but you are giving it a random shape every time you call the getitem() method .

look at this example that i tested for you :

import numpy as np

def get_item(index):
    height_X = np.random.randint(3, 6)
    width_X = np.random.randint(3, 6)   
    X = np.random.rand(1, height_X, width_X, 7)
    Y = np.array([0])

    return X, Y

X, Y = get_item(0)
print(X.shape, Y.shape)
X, Y = get_item(1)
print(X.shape, Y.shape)
X, Y = get_item(2)
print(X.shape, Y.shape)

outputs are :

(1, 5, 5, 7) (1,)
(1, 3, 4, 7) (1,)
(1, 4, 5, 7) (1,)

@hansschaa
Copy link
Author

Hello there because of the randomness you are adding to your code , the "generator" you are calling is simply returning this error, it expects a fixed input shape but you are giving it a random shape every time you call the getitem() method .

look at this example that i tested for you :

import numpy as np

def get_item(index):
    height_X = np.random.randint(3, 6)
    width_X = np.random.randint(3, 6)   
    X = np.random.rand(1, height_X, width_X, 7)
    Y = np.array([0])

    return X, Y

X, Y = get_item(0)
print(X.shape, Y.shape)
X, Y = get_item(1)
print(X.shape, Y.shape)
X, Y = get_item(2)
print(X.shape, Y.shape)

outputs are :

(1, 5, 5, 7) (1,)
(1, 3, 4, 7) (1,)
(1, 4, 5, 7) (1,)

Hi, thanks for your response.

It happens that I need to give my network tensors of different dimensions. I have set the batch to 1 so that it accepts tensors of different dimensions in each call to getItem. Still it doesn't work.

@sachinprasadhs sachinprasadhs added type:Bug keras-team-review-pending Pending review by a Keras team member. labels Jul 10, 2024
@divyashreepathihalli
Copy link
Collaborator

@hansschaa this is a fundamental constraint in how deep learning models expect input data. The batch size can vary but the input tensor shape will have to be fixed.

@hertschuh
Copy link
Contributor

@hansschaa you can have a generator which yields tensors with different dimension values. There is however one constraint, which is that the first two batches returned must have different values for all the dimensions in question. That is how Keras detects the dynamic dimensions.

You can look at this test for an example: https://github.com/keras-team/keras/blob/master/keras/src/trainers/data_adapters/generator_data_adapter_test.py#L105

More context in #19748

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
Labels
Projects
None yet
Development

No branches or pull requests

5 participants