title: Converting state-parameters of Pytorch LSTM to Keras LSTM
body: <p>I was trying to port an existing trained PyTorch model into Keras.</p>
  
  <p>During the porting, I got stuck at LSTM layer.</p>
  
  <p>Keras implementation of LSTM network seems to have three state kind of state matrices while Pytorch implementation have four.</p>
  
  <p>For eg, for an Bidirectional LSTM with hidden_layers=64, input_size=512 &amp; output size=128 state parameters where as follows</p>
  
  <p><strong>State params of Keras LSTM</strong></p>
  
  <pre><code>[&lt;tf.Variable 'bidirectional_1/forward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref&gt;,
   &lt;tf.Variable 'bidirectional_1/forward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref&gt;,
   &lt;tf.Variable 'bidirectional_1/forward_lstm_1/bias:0' shape=(256,) dtype=float32_ref&gt;,
   &lt;tf.Variable 'bidirectional_1/backward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref&gt;,
   &lt;tf.Variable 'bidirectional_1/backward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref&gt;,
   &lt;tf.Variable 'bidirectional_1/backward_lstm_1/bias:0' shape=(256,) dtype=float32_ref&gt;]
  </code></pre>
  
  <p><strong>State params of PyTorch LSTM</strong></p>
  
  <pre><code> ['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])],
  </code></pre>
  
  <p>I tried to look in to the code of both implementation but not able to understand much.</p>
  
  <p>Can someone please help me to transform 4-set of state params from PyTorch  into 3-set of state params in Keras</p>