由于LSTM是一种层,而在Keras中每个层只能有一个输出(如果我说错了请纠正),因此您无法在不修改源代码的情况下同时获得两个输出。
最近我正在攻略Keras以实现一些高级结构,一些您可能不喜欢的想法确实奏效了。我的做法是覆盖Keras层,以便我们可以访问表示隐藏状态的张量。
首先,您可以查看keras/layers/recurrent.py
中的call()
函数以了解Keras如何工作:
def call(self, x, mask=None):
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.stateful:
initial_states = self.states
else:
initial_states = self.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.go_backwards,
mask=mask,
constants=constants,
unroll=self.unroll,
input_length=input_shape[1])
if self.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.states[i], states[i]))
if self.return_sequences:
return outputs
else:
return last_output
其次,我们需要覆盖我们的层(Layer),以下是一个简单的脚本:
import keras.backend as K
from keras.layers import Input, LSTM
class MyLSTM(LSTM):
def call(self, x, mask=None):
self.extra_output = states
if self.return_sequences:
I = Input(shape=(...))
lstm = MyLSTM(20)
output = lstm(I)
extra_output = lstm.extra_output
calculate_function = K.function(inputs=[I], outputs=extra_output+[output])
MyLSTM
。只需在调用model_from_json()
时添加custom_object={'MyLSTM':MyLSTM}
即可。这应该很简单。 - Van