2017-08-11 280 views
0

我正在尝试运行一个用于印地语到英语翻译的示例代码。ImportError:无法在Keras中导入名称TimeDistributedDense

当我运行当我搜索在谷歌,我发现这个解决方案提供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

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

如果有人发现任何解决方案,请帮助。

+1

什么Keras版本您使用的? TimeDistributedDense已在Keras 2.0.0中删除 –

+0

@MatiasValdenegro:'python -c“import keras; print keras .__ version__” 使用TensorFlow后端。 2.0.6 '我使用的这个版本 – user123

回答

2

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

你只有两个选择:

  • 修复代码,代之以一个TimeDistributed具有致密层结合使用TimeDistributedDense的。
  • 将Keras降级到适当的版本。作者没有提到他使用了哪个Keras版本,所以也许Keras 1.2.2会起作用。
0

作为Matias提到的,您需要一个老版本的Keras才能使用该函数。

但是,您可以在新版本中使用此代码,以及:

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 
相关问题