【问题标题】:How to construct encoder from a loaded model in Keras?如何从 Keras 中加载的模型构建编码器?
【发布时间】:2019-07-23 12:55:29
【问题描述】:

我有一个编码器-解码器模型,其结构与 machinelearningmastery.comnum_encoder_tokens = 1949 的结构相同, num_decoder_tokens = 1944latent_dim = 2048

我想通过加载已经训练好的模型来构建编码器和解码器模型并尝试解码一些样本,但我收到错误"Graph disconnected: cannot obtain value for tensor Tensor("input_1_1:0", shape=(?,?, 1949), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

我的部分代码如下:

encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]

decoder_inputs = Input(shape=(None, num_decoder_tokens))
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                     initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs,
          validation_split=0.2)
model.save('modelname.h5')

# ...from here different python file for inference...

encoder = LSTM(latent_dim, return_state=True)
model = load_model('modelname.h5')
encoder_model = Model(model.output, encoder(model.output)) # I get the error here

我想在这里做的是:

encoder_inputs = Input(shape=(None, 1949))
encoder = LSTM(2048, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
encoder_model = Model(encoder_inputs, encoder_states)

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

  • 保存模型时是否会收到警告(如果我没记错的话是关于不可散列值等)?
  • 感谢您的评论。保存时没有问题,在声明encoder_model 进行推理时出现错误(我在代码中添加了注释)。
  • 您是否也声明了链接中提到的编码器/解码器模型?
  • 是的,但是我在编码器模型之后声明了解码器模型,所以我没有到达解码器模型,因为我在编码器声明时遇到了错误
  • 看来您声明编码器/解码器模型只是为了推理。你也应该宣布他们进行培训。你做到了吗?

标签: python tensorflow keras lstm sequence-to-sequence


【解决方案1】:

看看 Robert Sim 在堆栈溢出中对这篇帖子的回答:Restore keras seq2seq model

对于 github 上的这篇文章:https://github.com/keras-team/keras/pull/9119

他还在:https://github.com/simra/keras/blob/simra/s2srestore/examples/lstm_seq2seq_restore.py 中提供了一个示例,您可以在其中看到模型是如何加载的。以下代码取自该示例。

# Restore the model and construct the encoder and decoder.
model = load_model('s2s.h5')

encoder_inputs = model.input[0]   # input_1
encoder_outputs, state_h_enc, state_c_enc = model.layers[2].output   # lstm_1
encoder_states = [state_h_enc, state_c_enc]
encoder_model = Model(encoder_inputs, encoder_states)

decoder_inputs = model.input[1]   # input_2
decoder_state_input_h = Input(shape=(latent_dim,), name='input_3')
decoder_state_input_c = Input(shape=(latent_dim,), name='input_4')
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_lstm = model.layers[3]
decoder_outputs, state_h_dec, state_c_dec = decoder_lstm(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h_dec, state_c_dec]
decoder_dense = model.layers[4]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 2016-05-06
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多