简体   繁体   English

简单Conv1D作为keras中的第一层

[英]Simple Conv1D as first layer in keras

Here is my input 这是我的意见

x_train.shape # (12, 7) 12 observations each of length 7
x_train # dtype('int32')

Here's the architecture I'd like to achieve: 这是我想要实现的架构:

在此输入图像描述

I'd like a kernel of size 3 convolved over the sequence. 我想要一个大小为3的内核在序列上进行卷积。 From keras documentation at https://keras.io/layers/convolutional/ 来自https://keras.io/layers/convolutional/上的 keras文档

"When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, eg (10, 128) for sequences of 10 vectors of 128-dimensional vectors, or (None, 128) for variable-length sequences of 128-dimensional vectors." “当使用该层作为模型中的第一层时,提供一个input_shape参数(整数或无元组,例如(10,128),用于128个向量的10个向量的序列,或者(无,128)用于变量 - 128维向量的长度序列。“

Honestly I'm having a hard time understanding their logic. 老实说,我很难理解他们的逻辑。 Here's my attempt 这是我的尝试

docs_sequence = Input(shape=(7,), dtype='float32') # Longest document is 7 words
convolution = Conv1D(filters = 1,  # only 1 convolution
                     kernel_size = 3, # tri grams
                     strides = 1,
                     input_shape = (1, 7),
                     padding = 'valid',
                     activation = 'relu')(docs_sequence)
output = Dense(1, activation='sigmoid')(convolution)
cnn_model = Model(inputs = docs_sequence, outputs = [output])
cnn_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

and I'm consistently getting 而且我一直都在

ValueError: Input 0 is incompatible with layer conv1d_30: expected ndim=3, found ndim=2 ValueError:输入0与图层conv1d_30不兼容:预期ndim = 3,发现ndim = 2

As the error message says, your input is two dimensional while the convolutional layer expects a three dimensional input. 正如错误消息所示,您的输入是二维的,而卷积层需要三维输入。

With the following 有以下几点

docs_sequence = Input(shape=(7,1), ...

instead of 代替

docs_sequence = Input(shape=(7,), ...

Keras accepts the model. Keras接受该模型。 Basically this adds a dimension of size one to the input (the three dimensions from the error message include the minibatch dimension which one can think of being prepended to the shape argument above). 基本上,这会为输入添加一个大小为1的维度(错误消息中的三个维度包括一个可以认为是前面的shape参数的小批量维度)。

cnn_model.summary() then gives: 然后cnn_model.summary()给出:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 7, 1)              0
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 5, 1)              4
_________________________________________________________________
dense_1 (Dense)              (None, 5, 1)              2
=================================================================

When preparing the actual input data, you may have to add this dimension of size one to your input data. 在准备实际输入数据时,您可能必须将大小为1的维度添加到输入数据中。 You may want to use numpy.atleast_2d() or numpy.atleast_3d() for this, possibly combined with taking the transpose or use numpy.expand_dims() . 您可能希望使用numpy.atleast_2d()numpy.atleast_3d() ,可能与转置或使用numpy.expand_dims()结合使用。

In your case, np.atleast_3d(x_train) gives a shape of (12, 7, 1) . 在您的情况下, np.atleast_3d(x_train)的形状为np.atleast_3d(x_train) (12, 7, 1)

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

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