带有自定义 RNN 单元的 keras 双向层

标签 keras

下面的模型

lstm_model = Sequential()
lstm_model.add(embedding)
tensor_lstm_cell = TensorLSTMCell(hidden_size=lstm_size, num_units=4)
lstm_model.add(Bidirectional(RNN(tensor_lstm_cell, return_sequences=True)))

抛出以下错误: ValueError: Unknown layer: TensorLSTMCell,它似乎来自从config双向加载它。我想知道如何使用 model.add 功能将自定义 rnn 层添加到双向包装器

最佳答案

您可以使用 CustomObjectScope 包裹 Bidirectional 行,以便它可以识别您的自定义对象 TensorLSTMCell。例如,

from keras.utils.generic_utils import CustomObjectScope

class DummyLSTMCell(LSTMCell):
    pass

embedding = Embedding(10000, 32, input_shape=(None,))

lstm_model = Sequential()
lstm_model.add(embedding)
lstm_cell = DummyLSTMCell(32)
with CustomObjectScope({'DummyLSTMCell': DummyLSTMCell}):
    lstm_model.add(Bidirectional(RNN(lstm_cell, return_sequences=True)))

关于带有自定义 RNN 单元的 keras 双向层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899040/

相关文章:

python - 属性错误 : 'NoneType' object has no attribute '_inbound_nodes' while trying to add multiple keras Dense layers

python - 不支持 None 值,Keras LSTM 适合

python - 导入 Keras 会中断多处理

python - 在 Keras 维度不匹配的情况下堆叠两个 LSTM 层

python - 如何获取 keras 模型的输出作为数值,而不是 Tensor 对象?

python - 不同样本上的多对多 lstm 模型

tensorflow - 如何在Keras模型中替换(或插入)中间层?

python - 函数式 API 中的 Keras Multiply() 层

python - tf.train.AdamOptimizer 和在 keras.compile 中使用 adam 有什么区别?

nlp - 如何使用 CNN (Keras) 处理文本分类的长度变化