Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

machine learning - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...