我无法为一维输入向量构建 CNN。
输入值示例:
df_x.iloc[300]
Out[33]:
0 0.571429
1 1.000000
2 0.971429
3 0.800000
4 1.000000
5 0.142857
6 0.657143
7 0.857143
8 0.971429
9 0.000000
10 0.000000
11 0.000000
12 0.000000
13 0.000000
14 0.000000
15 0.000000
Name: 300, dtype: float64
输出值示例:
df_y.iloc[300]
Out[34]:
0 0.571429
1 0.914286
2 1.000000
3 0.971429
4 0.800000
5 1.000000
6 0.914286
7 0.942857
8 0.800000
9 0.657143
10 0.857143
11 0.971429
12 0.000000
13 0.000000
14 0.000000
15 0.000000
16 0.000000
17 0.000000
18 0.000000
19 0.000000
20 0.000000
21 0.000000
22 0.000000
我有 15k 个训练示例。
df_x.shape
Out[28]:
(15772, 16)
df_y.shape
Out[29]:
(15772, 23)
我当前的模型:
model = Sequential()
model.add(Conv2D(5, df_x.shape[1], input_shape=(5, 1)))
model.add(Dense(46, activation='relu'))
model.add(Dense(56, activation='relu'))
model.add(Dense(66, activation='relu'))
model.add(Dense(56, activation='relu'))
model.add(Dense(46, activation='relu'))
model.add(Dense(df_y.shape[1], activation='relu'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(df_x, df_y, epochs=5, batch_size=10)
我想构建模型,其中第一层将是转换大小 (5,1)、5 个过滤器和输入形状 df_x.shape[1], 1。
我有一个错误:
ValueError: Input 0 is incompatible with layer conv2d_10: expected ndim=4, found ndim=3
你能解释一下如何为一维输入值构建 CNN 吗?
请您参考如下方法:
为此,您应该使用 Conv1D 而不是 Conv2D。
Conv2D 之所以被命名为 2-dimensional,是因为它旨在处理图像。但是,Conv2D 的输入实际上是4 维 - (batch, width, height, channels); channels 对于 RGB 可以是 3,对于灰度图像可以是 1。这就是 keras 提示的原因:
ValueError: Input 0 is incompatible with layer conv2d_10: expected
ndim=4, foundndim=3
Conv1D 接受3 维 输入,这正是您所拥有的(前提是您将df_x 扩展为(15772, 16、1))。此外,input_shape 参数必须与每一行的大小相匹配。试试这个:
model.add(Conv1D(5, 5, input_shape=(df_x.shape[1], 1)))
