Keras 中的多步多元时间序列分类

Posted

技术标签:

【中文标题】Keras 中的多步多元时间序列分类【英文标题】:Multistep Multivariate Time Series Classification in Keras 【发布时间】:2021-07-12 03:22:50 【问题描述】:

我想知道你是否可以帮助我,我有一个 3 类时间序列分类问题,我需要预测未来最多两个步骤的类。 我用一个热编码器对输出进行了编码,所以我有 3 列 我对输出层的维度有疑问。当我向未来迈出一步时,我可以使用

"model.add(Dense(3,activation='softmax'))"

而且我运行的代码还可以,但是当我有 2 个步骤时,我不确定要使用哪个输出层。

我的训练数据的维度如下:

ytrain2 (100861, 2, 3) xtrain2 (100861, 6, 9)


from numpy import array
print(xnorm.shape)
Xtrain2, ytrain2 = split_sequence(xnorm,ynorm,6,2)
print(ytrain2)
Xtestf, ytestf = split_sequence(xtestnorm,ytestnorm,6,2)
print(ytrain2.shape)
recallV=list()
misclasV=list()
for i in n:
  for j in neurons:
    from keras.layers import Dense
    from keras.models import Sequential
    from keras.layers import Dropout
    import tensorflow as tf
    import keras as ks
    n_input = Xtrain2.shape[1] * Xtrain2.shape[2]
    ny=ytrain2.shape[1] * ytrain2.shape[2]
    X = Xtrain2.reshape(Xtrain2.shape[0],n_input)
    ytrain3=ytrain2.reshape(ytrain2.shape[0], ny)
    optimizer = ks.optimizers.Adam(lr=0.01)
    from keras.callbacks import ReduceLROnPlateau
    from keras.callbacks import EarlyStopping
    callbackEarlyStop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)

    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.95,
                              patience=5, min_lr=0.0000001,verbose=1)
    # define model
    model = Sequential()
    model.add(Dense(j, activation='relu', input_dim=n_input))
    model.add(Dropout(0.1))
    model.add(Dense(j, activation='relu'))
    model.add(Dropout(0.1))
    ###problem is here##
    model.add(Dense(3,activation='softmax'))**
    model.compile(optimizer=optimizer, loss='categorical_crossentropy',metrics=['accuracy'])

    model.fit(X, ytrain3,epochs=100,batch_size=256,shuffle=False,validation_split=0.2,callbacks=[reduce_lr,callbackEarlyStop], verbose=2)

【问题讨论】:

【参考方案1】:

您好,我找到了答案,问题只是通过使用 Keras 功能 API 和做一个多输出神经网络来解决,代码的摘录是:

 y1 = ytrain2[:, 0].reshape((ytrain2.shape[0], ytrain2.shape[1]))
 y2 = ytrain2[:, 1].reshape((ytrain2.shape[0], ytrain2.shape[1]))
 y3 = ytrain2[:, 2].reshape((ytrain2.shape[0],ytrain2.shape[1]))
 
 Input_1= Input(shape=(n_input,))

 x = Dense(120, activation='relu')(Input_1)
 x=  Dropout(0.15)(x)
 x = Dense(120, activation='relu')(x)
 x=  Dropout(0.15)(x)
 x = Dense(120, activation='relu')(x)
 x=  Dropout(0.15)(x)

 out1 = Dense(3,activation='softmax')(x)
 out2 = Dense(3,activation='softmax')(x)
 out3 = Dense(3,activation='softmax')(x)
 model = Model(inputs=Input_1, outputs=[out1, out2, out3])
 model.compile(optimizer=optimizer, loss='categorical_crossentropy',metrics=rec)
 model.fit(X, [y1,y2,y3],epochs=1054,batch_size=1024,shuffle=True,validation_split=0.12,callbacks=[reduce_lr,callbackEarlyStop], verbose=2  

【讨论】:

以上是关于Keras 中的多步多元时间序列分类的主要内容,如果未能解决你的问题,请参考以下文章

Keras每一步的多步LSTM批量训练分类

如何为LSTM Keras中的多步和多变量准备时间序列数据

如何处理keras中多变量LSTM的多步时间序列预测

Keras 中的多分类预测不止一个?

如何在 keras 中进行深度学习中的多标签分类?

为啥对于 Keras 中的多类分类, binary_crossentropy 比 categorical_crossentropy 更准确?