【问题标题】:ImportError: cannot import name TimeDistributedDense in KerasImportError:无法在 Keras 中导入名称 TimeDistributedDense
【发布时间】:2018-01-19 17:41:46
【问题描述】:

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

当我运行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

当我在谷歌上搜索时,我发现了这个解决方案 - https://github.com/fchollet/keras/tree/b587aeee1c1be3633a56b945af3e7c2c303369ca

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

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

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

【问题讨论】:

  • 您使用的是什么 Keras 版本?在 Keras 2.0.0 中删除了 TimeDistributedDense
  • @MatiasValdenegro: python -c "import keras; print keras.__version__" Using TensorFlow backend. 2.0.6 这个版本我正在使用

标签: python deep-learning keras


【解决方案1】:

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

不过,您也可以在新版本中使用time_distributed_dense 函数。

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

【讨论】:

    【解决方案2】:

    TimeDistributedDense 在 Keras 2.0.0 中被移除,因为这个功能可以很容易地分别使用 TimeDistributed 和 Dense 层来实现。

    你只有两个选择:

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

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 2018-08-13
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2014-10-10
      相关资源
      最近更新 更多