Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python keras how to change the size of input after convolution layer into lstm layer

I have a problem with the connection between convolution layer and lstm layer. The data is of shape(75,5) where there is 75 timesteps x 5 data points for each time step. What I want to do is do a convolution on (75x5), get new convolved (75x5) data and feed that data into lstm layer. However, it does not work because the shape of output of convolution layer has number of filters which I do not need. And therefore the shape of convolution layer output is (1,75,5) and input needed for lstm layer is (75,5). How do I just take the first filter.

model = Sequential() 
model.add(Convolution2D(1, 5,5,border_mode='same',input_shape=(1,75, 5)))
model.add(Activation('relu'))
model.add(LSTM(75, return_sequences=False, input_shape=(75, 5)))
model.add(Dropout(0.5))
model.add(Dense(1))
model.compile(loss='mse', optimizer='rmsprop')

And this is the error that comes up:

File "/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py", line 378, in __init__
super(LSTM, self).__init__(**kwargs)
File "/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py", line 97, in __init__
super(Recurrent, self).__init__(**kwargs)
File "/usr/local/lib/python3.4/dist-packages/keras/layers/core.py", line 43, in __init__
self.set_input_shape((None,) + tuple(kwargs['input_shape']))
File "/usr/local/lib/python3.4/dist-packages/keras/layers/core.py", line 138, in set_input_shape
', was provided with input shape ' + str(input_shape))
Exception: Invalid input shape - Layer expects input ndim=3, was provided with input shape (None, 1, 75, 5)
like image 294
Goodie123 Avatar asked Feb 07 '16 13:02

Goodie123


1 Answers

You can add Reshape() layer in between to make dimensions compatible.

http://keras.io/layers/core/#reshape

keras.layers.core.Reshape(dims)

Reshape an output to a certain shape.

Input shape

Arbitrary, although all dimensions in the input shaped must be fixed. Use the keyword argument input_shape (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model.

Output shape

(batch_size,) + dims

Arguments

dims: target shape. Tuple of integers, does not include the samples dimension (batch size).

like image 174
lejlot Avatar answered Sep 24 '22 03:09

lejlot