简体   繁体   English

输入形状和Keras中的Conv1d

[英]Input shape and Conv1d in Keras

The first layer of my neural network is like this: 我的神经网络的第一层是这样的:

model.add(Conv1D(filters=40,
                 kernel_size=25,
                 input_shape=x_train.shape[1:],
                 activation='relu',
                 kernel_regularizer=regularizers.l2(5e-6),
                 strides=1))

if my input shape is (600,10) 如果我的输入形状是(600,10)

i get (None, 576, 40) as output shape 我得到(None, 576, 40)作为输出形状

if my input shape is (6000,1) 如果我的输入形状是(6000,1)

i get (None, 5976, 40) as output shape 我得到(None, 5976, 40)作为输出形状

so my question is what exactly is happening here? 所以我的问题是这里到底发生了什么? is the first example simply ignoring 90% of the input? 是第一个简单地忽略90%输入的例子?

It is not "ignoring" a 90% of the input, the problem is simply that if you perform a 1-dimensional convolution with a kernel of size K over an input of size X the result of the convolution will have size X - K + 1. If you want the output to have the same size as the input, then you need to extend or "pad" your data. 它不是“忽略”90%的输入,问题很简单,如果你在大小为X的输入上使用大小为K的内核执行一维卷积,卷积的结果将具有X - K +的大小1.如果希望输出具有与输入相同的大小,则需要扩展或“填充”数据。 There are several strategies for that, such as add zeros, replicate the value at the ends or wrap around. 有几种策略,例如添加零,在末尾复制值或环绕。 Keras' Convolution1D has a padding parameter that you can set to "valid" (the default, no padding), "same" (add zeros at both sides of the input to obtain the same output size as the input) and "causal" (padding with zeros at one end only, idea taken from WaveNet ). Keras的Convolution1D有一个padding参数,可以设置为"valid" (默认,无填充), "same" (在输入的两侧添加零以获得与输入相同的输出大小)和"causal" (仅在一端用零填充,从WaveNet获取的想法 )。

Update 更新

About the questions in your comments. 关于您评论中的问题。 So you say your input is (600, 10) . 所以你说你的输入是(600, 10) That, I assume, is the size of one example, and you have a batch of examples with size (N, 600, 10) . 我认为,这是一个例子的大小,你有一批大小(N, 600, 10)的例子。 From the point of view of the convolution operation, this means you have N examples, each of with a length of at most 600 (this "length" may be time or whatever else, it's just the dimension across which the convolution works) and, at each of these 600 points, you have vectors of size 10 . 从卷积运算的角度来看,这意味着你有N例子,每个例子的长度最多为600 (这个“长度”可能是时间或其他任何东西,它只是卷积工作的维度)和在这600个点中,每个都有10向量。 Each of these vectors is considered an atomic sample with 10 features (eg price, heigh, size, whatever), or, as is sometimes called in the context of convolution, "channels" (from the RGB channels used in 2D image convolution). 这些矢量中的每一个被认为是具有10特征(例如价格,高度,大小等)的原子样本,或者,如在卷积的上下文中有时称为“通道”(来自2D图像卷积中使用的RGB通道)。

The point is, the convolution has a kernel size and a number of output channels, which is the filters parameter in Keras. 关键是,卷积具有内核大小和多个输出通道,这是Keras中的filters参数。 In your example, what the convolution does is take every possible slice of 25 contiguous 10-vectors and produce a single 40-vector for each (that, for every example in the batch, of course). 在您的示例中,卷积所做的是采用每个可能的25个连续10矢量切片,并为每个矢量生成一个40矢量(当然,对于批处理中的每个示例)。 So you pass from having 10 features or channels in your input to having 40 after the convolution. 因此,您将输入中的10个要素或通道转换为卷积后的40个要素或通道。 It's not that it's using only one of the 10 elements in the last dimension, it's using all of them to produce the output. 并不是它只使用最后一个维度中的10个元素中的一个,而是使用它们来生成输出。

If the meaning of the dimensions in your input is not what the convolution is interpreting, or if the operation it is performing is not what you were expecting, you may need to either reshape your input or use a different kind of layer. 如果输入中维度的含义不是卷积解释的含义,或者它正在执行的操作不是您期望的操作,则可能需要重新整形输入或使用不同类型的图层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM