【问题标题】:keras bidirectional layer with custom RNN Cell带有自定义 RNN 单元的 keras 双向层
【发布时间】:2017-12-23 23:36:15
【问题描述】:

以下型号

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 层添加到双向包装器

【问题讨论】:

    标签: keras


    【解决方案1】:

    您可以使用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)))
    

    【讨论】:

    • 酷!这会在保存/加载模型时解决“pickle”或“serialize”问题吗?
    • 我认为酸洗是一个不同的问题。 Keras 模型包含无法腌制的对象。在这里使用 CustomObjectScope 可能无济于事。
    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2019-11-12
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多