keras 中一维卷积网络的输入维度 [英] input dimensions to a one dimensional convolutional network in keras

查看:100
本文介绍了keras 中一维卷积网络的输入维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的很难理解 keras 中卷积 1d layer 的输入维度:

really finding it hard to understand the input dimensions to the convolutional 1d layer in keras:

输入形状

具有形状的 3D 张量:(样本、步骤、input_dim).

3D tensor with shape: (samples, steps, input_dim).

输出形状

具有形状的 3D 张量:(样本、new_steps、nb_filter).由于填充,steps 值可能已更改.

3D tensor with shape: (samples, new_steps, nb_filter). steps value might have changed due to padding.

我希望我的网络接收价格的时间序列(按顺序为 101)并输出 4 个概率.我当前的非卷积网络在这方面做得相当好(训练集为 28000)如下所示:

I want my network to take in a time series of prices (101, in order) and output 4 probabilities. My current non-convolutional network which does this fairly well (with a training set of 28000) looks like this:

standardModel = Sequential()
standardModel.add(Dense(input_dim=101, output_dim=100, W_regularizer=l2(0.5), activation='sigmoid'))
standardModel.add(Dense(4, W_regularizer=l2(0.7), activation='softmax'))

为了改进这一点,我想从具有长度为 10 的局部感受野的输入层制作一个特征图.(因此有 10 个共享权重和 1 个共享偏差).然后我想使用最大池化并将其输入到 40 个左右的神经元的隐藏层,然后在外层使用 4 个带有 softmax 的神经元输出它.

To improve this, I want to make a feature map from the input layer which has a local receptive field of length 10. (and therefore has 10 shared weights and 1 shared bias). I then want to use max pooling and feed this in to a hidden layer of 40 or so neurons and then output this with 4 neurons with softmax in the outer layer.

图片(非常抱歉!)

所以理想情况下,卷积层将采用二维张量:

So ideally, the convolutional layer would take a 2d tensor of dimensions:

(minibatch_size, 101)

(minibatch_size, 101)

并输出维度的 3d 张量

and output a 3d tensor of dimensions

(minibatch_size, 91, no_of_featuremaps)

(minibatch_size, 91, no_of_featuremaps)

然而,keras 层似乎需要输入中的一个维度,称为 step.我已经尝试理解这一点,但仍然不太明白.在我的情况下,步骤应该是 1,因为向量中的每一步都将时间增加 1?另外,new_step 是什么?

However, the keras layer seems to require a dimension in the input called step. I've tried understanding this and still don't quite get it. In my case, should step be 1 as each step in the vector is an increase in the time by 1? Also, what is new_step?

此外,如何将池化层(3d 张量)的输出以 2d 张量的形式转换为适合标准隐藏层(即 Dense keras 层)的输入?

In addition, how do you turn the output of the pooling layers (a 3d tensor) into input suitable for the standard hidden layer (i.e a Dense keras layer) in the form of a 2d tensor?

更新:在给出非常有用的建议之后,我尝试制作一个像这样的卷积网络:

Update: After the very helpful suggestions given, I tried making a convolutional network like so:

conv = Sequential()
conv.add(Convolution1D(64, 10, input_shape=(1,101)))
conv.add(Activation('relu'))
conv.add(MaxPooling1D(2))
conv.add(Flatten())
conv.add(Dense(10))
conv.add(Activation('tanh'))
conv.add(Dense(4))
conv.add(Activation('softmax'))

conv.Add(Flatten()) 行抛出一个范围超出有效边界错误.有趣的是,此错误不会仅针对此代码引发:

The line conv.Add(Flatten()) throws a range exceeds valid bounds error. Interestingly, this error is not thrown for just this code:

conv = Sequential()
conv.add(Convolution1D(64, 10, input_shape=(1,101)))
conv.add(Activation('relu'))
conv.add(MaxPooling1D(2))
conv.add(Flatten())

在做

print conv.input_shape
print conv.output_shape

结果

(None, 1, 101
(None, -256)

被退回

更新 2:

改变了

conv.add(Convolution1D(64, 10, input_shape=(1,101)))

conv.add(Convolution1D(10, 10, input_shape=(101,1))

它开始工作了.但是,两者之间有什么重要的区别吗?将 (None, 101, 1) 输入到我应该注意的 1d conv 层或 (None, 1, 101) ?为什么 (None, 1, 101) 不起作用?

and it started working. However, is there any important different between inputting (None, 101, 1) to a 1d conv layer or (None, 1, 101) that I should be aware of? Why does (None, 1, 101) not work?

推荐答案

之所以会这样,是因为 Keras 的设计者打算将一维卷积框架解释为处理序列的框架.要完全理解差异 - 尝试想象您有多个特征向量的序列.那么你的输出将至少是二维的 - 其中第一维与时间相关,其他维度与特征相关.一维卷积框架旨在以某种方式大胆地处理这个时间维度,并尝试在数据中找到重复出现的模式——而不是执行经典的多维卷积变换.

The reason why it look like this is that Keras designer intended to make 1-dimensional convolutional framework to be interpreted as a framework to deal with sequences. To fully understand the difference - try to imagine that you have a sequence of a multiple feature vectors. Then your output will be at least two dimensional - where first dimension is connected with time and other dimensions are connected with features. 1-dimensional convolutional framework was designed to in some way bold this time dimension and try to find the reoccuring patterns in data - rather than performing a classical multidimensional convolutional transformation.

在您的情况下,您必须简单地将数据重塑为具有形状 (dataset_size, 101, 1) - 因为您只有一个特征.使用 numpy.reshape 函数可以轻松完成.要了解新步骤的含义-您必须了解您正在随时间进行卷积-因此您要更改数据的时间结构-这会导致新的时间连接结构.为了将您的数据转换为适合密集/静态层的格式,请使用 keras.layers.flatten 层 - 与经典卷积情况相同.

In your case you must simply reshape your data to have shape (dataset_size, 101, 1) - because you have only one feature. It could be easly done using numpy.reshape function. To understand what does a new step mean - you must understand that you are doing the convolution over time - so you change the temporal structure of your data - which lead to new time-connected structure. In order to get your data to a format which is suitable for dense / static layers use keras.layers.flatten layer - the same as in classic convolutional case.

更新: 正如我之前提到的 - 输入的第一个维度与时间有关.所以 (1, 101)(101, 1) 之间的区别在于,在第一种情况下,你有一个时间步长有 101 个特征,第二个 - 101 个时间步长1个特点.您在第一次更改后提到的问题源于对此类输入进行大小为 2 的池化.只有一个时间步长 - 您不能在大小为 2 的时间窗口上汇集任何值 - 仅仅是因为没有足够的时间步长来做到这一点.

UPDATE: As I mentioned before - the first dimension of input is connected with time. So the difference between (1, 101) and (101, 1) lies in that in first case you have one time step with 101 features and in second - 101 timesteps with 1 feature. The problem which you mentioned after your first change has its origin in making pooling with size 2 on such input. Having only one timestep - you cannot pool any value on a time window of size 2 - simply because there is not enough timesteps to do that.

这篇关于keras 中一维卷积网络的输入维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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