votes up 6

Please initialize `TimeDistributed` layer with a `tf.keras.layers.Layer` instance. You passed: (input)

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

""" ses:
    ValueError: If not initialized with a `tf.keras.layers.Layer` instance.
  """

  def __init__(self, layer, **kwargs):
    if not isinstance(layer, Layer):
      raise ValueError(
          'Please initialize `TimeDistributed` layer with a '
          '`tf.keras.layers.Layer` instance. You passed: {input}'.format(
              input=layer))
    super(TimeDistributed, self).__init__(layer, **kwargs)
    self.supports_masking = True

    # It is safe to use the fast, reshape-based approach with all of our
    # 
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 0 votes down

Summary:

This exception arises when creating an instance of TimeDistributed. The init function takes in a required Layer object. A check is performed to assert that the passed object is indeed a layer object. The exception occurs when calling TimeDistributed from keras.layers.wrappers. The best solution for getting it to work is to use tf.keras.layer.TimeDistributed. Using it will give the expected results without the errors.

Code to Reproduce the Error (Wrong):

from keras.layers.wrappers import TimeDistributed
import tensorflow as tf

conv_2d_layer = tf.keras.layers.Conv2D(64, (3, 3))
output = TimeDistributed(conv_2d_layer)

Error Message:

ValueError                                Traceback (most recent call last)
<ipython-input-59-1f812a7853d4> in <module>()
      3 
      4 conv_2d_layer = tf.keras.layers.Conv2D(64, (3, 3))
----> 5 output = TimeDistributed(conv_2d_layer)

/usr/local/lib/python3.7/dist-packages/keras/layers/wrappers.py in __init__(self, layer, **kwargs)
    121           'Please initialize `TimeDistributed` layer with a '
    122           '`tf.keras.layers.Layer` instance. You passed: {input}'.format(
--> 123               input=layer))
    124     super(TimeDistributed, self).__init__(layer, **kwargs)
    125     self.supports_masking = True

ValueError: Please initialize `TimeDistributed` layer with a `tf.keras.layers.Layer` instance. You passed: <tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fbe43dceb50>

Working Version (Right):

from keras.layers.wrappers import TimeDistributed
import tensorflow as tf

conv_2d_layer = tf.keras.layers.Conv2D(64, (3, 3))
outputs = tf.keras.layers.TimeDistributed(conv_2d_layer)
Jul 17, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix