keras icon indicating copy to clipboard operation
keras copied to clipboard

TImeDistributed + Add Layers Error

Open marta-q opened this issue 1 year ago • 8 comments

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?

marta-q avatar Aug 07 '24 08:08 marta-q

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.

newresu avatar Aug 08 '24 06:08 newresu

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.

marta-q avatar Aug 08 '24 07:08 marta-q

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.

newresu avatar Aug 08 '24 09:08 newresu

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...

marta-q avatar Aug 08 '24 11:08 marta-q

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

Every input should be at least 3D

newresu avatar Aug 08 '24 11:08 newresu

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?

marta-q avatar Aug 08 '24 11:08 marta-q

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.

newresu avatar Aug 08 '24 11:08 newresu

I see, thanks for your help!

marta-q avatar Aug 08 '24 15:08 marta-q

Hi @marta-q, Instead of wrapping the Add layer with TimeDistributed, you can use the Add layer directly since it already supports temporal operations.

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

Please close the issue if your error is resolved. Thanks !

dhantule avatar Jan 02 '25 06:01 dhantule

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 Jan 18 '25 01:01 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 01 '25 02:02 github-actions[bot]

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

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