gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:47:15 25 4
gpt4 key购买 nike

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

当我运行提供的代码时 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com