Keras 中的 LSTM 实现是如何工作的
How does the LSTM implementation in Keras work
我正在为 class LSTMCell
(https://github.com/keras-team/keras/blob/master/keras/layers/recurrent.py)
查看 recurrent.py
中的代码
是否 class 计算单个时间步长的隐藏状态和进位状态?
我在哪里可以找到处理展开网络的代码,即从一个时间步到另一个时间步的代码?
我正在尝试为单个示例计算每个门在每个时间步长的输出。到目前为止,我可以从训练有素的网络中提取权重和偏差,并按照第 1828 行到 1858 行的代码计算激活值。特别是:
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))
o = self.recurrent_activation(x_o + K.dot(h_tm1_o,
self.recurrent_kernel_o))
我的输入有形状:input(seq_length, nb_dim)。所以为了正确计算每个门的输出,我应该做这样的事情:
for step in range(seq_length):
input_step = input[step, :]
x_i = np.dot(input_step, kernel_i) + bias_i
i = recurrent_activation(x_i + np.dot(h_tm1_i, recurrent_kernel_i)
<<< repeat for other gates >>>
<<<compute cell hidden state/carry state>>>
Where can I find the code that deals with the unwinded network, i.e that goes from timestep to timestep?
这个逻辑是由keras.backend.rnn
function (recurrent.py
完成的):
last_output, outputs, states = K.rnn(step,
inputs,
initial_state,
constants=constants,
go_backwards=self.go_backwards,
mask=mask,
unroll=self.unroll,
input_length=timesteps)
step
基本上就是一个细胞的调用...
def step(inputs, states):
return self.cell.call(inputs, states, **kwargs)
... 在 LSTM 单元的情况下计算 i
、f
、c
和 o
门,如您的问题所述,并评估从中输出和状态张量。
如果您使用的是 tensorflow 后端,您可以在 keras/backend/tensorflow_backend.py
.
中找到迭代输入序列的实际循环
我正在为 class LSTMCell
(https://github.com/keras-team/keras/blob/master/keras/layers/recurrent.py)
recurrent.py
中的代码
是否 class 计算单个时间步长的隐藏状态和进位状态?
我在哪里可以找到处理展开网络的代码,即从一个时间步到另一个时间步的代码?
我正在尝试为单个示例计算每个门在每个时间步长的输出。到目前为止,我可以从训练有素的网络中提取权重和偏差,并按照第 1828 行到 1858 行的代码计算激活值。特别是:
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))
o = self.recurrent_activation(x_o + K.dot(h_tm1_o,
self.recurrent_kernel_o))
我的输入有形状:input(seq_length, nb_dim)。所以为了正确计算每个门的输出,我应该做这样的事情:
for step in range(seq_length):
input_step = input[step, :]
x_i = np.dot(input_step, kernel_i) + bias_i
i = recurrent_activation(x_i + np.dot(h_tm1_i, recurrent_kernel_i)
<<< repeat for other gates >>>
<<<compute cell hidden state/carry state>>>
Where can I find the code that deals with the unwinded network, i.e that goes from timestep to timestep?
这个逻辑是由keras.backend.rnn
function (recurrent.py
完成的):
last_output, outputs, states = K.rnn(step,
inputs,
initial_state,
constants=constants,
go_backwards=self.go_backwards,
mask=mask,
unroll=self.unroll,
input_length=timesteps)
step
基本上就是一个细胞的调用...
def step(inputs, states):
return self.cell.call(inputs, states, **kwargs)
... 在 LSTM 单元的情况下计算 i
、f
、c
和 o
门,如您的问题所述,并评估从中输出和状态张量。
如果您使用的是 tensorflow 后端,您可以在 keras/backend/tensorflow_backend.py
.