python - 导入错误 : cannot import name TimeDistributedDense in Keras

标签 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

当我在谷歌上搜索时,我找到了这个解决方案 - 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我收到同样的错误。

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

最佳答案

正如 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

关于python - 导入错误 : cannot import name TimeDistributedDense in Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631235/

相关文章:

python - ValueError : Error when checking target: expected dense_3 to have shape (1, ) 但得到形状为 (6,) 的数组

php - 表格单元格/二维数组排序算法

python - 如何从用户那里获取输入以在字典中查找键并输出其值?

machine-learning - 卷积神经网络与下采样?

python - 在 slim 的tensorflow和tf记录批处理中微调inceptionv3的问题

Python Keras Tensorflow 嵌入层索引 [i,j] = k 不在 [0,max_features] 中

python - 如何在树莓派或任何其他 unix 系统上编写多命令 cronjob

python - 在python进程之间共享tkinter窗口对象

memory - Caffe:如何选择适合内存的最大可用批量大小?

python - 在目标检测中使用步长为 1 的最大池化层的目的是什么