tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.46k stars 74.31k forks source link

Please initialize `TimeDistributed` layer with a `tf.keras.layers.Layer` instance #72953

Open mayurmunshi opened 3 months ago

mayurmunshi commented 3 months ago

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

2.15

Custom code

Yes

OS platform and distribution

Linux Ubuntu

Mobile device

No response

Python version

3.10

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

ValueError: Please initialize TimeDistributed layer with a tf.keras.layers.Layer instance. Received: KerasTensor(type_spec=TensorSpec(shape=(None, 3, 32, 32, 16, 256), dtype=tf.float32, name=None), name='activation_19/Relu:0', description="created by layer 'activation_19'")

Standalone code to reproduce the issue

Please initialize `TimeDistributed` layer with a `tf.keras.layers.Layer` instance

Relevant log output

alueError                                Traceback (most recent call last)
Cell In[103], line 5
      1 #Model parameters
      3 input_shape = (3,128,128,64,1)
----> 5 modelCNNLSTM310724 = ResNet50_3DCNNLSTM310724(input_shape )#= (128,128,64,1))
      7 modelCNNLSTM310724.summary()

Cell In[102], line 42, in ResNet50_3DCNNLSTM310724(input_shape)
     39 x = layers.BatchNormalization()(x)
     40 x = layers.Dropout(0.5)(x)
---> 42 x = tf.keras.layers.TimeDistributed(conv_block_3d(x, kernel_size=(3, 3, 3), filters=[64, 64, 256], strides=(1, 1, 1)))
     43 x = identity_block_3d(x, kernel_size=(3, 3, 3), filters=[64, 64, 256])
     44 x = identity_block_3d(x, kernel_size=(3, 3, 3), filters=[64, 64, 256])

File ~/.local/lib/python3.10/site-packages/keras/src/layers/rnn/time_distributed.py:74, in TimeDistributed.__init__(self, layer, **kwargs)
     72 def __init__(self, layer, **kwargs):
     73     if not isinstance(layer, Layer):
---> 74         raise ValueError(
     75             "Please initialize `TimeDistributed` layer with a "
     76             f"`tf.keras.layers.Layer` instance. Received: {layer}"
     77         )
     78     super().__init__(layer, **kwargs)
     79     self.supports_masking = True

ValueError: Please initialize `TimeDistributed` layer with a `tf.keras.layers.Layer` instance. Received: KerasTensor(type_spec=TensorSpec(shape=(None, 3, 32, 32, 16, 256), dtype=tf.float32, name=None), name='activation_19/Relu:0', description="created by layer 'activation_19'")
Venkat6871 commented 3 months ago

Hi @mayurmunshi ,

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

mayurmunshi commented 3 months ago

Hello,

The code snippet is as follows

inputs = Input(input_shape) custom_layer = ZeroPadding3D((3,3,1)) time_distributed_custom_layer = TimeDistributed(custom_layer) x = time_distributed_custom_layer(inputs)

On trying to execute this step at model.fit stage I am getting following error message :

epochs = 20 ----> 2 model.fit( 3 train_dataset, 4 validation_data=validation_dataset, 5 epochs=epochs, 6 shuffle=True,batch_size=16, 7 verbose=2, 8 callbacks=[checkpoint_cb,tensorboard_cb,early_stopping_cb] 9 )

File ~/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback..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 /tmp/autograph_generated_filecb7v_b6i.py:15, in outer_factory..inner_factory..tf__train_function(iterator) 13 try: 14 doreturn = True ---> 15 retval = ag.converted_call(ag__.ld(step_function), (ag.ld(self), ag.ld(iterator)), None, fscope) 16 except: 17 do_return = False

ValueError: in user code:

File "/home/user/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1401, in train_function  *
    return step_function(self, iterator)
File "/home/user/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1384, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/home/user/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1373, in run_step  **
    outputs = model.train_step(data)
File "/home/user/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 1150, in train_step
    y_pred = self(x, training=True)
File "/home/user/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None

ValueError: Exception encountered when calling layer 'time_distributed_21' (type TimeDistributed).

as_list() is not defined on an unknown TensorShape.

Call arguments received by layer 'time_distributed_21' (type TimeDistributed):
  • inputs=tf.Tensor(shape=<unknown>, dtype=float32)
  • training=True
  • mask=None