首页 文章

将Pytorch LSTM的状态参数转换为Keras LSTM

提问于
浏览
4

我试图将现有训练有素的PyTorch模型移植到Keras .

在移植过程中,我陷入了LSTM层 .

LSTM网络的Keras实现似乎有三种状态矩阵,而Pytorch实现有四种 .

例如,对于具有hidden_layers = 64的双向LSTM,input_size = 512&output size = 128状态参数,如下所示

State params of Keras LSTM

[<tf.Variable 'bidirectional_1/forward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref>,
 <tf.Variable 'bidirectional_1/forward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref>,
 <tf.Variable 'bidirectional_1/forward_lstm_1/bias:0' shape=(256,) dtype=float32_ref>,
 <tf.Variable 'bidirectional_1/backward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref>,
 <tf.Variable 'bidirectional_1/backward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref>,
 <tf.Variable 'bidirectional_1/backward_lstm_1/bias:0' shape=(256,) dtype=float32_ref>]

State params of PyTorch LSTM

['rnn.0.rnn.weight_ih_l0', torch.Size([256, 512])],
 ['rnn.0.rnn.weight_hh_l0', torch.Size([256, 64])],
 ['rnn.0.rnn.bias_ih_l0', torch.Size([256])],
 ['rnn.0.rnn.bias_hh_l0', torch.Size([256])],
 ['rnn.0.rnn.weight_ih_l0_reverse', torch.Size([256, 512])],
 ['rnn.0.rnn.weight_hh_l0_reverse', torch.Size([256, 64])],
 ['rnn.0.rnn.bias_ih_l0_reverse', torch.Size([256])],
 ['rnn.0.rnn.bias_hh_l0_reverse', torch.Size([256])],

我试着查看两个实现的代码,但不能理解太多 .

有人可以帮我把PyTorch的4组状态参数转换成Keras的3组状态参数

1 回答

  • 6

    他们真的没那么不同 . 如果在PyTorch中总结两个偏置向量,则方程将与Keras中实现的方程相同 .

    这是PyTorch documentation上的LSTM公式:

    enter image description here

    PyTorch使用两个单独的偏置向量进行输入转换(下标以 i 开头)和循环转换(下标以 h 开头) .

    在Keras LSTMCell

    x_i = K.dot(inputs_i, self.kernel_i)
            x_f = K.dot(inputs_f, self.kernel_f)
            x_c = K.dot(inputs_c, self.kernel_c)
            x_o = K.dot(inputs_o, self.kernel_o)
            if self.use_bias:
                x_i = K.bias_add(x_i, self.bias_i)
                x_f = K.bias_add(x_f, self.bias_f)
                x_c = K.bias_add(x_c, self.bias_c)
                x_o = K.bias_add(x_o, self.bias_o)
    
            if 0 < self.recurrent_dropout < 1.:
                h_tm1_i = h_tm1 * rec_dp_mask[0]
                h_tm1_f = h_tm1 * rec_dp_mask[1]
                h_tm1_c = h_tm1 * rec_dp_mask[2]
                h_tm1_o = h_tm1 * rec_dp_mask[3]
            else:
                h_tm1_i = h_tm1
                h_tm1_f = h_tm1
                h_tm1_c = h_tm1
                h_tm1_o = h_tm1
            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))
    

    输入转换中只添加了一个偏差 . 但是,如果我们总结PyTorch中的两个偏差,则方程式将是等价的 .

    双偏置LSTM是在cuDNN中实现的(参见developer guide) . 我'm really not that familiar with PyTorch, but I guess that'为什么他们使用两个偏差参数 . 在Keras中, CuDNNLSTM 层也有两个偏置权重向量 .

相关问题