Keras/Tensorflow Conv1D expected input shape
我想对29个要素输入数据应用一维卷积(如29x1形状)。 我告诉Keras
Keras文档给出了如何使用input_shape的奇怪示例:
(None, 128) for variable-length sequences with 128 features per step.
我不确定可变长度序列的含义,但是由于我有29个功能,因此我也尝试了
我是否对一维卷积的功能有误解?
在给定7x1输入的情况下,这是我期望Conv1D在内核大小为3的情况下所做的直观描述。
1 2 3 4 5 | [x][x][x][ ][ ][ ][ ] [ ][x][x][x][ ][ ][ ] [ ][ ][x][x][x][ ][ ] [ ][ ][ ][x][x][x][ ] [ ][ ][ ][ ][x][x][x] |
为什么Keras期望3维?
The three dimensions are (batch_size, feature_size, channels).
blockquote>
blockquote>定义一维转换层
1 Conv1D(32, (3), activation='relu' , input_shape=( 29, 1 ))将(4000、29、1)样品送入该层。
简单的例子:
1
2
3
4
5
6
7
8
9
10
11
12 from keras import models, layers
import numpy as np
x = np.ones((10, 29, 1))
y = np.zeros((10,))
model = models.Sequential()
model.add(layers.Conv1D(32, (3), activation='relu' , input_shape=( 29,1)))
model.add(layers.Flatten())
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer="adam", metrics=['accuracy'])
print(model.summary())
model.fit(x,y)