Keras:一维输入的卷积层 [英] Keras: convolutional layer for 1D input

查看:530
本文介绍了Keras:一维输入的卷积层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为1D输入向量构建CNN.

I can not build CNN for 1D input vector.

输入值示例:

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

我有15,000个训练示例.

I have 15k traing examples.

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.

I want to build model where first layer will be conv size (5,1), 5 filters and input shape df_x.shape[1], 1.

我有一个错误:

ValueError: Input 0 is incompatible with layer conv2d_10: expected ndim=4, found ndim=3

您能解释一下如何为一维输入值构建CNN吗?

Can you explain me how can I build CNN for 1D input values?

推荐答案

为此,您应该使用Conv1D而不是Conv2D.

You should use Conv1D instead of Conv2D for that.

Conv2D之所以被命名为2维,是因为它旨在处理图像.但是,Conv2D的输入实际上是 4维-(batch, width, height, channels)channels对于RGB可以是3,对于灰度图像可以是1.这就是keras抱怨的原因:

Conv2D is named 2-dimensional because it's designed to process images. However, the input to Conv2D is actually 4-dimensional - (batch, width, height, channels); the channels can be 3 for RGB or 1 for grey-scale images. That's why keras is complaining:

ValueError:输入0与层conv2d_10不兼容:预期 ndim=4,找到了ndim=3

ValueError: Input 0 is incompatible with layer conv2d_10: expected ndim=4, found ndim=3

Conv1D接受 3维输入,这正是您所拥有的(只要将df_x扩展到(15772, 16, 1)).同样,input_shape参数必须匹配每一行的大小.试试这个:

Conv1D accepts 3-dimensional input and that's exactly what you have (provided that you expand your df_x to (15772, 16, 1)). Also the input_shape argument must match the size of each row. Try this:

model.add(Conv1D(5, 5, input_shape=(df_x.shape[1], 1)))

这篇关于Keras:一维输入的卷积层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆