我在Keras工作,我有一个LSTM,我指定了intial_state=h0
。现在,我希望h0
成为可训练的变量。我怎么能这样做?
A similar question was asked for TensorFlow,但我确信在Keras中执行此操作的正确方法并不意味着import keras.backend as K
和黑客Keras类。
目前,我的丑陋解决方案在于使用等于0的虚拟输入并学习初始状态作为输出(=层偏置,因为我给出虚拟输入= 0)的Dense层,其输入由虚拟输入给出:
dummy_inp = Input((1,), name='dummy_inp')
dummy_inp_zero = Lambda(lambda t: t*0)(dummy_inp) # to ensure that the input=0
layer_h0 = Dense(dim_lstm_state, bias_initializer='zeros')
lstm_network = LSTM(n_units, bias_initializer='zeros', return_sequence=True)
h0 = layer_h0(dummy_inp_zero)
这很有效,但真的很难看。有没有优雅的方法来做到这一点?
提前感谢您的帮助!
答案 0 :(得分:0)
可以在https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html
查找更多详细信息encoder_states = [state_h, state_c]
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
initial_state=encoder_states)