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

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

问题描述

真的很难理解喀拉斯语中卷积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张量

(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?

此外,您如何以2d张量的形式将池化层(3d张量)的输出转换为适合标准隐藏层(即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)到一维转换层或(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.

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

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