简体   繁体   English

Keras:在这种情况下我应该使用一维还是二维卷积层?

[英]Keras: Should I use an 1D or a 2D convolutional layer in this case?

Suppose that we have a dataset with N labeled instances, and each instance is a 2D matrix of 2 X M. That is, each instance has two rows, where each row is a vector of size M (M columns).假设我们有一个包含 N 个标记实例的数据集,每个实例是一个 2 X M 的二维矩阵。也就是说,每个实例有两行,其中每一行是一个大小为 M(M 列)的向量。

I would like to build a NN whose first layer performs a convolution operation, with a kernel with two rows and one column.我想构建一个 NN,其第一层执行卷积操作,kernel 有两行一列。 The idea is to apply this kernel to each column of the input matrix, generating a single value for each column as result.想法是将此 kernel 应用于输入矩阵的每一列,为每一列生成一个值作为结果。 The idea would be generating as output a vector of size M, where each position P of the vector would be generated by a convolution of the two rows in column P. The following picture illustrates the idea.这个想法将生成为 output 大小为 M 的向量,其中向量的每个 position P 将由 P 列中的两行的卷积生成。下图说明了这个想法。

在此处输入图像描述

I don't know how to build this convolutional layer.我不知道如何构建这个卷积层。 Do I need a 1D or a 2D convolution in this case?在这种情况下,我需要 1D 还是 2D 卷积?

I would like to build a NN with the following architecture:我想构建一个具有以下架构的神经网络:

  • Convolutional layer with inputs of 2 XM and outputs of M. I would like to apply k kernels (producing k vectors of size M)输入为 2 XM,输出为 M 的卷积层。我想应用 k 个内核(生成 k 个大小为 M 的向量)
  • Dense layer with 500 neurons and relu activation.具有 500 个神经元和 relu 激活的密集层。
  • Dropout of 0.2 0.2的辍学
  • Dense layer with 2 neurons and softmax activation.具有 2 个神经元和 softmax 激活的密集层。

Can you help me in building this architecture?你能帮我建立这个架构吗?

You want to be using a 2D CNN for this purpose.您想为此目的使用 2D CNN。 A 1D CNN will only expect 1 spatial dimension but you have 2 spatial dimensions even though you don't have any 'width' to convolve multiple times on.一维 CNN 只需要 1 个空间维度,但您有 2 个空间维度,即使您没有任何“宽度”可以进行多次卷积。

A 2D CNN expects a 4D (batch, height, width, channels) . 2D CNN 需要 4D (batch, height, width, channels) Your kernel would also be 4D accordingly.您的 kernel 也相应地是 4D。

Check this code for more details -检查此代码以获取更多详细信息 -

import tensorflow as tf

inp = np.array([[[[2.1],[0.8]],[[1.3],[2.4]],[[1.8],[1.3]]]])

kernel = np.array([[[[1.0]],[[2.0]]]])

print('input shape ->',inp.shape)
print('kernel shape ->',kernel.shape)

result = tf.nn.conv2d(x, kernel, strides=(1,1,1,1), padding='VALID')

print('result shape ->',result.shape)
print(result.numpy())
input shape -> (1, 3, 2, 1)
kernel shape -> (1, 2, 1, 1)
result shape -> (1, 3, 1, 1)

[[[[3.6999998]]

  [[6.1000004]]

  [[4.3999996]]]]

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

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