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

TImeDistributed + Add Layers Error #20093

Open
marta-q opened this issue Aug 7, 2024 · 8 comments
Open

TImeDistributed + Add Layers Error #20093

marta-q opened this issue Aug 7, 2024 · 8 comments
Assignees
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@marta-q
Copy link

marta-q commented Aug 7, 2024

Using TensorFlow==2.17.0 , Keras==3.4.1 I am having issues when trying to use the Add Layer together with the TImeDistributed one:

X = TimeDistributed(Add(), name='add_residual_convolution_' + str(it))([X, X_residual])

ValueError: `TimeDistributed` Layer should be passed an `input_shape` with at least 3 dimensions, received: [(None, 12, 0, 2), (None, 12, 0, 2)]

I have also tried passing the input_shape=X.shape argument to TimeDistributed, but the same error appears.

How can I solve this?

@ghsanti
Copy link
Contributor

ghsanti commented Aug 8, 2024

The time distributed layer expects 4 dimensions (frames, width, height, channels), here there are two inputs with 3 dimensions, which is what the error tells.

Do you really need the time distributed layer here? If you are just trying to add the tensors, Add is all you need.

@marta-q
Copy link
Author

marta-q commented Aug 8, 2024

Hmm they have 4 dimensions each: (None, 12, 0, 2)
This was just an example with wrong dimensions, the third dimension isn't really zero and the error still happens with the actual values.
I was just trying to reproduce the code from someone else, this supposedly worked with keras and tensorflow 2.11.0.

@ghsanti
Copy link
Contributor

ghsanti commented Aug 8, 2024

None is potentially the batch dimension, not a dimension from a single input sample; each input-sample has to be 4 dimensions, with the batch it's 5.

@marta-q
Copy link
Author

marta-q commented Aug 8, 2024

When I pass only X (same exact dimensions) with a different layer type, that works, so the issue here is not the dimension of the individual tensors, but that TimeDistributed does not have a proper implementation for using the Add() layer.
So it's necessary to either implement an error saying Add() layer is not accepted by TimeDistributed or fix TimeDistributed so that it accepts a list of tensors when Add() layer is used.

I was just trying to see if anyone could give me a workaround for this...

@ghsanti
Copy link
Contributor

ghsanti commented Aug 8, 2024

You are right, actually TimeDistributed accepts 3D, according to the docs:

Every input should be at least 3D

@marta-q
Copy link
Author

marta-q commented Aug 8, 2024

So far I've changed it to this:

X = Add()([X, X_residual])
X = TimeDistributed(Dense(2), name='add_residual_convolution_' + str(it))(X)

But I don't think this is achieving exactly the same 🤔
You said in the beginning that using only Add() could work the same, but I'm not sure I get how, could you try to explain?

@ghsanti
Copy link
Contributor

ghsanti commented Aug 8, 2024

You said in the beginning that using only Add() could work the same, but I'm not sure I get how, could you try to explain?

From what I understand (from the docs.) the layer TimeDistributed really is just to iterate an operation over an extra dimension. So in the case of Conv2D it allows you to iterate over video data (where you imagine that extra dimension in the input as time.)

In this case it seems to me that the layer is not necessary, and you just need Add()([a,b]) (but I'm just another user, and haven't really had to deal with that layer before.)

Maybe @sachinprasadhs has better advice.

@marta-q
Copy link
Author

marta-q commented Aug 8, 2024

I see, thanks for your help!

@mehtamansi29 mehtamansi29 added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

4 participants