ImportError:无法在Keras中导入名称TimeDistributedDense

use*_*123 2 python deep-learning keras

我正在尝试为印地语到英语翻译运行一个示例代码。

当我运行提供的代码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
Run Code Online (Sandbox Code Playgroud)

当我在Google上搜索时,我找到了此解决方案-https://github.com/fchollet/keras/tree/b587aeee1c1be1363363a56b945af3e7c2c303369ca

我尝试使用https://github.com/fchollet/keras/tree/b587aeee1c1be3633a56b945af3e7c2c303369ca上的代码Zip包

使用安装了keras,sudo python setup.py install但是当我运行提供的代码https://github.com/karimkhanp/Seq2Seq时,仍然遇到相同的错误。

如果有人找到任何解决方案,请提供帮助。

Pad*_*ddy 6

如Matias所述,您需要旧版本的Keras才能使用该功能。

但是,您也可以time_distributed_dense在新版本中使用function。

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
Run Code Online (Sandbox Code Playgroud)


Mat*_*gro 5

在Keras 2.0.0中删除了TimeDistributedDense,因为可以分别使用TimeDistributed和Dense层轻松实现此功能。

您只有两种选择:

  • 修复代码,并将TimeDistributedDense替换为结合了Dense层的TimeDistributed。
  • 将Keras降级到适当的版本。作者没有提及他使用的Keras版本,因此也许Keras 1.2.2可以工作。