下面的模型
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/