Python keras如何将卷积层后的输入大小改成lstm层 [英] Python keras how to change the size of input after convolution layer into lstm layer

查看:28
本文介绍了Python keras如何将卷积层后的输入大小改成lstm层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对卷积层和lstm层之间的连接有问题.数据的形状为 (75,5),其中每个时间步长有 75 个时间步长 x 5 个数据点.我想要做的是对 (75x5) 进行卷积,获取新的卷积 (75x5) 数据并将该数据输入 lstm 层.但是,它不起作用,因为卷积层的输出形状具有我不需要的过滤器数量.因此卷积层输出的形状为(1,75,5),lstm 层所需的输入为(75,5).我如何只使用第一个过滤器.

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

这是出现的错误:

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)

推荐答案

您可以在中间添加 Reshape() 层以使尺寸兼容.

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.

输出形状

(batch_size,) + dims

参数

dims:目标形状.整数元组,不包括样本维度(batch size).

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

这篇关于Python keras如何将卷积层后的输入大小改成lstm层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆