TimeDistributed not compatible with multi-output models
@gutza1 Sorry for the late response!
As per this [PR](https://github.com/tensorflow/tensorflow/pull/40993) TimeDistributed is compatible with multi-output models. Please check the similar [issue](https://github.com/tensorflow/tensorflow/issues/40896). 
Could you please provide the simple code snippet which pinpoints the issue or error you are facing while execution.Thank you! After some further thought, I think the issue might be with how I am defining the outputs. My network is importing a pre-trained CNN and trying to extract the outputs of its intermediate layers for use in a U-Net like structure. Thus, I used the following code to build a new model derived from the old model:

`new_model = tf.keras.models.Model(inputs=cnn_input, outputs=[cnn_final_output,cnn_intermediate_output_1, etc...])`

In the issue you sent, the multiple outputs are returned functionally. Would I have to create a custom layer that replicates the example given in order to use TimeDistributed with multiple outputs? @gutza1 It would be helpful for us if you could provide the complete code snippet to replicate the issue reported?
Thank you! The code is split across two segments. The first is in the initialization of the custom Model subclass I use:


```
self.cnn_model = cnn_model
...
self.cnn_output_layers = []

for i in range(len(cnn_layers)):
self.cnn_output_models.append(cnn_model.get_layer(cnn_layers[i]).output)

self.cnn_model = Model(inputs=cnn_input, outputs=[self.cnn_model.output] + self.cnn_output_layers)

Then, the execution is in the custom call method:

def call(self, inputs, states=None, return_state=False, training=False):
input_images = inputs[0]
...
mask = input_timesteps[:, :, 0] > 0.0
...
cnn_output = tfl.TimeDistributed(self.cnn_model)(input_images, mask=mask)
``` I have returned to this issue. I am now willing to share my code, which has undergone significant refactoring.
```
import tensorflow as tf
from tensorflow.keras import layers as tfl
from tensorflow.keras.models import Model

class RNNCNNResidualModel(tf.keras.Model):
  def __init__(self, rnn_units, cnn_model, cnn_layers, unet_filters, inner_conv_size=3, inner_conv_stride=1, weight_decay=1e-4, leaky_alpha=0.2):
    super().__init__(self)

    self.rnn_units = rnn_units
    self.cnn_model = cnn_model
    self.cnn_layers = cnn_layers
    self.unet_filters = unet_filters
    self.inner_conv_size = inner_conv_size
    self.inner_conv_stride = inner_conv_stride
    self.weight_decay = weight_decay
    self.leaky_alpha = leaky_alpha
    
    cnn_input = self.cnn_model.input
    
    #assemble list of cnn output layers by name

    #self.cnn_output_models = []
    cnn_outputs = [self.cnn_model.output]

    for i in range(len(cnn_layers)):
      #new_model = Model(inputs=cnn_input, outputs=cnn_model.get_layer(self.cnn_layers[i]).output)
      #new_model.trainable = False
      #self.cnn_output_models.append(new_model)
      cnn_outputs.append(cnn_model.get_layer(self.cnn_layers[i]).output)
      
    self.cnn_model = Model(inputs=cnn_input, outputs=cnn_outputs)
    self.cnn_model.trainable = False

    self.lstm_cell = tfl.LSTM(self.rnn_units, input_shape = [None, 2], return_sequences=True, return_state=True)
    self.dense = tfl.Dense(1, activation="linear", kernel_initializer='he_normal')

    self.conv_lstm = tfl.ConvLSTM2D(input_shape = [None, 6, 6, self.unet_filters[i]],
                                   return_sequences=True, return_state=True,
                                   filters=self.rnn_units, 
                                   kernel_size = (self.inner_conv_size, self.inner_conv_size), 
                                   strides = (self.inner_conv_stride, self.inner_conv_stride),
                                   padding = "same",
                                   kernel_initializer="he_normal", 
                                   kernel_regularizer=tf.keras.regularizers.l2(self.weight_decay))
    self.conv_lstm.build([None, None, 6, 6, self.unet_filters[0]])

    self.decoder = self.init_decoder()

  def init_decoder(self):

    upsampling_output = self.cnn_model.output[1]
    for i in range(len(self.cnn_model.output)-2):
      cnn_output = self.cnn_model.output[i+2]
      upsampling_output = tf.concat([cnn_output, upsampling_output], axis=-1)
      new_output = tfl.Convolution2D(filters=32, 
                                   kernel_size = (self.inner_conv_size, self.inner_conv_size), 
                                   strides = (self.inner_conv_stride, self.inner_conv_stride),
                                   padding = "same",
                                   kernel_initializer="he_normal", 
                                   kernel_regularizer=tf.keras.regularizers.l2(self.weight_decay))(upsampling_output)
      new_output = tfl.BatchNormalization()(new_output)
      new_output = tfl.Activation(tfl.LeakyReLU(self.leaky_alpha))(new_output)
      upsampling_output = tf.concat([upsampling_output, new_output], axis=-1)
      upsampling_output = tfl.Conv2DTranspose(filters=self.unet_filters[i + 2], 
                                    kernel_size = (3, 3), 
                                    strides = (2, 2), 
                                    padding = "valid",
                                    kernel_initializer="he_normal", 
                                    kernel_regularizer=tf.keras.regularizers.l2(self.weight_decay))(upsampling_output)
    
    upsampling_output = tfl.Cropping2D(cropping=((5, 5), (5, 5)))(upsampling_output)

    decoder_model = Model(inputs=list(self.cnn_model.output[1:]),outputs=upsampling_output)
    return decoder_model

  def call(self, inputs, states=None, return_state=False, training=False):
    input_images = inputs[0]
    input_timesteps = inputs[1]
    mask = input_timesteps[:, :, 0] > 0.0

    cnn_outputs = tfl.TimeDistributed(self.cnn_model)(input_images, training=training, mask=mask)
    fgr_output = cnn_outputs[0]
    if states is None:
      states = [self.lstm_cell.get_initial_state(input_timesteps), self.conv_lstm.get_initial_state(cnn_outputs[0])]
    lstm_output = tfl.Multiply()([cnn_outputs[0], tf.expand_dims(tf.expand_dims(input_timesteps, axis=-1), axis=-1)])
    lstm_output, conv_cell_states, conv_carry = self.conv_lstm(lstm_output, 
                                                                            initial_state=states[1], 
                                                                            training=training, 
                                                                            mask=mask)
    states[1] = [conv_cell_states, conv_carry]
    
    upsampling_output = tfl.TimeDistributed(self.decoder)([lstm_output] + cnn_outputs[1:], training=training, mask=mask)

    image_output = tfl.Add()([input_images, upsampling_output])
    image_output = tfl.ReLU(max_value = 1.0)(image_output)

    lstm_input = tfl.Concatenate()([fgr_output, input_timesteps])
    lstm_output, cell_states, carry = self.lstm_cell(lstm_input, initial_state=states[0], training=training, mask=mask)
    time_dense = self.dense(lstm_output)
    fgr_output = tfl.Add()([fgr_output, time_dense])
    fgr_output = tfl.Activation(tfl.ReLU())(fgr_output)
    states[0] = [cell_states, carry]
    output = [image_output, fgr_output]
    #x = self.dense(x, training=training)

    if return_state:
      return output, states
    else:
      return output
```

In addition, I have performed a full stack trace of the error:

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[/Users/[REDACTED]/Peter](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]) DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb Cell 1 line 1
    [176](vscode-notebook-cell:/Users/[REDACTED]%20DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb#W0sZmlsZQ%3D%3D?line=175) cnn_model.trainable = False
    [177](vscode-notebook-cell:/Users/[REDACTED]%20DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb#W0sZmlsZQ%3D%3D?line=176) model = RNNCNNResidualModel(N_A, cnn_model, ["activation_239", "average_pooling2d_44", "average_pooling2d_19", "average_pooling2d_6", "conv2d"], [1980, 1452[/](https://file+.vscode-resource.vscode-cdn.net/)4, 660[/](https://file+.vscode-resource.vscode-cdn.net/)4, 264[/](https://file+.vscode-resource.vscode-cdn.net/)4, 66[/](https://file+.vscode-resource.vscode-cdn.net/)2, 1])
--> [179](vscode-notebook-cell:/Users/[REDACTED]%20DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb#W0sZmlsZQ%3D%3D?line=178) model.build([tf.TensorShape([None, None, 101, 101, 1]), tf.TensorShape([None, None, 1])])
    [181](vscode-notebook-cell:/Users/[REDACTED]%20DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb#W0sZmlsZQ%3D%3D?line=180) #Initialize inputs
    [183](vscode-notebook-cell:/Users/[REDACTED]%20DL/TRG_project_recurrent_cnn_residual_unet_instantaneous_train.ipynb#W0sZmlsZQ%3D%3D?line=182) plot_model(model, to_file='rnn_model_plot.png', show_shapes=True, show_layer_names=True)

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py:521](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py:521), in Model.build(self, input_shape)
    516     raise ValueError(
    517         "You can only call `build()` on a model if its "
    518         "`call()` method accepts an `inputs` argument."
    519     )
    520 try:
--> 521     self.call(x, **kwargs)
    522 except (tf.errors.InvalidArgumentError, TypeError) as e:
    523     raise ValueError(
    524         "You cannot build your model by calling `build` "
    525         "if your layers do not support float type inputs. "
   (...)
    529         f"`call` is: {e}."
    530     )

File [~/[REDACTED]](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/[REDACTED]) DL/rnn_cnn_residual_unet_model.py:88, in RNNCNNResidualModel.call(self, inputs, states, return_state, training)
     85 input_timesteps = inputs[1]
     86 mask = input_timesteps[:, :, 0] > 0.0
---> 88 cnn_outputs = tfl.TimeDistributed(self.cnn_model)(input_images, training=training, mask=mask)
     89 fgr_output = cnn_outputs[0]
     90 if states is None:

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:61](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:61), in filter_traceback.<locals>.error_handler(*args, **kwargs)
     59 def error_handler(*args, **kwargs):
     60     if not tf.debugging.is_traceback_filtering_enabled():
---> 61         return fn(*args, **kwargs)
     63     filtered_tb = None
     64     try:

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/base_layer.py:1155](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/base_layer.py:1155), in Layer.__call__(self, *args, **kwargs)
   1153     self._handle_activity_regularization(inputs, outputs)
   1154 if self._supports_masking:
-> 1155     self._set_mask_metadata(
   1156         inputs, outputs, input_masks, not eager
   1157     )
   1158 if self._saved_model_inputs_spec is None:
   1159     self._set_save_spec(inputs, args, kwargs)

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/base_layer.py:2896](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/base_layer.py:2896), in Layer._set_mask_metadata(self, inputs, outputs, previous_mask, build_graph)
   2893         self._set_mask_keras_history_checked(flat_outputs)
   2894     return
-> 2896 output_masks = self.compute_mask(inputs, previous_mask)
   2897 if output_masks is None:
   2898     return

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/layers/rnn/time_distributed.py:348](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/layers/rnn/time_distributed.py:348), in TimeDistributed.compute_mask(self, inputs, mask)
    346         input_length = tf.nest.flatten(input_length)[0]
    347     reshape_batch_size = batch_size if batch_size else -1
--> 348     output_mask_shape = self._get_shape_tuple(
    349         (reshape_batch_size, input_length), output_mask, 1
    350     )
    351     output_mask = backend.reshape(output_mask, output_mask_shape)
    352 return output_mask

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/layers/rnn/time_distributed.py:104](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/layers/rnn/time_distributed.py:104), in TimeDistributed._get_shape_tuple(self, init_tuple, tensor, start_idx)
     88 """Finds non-specific dimensions in the static shapes.
     89 
     90 The static shapes are replaced with the corresponding dynamic shapes of
   (...)
    101   corresponding dimension from `tf.shape(tensor)`.
    102 """
    103 # replace all None in int_shape by backend.shape
--> 104 int_shape = backend.int_shape(tensor)[start_idx:]
    105 if not any(s is None for s in int_shape):
    106     return init_tuple + int_shape

File [~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/backend.py:1534](https://file+.vscode-resource.vscode-cdn.net/Users/[REDACTED]%20DL/~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/backend.py:1534), in int_shape(x)
   1514 """Returns shape of tensor/variable as a tuple of int/None entries.
   1515 
   1516 Args:
   (...)
   1531 
   1532 """
   1533 try:
-> 1534     shape = x.shape
   1535     if not isinstance(shape, tuple):
   1536         shape = tuple(shape.as_list())

AttributeError: 'list' object has no attribute 'shape'
```

It appears the error originates in calculating the output mask for the TimeDistributed layer. @sachinprasadhs Any advice?
### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

tf 2.13

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

3.10.12

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I have been attempting to create a custom CNN-RNN hybrid model that incorporates a pretrained custom DenseNet-derived CNN. My model requires from the CNN both its final output and the intermediate outputs of its dense blocks (as I am attempting to create a U-Net like architecture). I attempted to create a new model from the given CNN that would output these intermediate convolutional layers along with the final output value. However, when I connected it to a TimeDistributed layer, I received a strange error with no clear explanation:

`AttributeError: 'list' object has no attribute 'shape'`

After some experimentation, I realized this error was arising because the TimeDistributed layer was not programmed to handle multi-output layers passed to it. I understand that this issue will likely require a fix to Tensorflow. In the meanwhile, any suggestions for workarounds would be appreciated.

### Standalone code to reproduce the issue

```shell
cnn_model = tf.keras.models.load_model(cnn_filepath)
cnn_final_output = cnn_model.layers[-1].output
cnn_intermediate_output = cnn_model.layers[-3].output
new_model = tf.keras.models.Model(inputs=cnn_input, outputs=[cnn_final_output,cnn_intermediate_output])

output = tfl.TimeDistributed(new_model)(input_images, mask=mask)
```


### Relevant log output

```shell
AttributeError                            Traceback (most recent call last)
/Users/[NAME REMOVED]/[NAME REMOVED] DL/TRG_project_recurrent_cnn_residual_instantaneous_train.ipynb Cell 1 line 1
    173 cnn_model.trainable = False
    174 model = RNNCNNResidualModel(N_A, cnn_model, ["average_pooling2d_6", "average_pooling2d_19", "average_pooling2d_44", "activation_239"])
--> 176 model.build([tf.TensorShape([None, None, 101, 101, 1]), tf.TensorShape([None, None, 1])])
    178 #Initialize inputs
    180 plot_model(model, to_file='rnn_model_plot.png', show_shapes=True, show_layer_names=True)

File ~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/engine/training.py:521, in Model.build(self, input_shape)
    516     raise ValueError(
    517         "You can only call `build()` on a model if its "
    518         "`call()` method accepts an `inputs` argument."
    519     )
    520 try:
--> 521     self.call(x, **kwargs)
    522 except (tf.errors.InvalidArgumentError, TypeError) as e:
    523     raise ValueError(
    524         "You cannot build your model by calling `build` "
    525         "if your layers do not support float type inputs. "
   (...)
    529         f"`call` is: {e}."
    530     )

File ~/[NAME REMOVED] DL/rnn_cnn_residual_model.py:41, in RNNCNNResidualModel.call(self, inputs, states, return_state, training)
     38 if states is None:
     39   states = self.lstm_cell.get_initial_state(input_timesteps)
---> 41 output = tfl.TimeDistributed(self.cnn_model)(input_images, mask=mask)
     42 fgr_output = output[0]
     43 lstm_input = tfl.Concatenate()([fgr_output, input_timesteps])

File ~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~/mambaforge/envs/tensorflow/lib/python3.10/site-packages/keras/src/backend.py:1534, in int_shape(x)
   1514 """Returns shape of tensor/variable as a tuple of int/None entries.
   1515 
   1516 Args:
   (...)
   1531 
   1532 """
   1533 try:
-> 1534     shape = x.shape
   1535     if not isinstance(shape, tuple):
   1536         shape = tuple(shape.as_list())

AttributeError: 'list' object has no attribute 'shape'
```
