我正在使用训练有素的LSTM模型 . 我想在预测时提取每个门的输出,并且输入是单个序列 . 在 recurrent.py
(来自keras包)中,将通过以下方式给出:
i = self.recurrent_activation(x_i + K.dot(h_tm1_i,
self.recurrent_kernel_i))
f = self.recurrent_activation(x_f + K.dot(h_tm1_f,
self.recurrent_kernel_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1_c,
self.recurrent_kernel_c))
我无法提取 i
, c
, f
, o
矩阵,因为可以对权重和偏差进行提取:
for layer in model.layers:
if "lstm" in str(layer).lower():
kernel_i = K.get_value(layer.kernel_i)
kernel_f = K.get_value(layer.kernel_f)
....
EDITED
我尝试使用例如计算激活:x_i = K.dot(inputs_i,kernel_i)
但是,它会输出以下错误:'numpy.ndarray'对象没有属性'get_shape'
这是错误的,因为 inputs_i
和 kernel_i
都是numpy数组 . 正确的方法是:
x_i = np.dot(inputs_i, kernel_i)
然后添加偏见: x_i = x_i + bias_i
仍然在 recurrent.py
之后,下一步是获取输入门 i
的输出,例如:
i = self.recurrent_activation(x_i + K.dot(h_tm1_i,
self.recurrent_kernel_i))
需要先将 x_i
转换为张量,然后从图层对象中获取recurrent_activation:
x_i_tensor = tf.convert_to_tensor(x_i, np.float32)
i = layer_lstm.recurrent_activation(x_i_tensor + K.dot(h_tm1_i,
layer_lstm.recurrent_kernel_i))
So, now the question is how to access the hidden state h_tm1_i ?我很感激一些建议 . 谢谢