代码之家  ›  专栏  ›  技术社区  ›  mcanizo

keras conv1d输入数据重塑

  •  2
  • mcanizo  · 技术社区  · 6 年前

    我正在尝试使用一维CNN对Keras进行二元分类。我有一台机器可以连续执行一个动作,我的目标是对该动作是正常还是异常进行分类。

    为了监测每个动作的行为,有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   |
          ...      |   ....    |   ....   |   ....   |   ....   |   ....   |
    

    模型应如下所示:

    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)
    

    有人能告诉我实现这一目标的正确方法吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   layog    6 年前

    使用多个传感器似乎是合乎逻辑的,也不应该成为问题,而且考虑到多个测量值的大小也似乎是正确的。因此,您可以尝试训练此模型并检查结果。

    我推荐的另一种方法是对所有传感器使用不同的卷积。所以你有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方法。