首页 > 解决方案 > 时间序列多类分类数据的损失和准确度常数

问题描述

我有具有多类分类的可变长度时间序列数据。头部看起来像:

0   DR_24526    1   -0.261916   0.377803    1.617511    0.311707    -0.055292   0   0.740317    0   4   1.810690    -0.375699   -1.025374   0   0.806782    0.529635    -0.577077
1   DR_24526    1   0.484744    -0.262327   -0.447281   -0.497518   -0.526008   0   0.740317    0   4   1.810690    -0.618167   -1.353477   0   0.806782    0.529635    -0.577077
2   DR_24526    1   0.484744    0.484492    2.415695    1.882432    -0.565707   0   0.740317    0   4   1.810690    -0.618167   -1.353477   0   0.806782    0.529635    -0.577077
3   DR_24526    2   0.058081    0.591180    -0.415251   -0.512043   0.131860    0   0.740317    0   4   1.810690    -0.618167   -1.353477   0   0.806782    0.529635    -0.577077
4   DR_24526    1   0.591409    0.484492    1.185172    2.287045    -0.350199   0   0.740317    0   4   1.810690    -0.618167   -1.353477   0   0.806782    0.529635    -0.577077

第一列是 ID,其组的长度不同。我已经填充和截断以使它们的长度相等。

sequences = list()

for name, group in tqdm(train_df.groupby(['ID'])):
    sequences.append(group.drop(columns=['ID']).values)

#Padding the sequence with the values in last row to max length
to_pad = 112
new_seq = []
for one_seq in sequences:
    len_one_seq = len(one_seq)
    last_val = one_seq[-1]
    n = to_pad - len_one_seq
   
    to_concat = np.repeat(one_seq[-1], n).reshape(17, n).transpose()
    new_one_seq = np.concatenate([one_seq, to_concat])
    new_seq.append(new_one_seq)
final_seq = np.stack(new_seq)

#truncate the sequence to length 60
# from tf.keras.preprocessing import sequence
seq_len = 16
final_seq=tf.keras.preprocessing.sequence.pad_sequences(final_seq, maxlen=seq_len, padding='post', dtype='float', truncating='post')

在另一个 df 中有一个目标列,其中包含 3 个类 0、1、2,其类数与 ID 相同

target = pd.get_dummies(train['DrivingStyle'])
target = np.asarray(target)

这是我的模型代码

model = tf.keras.models.Sequential()
model.add(L.Bidirectional(L.LSTM(64, dropout=0.2, input_shape=(seq_len, 17), return_sequences=True)))
model.add(L.Bidirectional(L.LSTM(64, dropout=0.2)))
model.add(L.Dense(3, activation='softmax'))

# adam = tf.optimizers.Adam(lr=0.1, clipvalue=0.5)
# adam = tf.keras.optimizers.Adam(lr=0.001, clipvalue=0.8)
# sgd = tf.keras.optimizers.SGD(lr=1)
sgd = tf.keras.optimizers.SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

model.fit(
    final_seq,
    target,
    epochs=10,
    batch_size=84,
    callbacks=[
        tf.keras.callbacks.ReduceLROnPlateau(patience=5),
    ]
)

But my loss and accuracy are levelling to a constant value

Epoch 1/10
155/155 [==============================] - 2s 11ms/step - loss: 1.1425 - accuracy: 0.3136
Epoch 2/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0670 - accuracy: 0.4461
Epoch 3/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0505 - accuracy: 0.4810
Epoch 4/10
155/155 [==============================] - 2s 10ms/step - loss: 1.0463 - accuracy: 0.4882
Epoch 5/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0451 - accuracy: 0.4889
Epoch 6/10
155/155 [==============================] - 2s 14ms/step - loss: 1.0437 - accuracy: 0.4904
Epoch 7/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0438 - accuracy: 0.4905
Epoch 8/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0426 - accuracy: 0.4920
Epoch 9/10
155/155 [==============================] - 2s 13ms/step - loss: 1.0433 - accuracy: 0.4911
Epoch 10/10
155/155 [==============================] - 2s 11ms/step - loss: 1.0419 - accuracy: 0.4909

我已经尝试过类似问题的其他解决方案。我尝试了 3 个具有 256 个节点的隐藏 LSTM 层,但它们都没有工作。

数据形状

print(final_seq.shape)
print(target.shape)
(12994, 16, 17)
(12994, 3)

标签: pythonmachine-learningdeep-learningtime-seriesdata-science

解决方案


更新答案 因此,您的数据形状看起来不错。有些东西,我会改变,这可能会改善结果:

  1. 将您的 batch_size 降低到 16 或 32,因为较低的 batch_size 会提高准确性
  2. 再次使用 Adam 作为优化器,我不知道您的自定义 SGD 设置是否对模型产生了不好的影响
  3. 在数据进入模型之前检查您的数据。可能存在一些预处理问题,我们在这里看不到。
  4. 也许,您刚刚拥有的数据不足以进行适当的分类。您可以考虑减少类数量来测试一个类是否导致问题,因为它的功能与其他类混合在一起。
  5. 由于某些功能不会随时间变化,您可以尝试使用 CNN。也许并非所有特征都与此分类相关,因此使用较少的特征可能会更好

推荐阅读