简体   繁体   English

是否可以在 Keras 中生成二维卷积层的一维 output?

[英]Is it possible to generate an 1D dimensional output of a 2D convolutional layer in Keras?

I'm trying to apply convolutional neural networks for dealing with a 2D input, which is a 2X300 matrix.我正在尝试应用卷积神经网络来处理 2D 输入,这是一个 2X300 矩阵。 It is basically a matrix with 2 lines, where each line is a vector of 300 positions.它基本上是一个有 2 行的矩阵,其中每行是 300 个位置的向量。

I would like to apply a kernel of size 2X1 (two lines and one column).我想申请一个尺寸为 2X1(两行一列)的 kernel。 The idea is to apply the kernel to each position i of the two vectors.这个想法是将 kernel 应用于两个向量的每个 position i 。 Intuitively, I think that this convolution operation would generate an output of size 1X300.直觉上,我认为这个卷积操作会生成一个大小为 1X300 的 output。 That is, I think that the output will be an unidimensional vector with 300 columns.也就是说,我认为 output 将是一个 300 列的一维向量。 Am I right?我对吗?

I would like to include a convolutional layer like this:我想包括一个这样的卷积层:

layers.Conv2D(10, kernel_size=(2, 1), activation="relu",name="conv1")

That makes sense?那讲得通? Will this layer generate a one-dimensional vector of 300 positions?这个层会生成一个300个位置的一维向量吗?

No, an input to a 2D convolutional layer needs to have 3 dimensions (typically width,height,channel).不,2D 卷积层的输入需要具有 3 个维度(通常是宽度、高度、通道)。 What you are looking for is a 1D convolutional layer, which operates on a sequence of data (typically timestep,channel).您正在寻找的是一维卷积层,它对一系列数据(通常是时间步长、通道)进行操作。 In your case, the channel is the first dimension, ie, the dimension where all inputs are taken into account for each step, and your second dimension is the steps (over each of which the convolutional kernel is shifted).在您的情况下,通道是第一个维度,即每个步骤都考虑所有输入的维度,而您的第二个维度是步骤(在每个步骤上,卷积 kernel 被移动)。 This needs to be specified with the data_format keyword as 'channels_last' would be default for tensorflow/keras.这需要使用data_format关键字指定,因为'channels_last'将是 tensorflow/keras 的默认值。

model.add(Conv1D(1, kernel_size=(1,), data_format='channels_first', activation = 'relu', input_shape=(2, 300)))

This will give you an output of shape (1,300) .这将为您提供形状(1,300)的 output 。 The first parameter is the number of output channels (1 in your case).第一个参数是 output 通道的数量(在您的情况下为 1)。 The kernel size is 1, as you do not take into account samples from other steps for the convolution, only the two channels (1D convolution is always performed across all channels). kernel 大小为 1,因为您不考虑卷积其他步骤的样本,仅考虑两个通道(始终在所有通道上执行一维卷积)。 If you choose a larger kernel size, padding='same' might be required as an additional parameter to get an output of the same number of steps.如果您选择更大的 kernel 大小,则可能需要padding='same'作为附加参数,以获得相同步数的 output。

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

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