gpt4 book ai didi

python - keras conv1d输入数据 reshape

转载 作者:行者123 更新时间:2023-12-01 09:32:04 24 4
gpt4 key购买 nike

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

为了监控每个 Action 的行为,有 4 个传感器收集 100 个测量值。因此,对于每个 Action ,我有 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 |
... | .... | .... | .... | .... | .... |

我的问题是如何 reshape 此数据集以在 Keras 中应用 convd1。以及如何为一堆向量分配标签。请注意,我的数据集由 10,000 个操作组成。我的假设是我有 4 个 channel (维度),每个 channel 有 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输入数据 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49877422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com