我试图在Keras中使用一维CNN进行二进制分类。我有一台连续执行动作的机器,我的目标是对该动作是正常还是异常进行分类。
为了监视每个动作的行为,有4个传感器收集100个测量值。因此,对于每个动作,我都有4x100 = 400个数据点。在一个单独的文件中,每个动作都有标签对应。我的数据集如下所示:
measurement ID | action ID | sensor 1 | sensor 2 | sensor 3 | sensor 4 |
-----------------------------------------------------------------
1 | 1 | 42.3 | 42.3 | 42.3 | 42.3 |
2 | 1 | 42.3 | 42.3 | 42.3 | 42.3 |
3 | 1 | 42.3 | 42.3 | 42.3 | 42.3 |
... | .... | .... | .... | .... | .... |
100 | 1 | 42.3 | 42.3 | 42.3 | 42.3 |
1 | 2 | 42.3 | 42.3 | 42.3 | 42.3 |
2 | 2 | 42.3 | 42.3 | 42.3 | 42.3 |
3 | 2 | 42.3 | 42.3 | 42.3 | 42.3 |
... | .... | .... | .... | .... | .... |
100 | 2 | 42.3 | 42.3 | 42.3 | 42.3 |
... | .... | .... | .... | .... | .... |
我的问题是如何重塑此数据集以在Keras中应用convd1。还有如何为一堆矢量分配标签。请注意,我的数据集由10,000个动作组成。我的假设是我有4个通道(尺寸),每个通道有100个值的向量,因此我的输入形状应为(maxlen = 100,尺寸= 4)。也许我完全错了。
该模型应如下所示:
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=5 activation='relu',input_shape=(100,4)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.1, momentum=0.9, decay=0, nesterov=False)
model.compile(loss='binary_crossentropy', optimizer=sgd)
model.fit(trainX, trainY, validation_data=(testX, testY), epochs=100, batch_size=100)
谁能指出这是正确的方法?
最佳答案
使用传感器的数量似乎是合乎逻辑的,并且应该不是问题,并且考虑多个测量的尺寸也似乎是正确的。因此,您可以尝试训练此模型并检查结果。
我推荐的另一种方法是对所有传感器使用不同的卷积。因此,您将有4个卷积,每个卷积接受来自一个传感器的形状(100, 1)
的输入。 Keras代码看起来像
from keras.layers import Input, Conv1D, Dense, concatenate, Flatten
from keras.models import Model
s1_input = Input((100, 1))
s2_input = Input((100, 1))
s3_input = Input((100, 1))
s4_input = Input((100, 1))
conv1 = Conv1D(filters=64, kernel_size=5, activation='relu')(s1_input)
conv2 = Conv1D(filters=64, kernel_size=5, activation='relu')(s2_input)
conv3 = Conv1D(filters=64, kernel_size=5, activation='relu')(s3_input)
conv4 = Conv1D(filters=64, kernel_size=5, activation='relu')(s4_input)
f1 = Flatten()(conv1)
f2 = Flatten()(conv2)
f3 = Flatten()(conv3)
f4 = Flatten()(conv4)
dense_in = concatenate([f1, f2, f3, f4])
output = Dense(1, activation='sigmoid')(dense_in)
model = Model(inputs=[s1_input, s2_input, s3_input, s4_input], outputs=[output])
还有另一种RNN方式,您将100次测量视为时间步长,并在每个步骤中输入4个传感器的数据。但是,我高度怀疑这种方法能否优于CNN方法。
关于python - keras conv1d输入数据重塑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49877422/