我试图将现有训练有素的PyTorch模型移植到Keras.
在移植过程中,我陷入了LSTM层.
LSTM网络的Keras实现似乎有三种状态矩阵,而Pytorch实现有四种.
例如,对于具有hidden_layers = 64的双向LSTM,input_size = 512&output size = 128状态参数,如下所示
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>]
Run Code Online (Sandbox Code Playgroud)
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])],
Run Code Online (Sandbox Code Playgroud)
我试着查看两个实现的代码,但不能理解太多.
有人可以帮我把PyTorch的4组状态参数转换成Keras的3组状态参数
我试图将CRNN模型移植到 Keras。
但是,我在将 Conv2D 层的输出连接到 LSTM 层时卡住了。
CNN 层的输出将具有(batch_size, 512, 1, width_dash)的形状,其中第一个取决于 batch_size,最后一个取决于输入的输入宽度(此模型可以接受可变宽度输入)
例如:形状为[2, 1, 32, 829]的输入产生形状为(2, 512, 1, 208) 的输出
现在,根据Pytorch 模型,我们必须执行挤压 (2) 和permute(2, 0, 1) 它将产生一个形状为 [208, 2, 512] 的张量
我试图实现这是 Keras,但我无法做到这一点,因为在 Keras 中,我们无法更改keras.models.Sequential模型中的batch_size 维度
有人可以指导我如何将这个模型的一部分移植到 Keras 吗?