Repository: tensorflow
Issue: TimeDistributed not compatible with multi-output models
Labels: stat:awaiting tensorflower,type:bug,comp:apis,TF 2.13

### 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'
```