[英]convert Lasagne to Keras code (CNN -> LSTM)
I would like to convert this Lasagne code:我想转换这个千层面代码:
et = {}
net['input'] = lasagne.layers.InputLayer((100, 1, 24, 113))
net['conv1/5x1'] = lasagne.layers.Conv2DLayer(net['input'], 64, (5, 1))
net['shuff'] = lasagne.layers.DimshuffleLayer(net['conv1/5x1'], (0, 2, 1, 3))
net['lstm1'] = lasagne.layers.LSTMLayer(net['shuff'], 128)
in Keras code.在 Keras 代码中。 Currently I came up with this:
目前我想出了这个:
multi_input = Input(shape=(1, 24, 113), name='multi_input')
y = Conv2D(64, (5, 1), activation='relu', data_format='channels_first')(multi_input)
y = LSTM(128)(y)
But I get the error: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
但我收到错误:
Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
from keras.layers import Input, Conv2D, LSTM, Permute, Reshape
multi_input = Input(shape=(1, 24, 113), name='multi_input')
print(multi_input.shape) # (?, 1, 24, 113)
y = Conv2D(64, (5, 1), activation='relu', data_format='channels_first')(multi_input)
print(y.shape) # (?, 64, 20, 113)
y = Permute((2, 1, 3))(y)
print(y.shape) # (?, 20, 64, 113)
# This line is what you missed
# ==================================================================
y = Reshape((int(y.shape[1]), int(y.shape[2]) * int(y.shape[3])))(y)
# ==================================================================
print(y.shape) # (?, 20, 7232)
y = LSTM(128)(y)
print(y.shape) # (?, 128)
I put the documents of Lasagne and Keras here so you can do cross-referencing:我把 Lasagne 和 Keras 的文档放在这里,所以你可以做交叉引用:
Recurrent layers can be used similarly to feed-forward layers except that the input shape is expected to be
(batch_size, sequence_length, num_inputs)
除了输入形状预期为
(batch_size, sequence_length, num_inputs)
之外,循环层可以与前馈层类似地使用
Input shape
输入形状
3D tensor with shape
(batch_size, timesteps, input_dim)
.具有形状
(batch_size, timesteps, input_dim)
3D 张量。
Basically the API is the same, but Lasagne probably does reshape for you (I need to check the source code later).基本上 API 是一样的,但 Lasagne 可能会为你重塑(我需要稍后检查源代码)。 That's why you got this error:
这就是您收到此错误的原因:
Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
, since the tensor shape after Conv2D
is (?, 64, 20, 113)
of ndim=4
, 因为
Conv2D
之后的张量形状是(?, 64, 20, 113)
of ndim=4
Therefore, the solution is to reshape it to (?, 20, 7232)
.因此,解决方案是将其重塑为
(?, 20, 7232)
。
Confirmed with the Lasagne source code , it does the trick for you:与千层面源代码确认,它为你做的伎俩:
num_inputs = np.prod(input_shape[2:])
So the correct tensor shape as input for LSTM is (?, 20, 64 * 113)
= (?, 20, 7232)
所以作为 LSTM 输入的正确张量形状是
(?, 20, 64 * 113)
= (?, 20, 7232)
Permute
is redundant here in Keras since you have to reshape anyway. Permute
在Permute
是多余的,因为无论如何你都必须重塑。 The reason why I put it here is to have a "full translation" from Lasagne to Keras, and it does what DimshuffleLaye
does in Lasagne.我把它放在这里的原因是有一个从 Lasagne 到
DimshuffleLaye
的“完整翻译”,它做了DimshuffleLaye
在 Lasagne 中所做的。
DimshuffleLaye
is however needed in Lasagne because of the reason I mentioned in Edit , the new dimension created by Lasagne LSTM is from the multiplication of "the last two" dimensions.然而,由于我在Edit 中提到的原因,在 Lasagne 中需要
DimshuffleLaye
,Lasagne LSTM 创建的新维度来自“最后两个”维度的乘法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.