user123
user123

Reputation: 5407

ImportError: cannot import name TimeDistributedDense in Keras

I am trying run one sample code for Hindi to English translation.

when I run the code provided https://github.com/karimkhanp/Seq2Seq

Using TensorFlow backend.
Traceback (most recent call last):
  File "seq2seq.py", line 5, in <module>
    from model import seq2seq
  File "/home/ubuntu/Documents/karim/Data/bse/phase3/deep_learning/Seq2Seq/seq2seq/model.py", line 5, in <module>
    from keras.layers.core import Activation, RepeatVector, TimeDistributedDense, Dropout, Dense
ImportError: cannot import name TimeDistributedDense

When I searched on google I found this solution - https://github.com/fchollet/keras/tree/b587aeee1c1be3633a56b945af3e7c2c303369ca

I tried with code Zip package available on https://github.com/fchollet/keras/tree/b587aeee1c1be3633a56b945af3e7c2c303369ca

Installed keras using sudo python setup.py install But still when I run the code provided https://github.com/karimkhanp/Seq2Seq I am getting same error.

Please help if someone found any solution.

Upvotes: 2

Views: 9640

Answers (2)

Vadim
Vadim

Reputation: 4529

as Matias mentioned, you need the old version of Keras in order to use the function.

However, you can use time_distributed_dense function with the new version as well.

def time_distributed_dense(x, w, b=None, dropout=None,
                           input_dim=None, output_dim=None, timesteps=None):
    '''Apply y.w + b for every temporal slice y of x.
    '''
    if not input_dim:
        # won't work with TensorFlow
        input_dim = K.shape(x)[2]
    if not timesteps:
        # won't work with TensorFlow
        timesteps = K.shape(x)[1]
    if not output_dim:
        # won't work with TensorFlow
        output_dim = K.shape(w)[1]

    if dropout:
        # apply the same dropout pattern at every timestep
        ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
        dropout_matrix = K.dropout(ones, dropout)
        expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
        x *= expanded_dropout_matrix

    # collapse time dimension and batch dimension together
    x = K.reshape(x, (-1, input_dim))

    x = K.dot(x, w)
    if b:
        x = x + b
    # reshape to 3D tensor
    x = K.reshape(x, (-1, timesteps, output_dim))
    return x

Upvotes: 9

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

TimeDistributedDense was removed in Keras 2.0.0, as this functionality can be easily implemented with a TimeDistributed and Dense layers separately.

You only have two options:

  • Fix the code and replace used of TimeDistributedDense with a TimeDistributed combined with a Dense layer.
  • Downgrade Keras to an appropriate version. The author doesn't mention which Keras version he used, so maybe Keras 1.2.2 will work.

Upvotes: 5

Related Questions