用于回归的 LSTM

原文标题LSTM used for regression

问题:我有T个时间步的序列,每个时间步都包含F个特征,所以一个数据集(S x T x F)和S中的每个s由2个值(Target_1和Target_2)描述

目标:使用 LSTM 对架构进行建模/训练,以便学习/实现函数逼近器模型 M 并给定序列,以预测Target_1和Target_2?

像这样的东西:

M(s) ~ (Target_1,Target_2)

我真的很难找到一种方法,下面是一个可能不起作用的示例的 Keras 实现。我为第一个目标值制作了 2 个模型,为第二个目标值制作了 1 个模型。

model1 = Sequential()
model1.add(Masking(mask_value=-10.0))
model1.add(LSTM(1, input_shape=(batch, timesteps, features), return_sequences = True))  
model1.add(Flatten())
model1.add(Dense(hidden_units, activation = "relu"))
model1.add(Dense(1, activation = "linear"))
model1.compile(loss='mse', optimizer=Adam(learning_rate=0.0001))
model1.fit(x_train, y_train[:,0], validation_data=(x_test, y_test[:,0]), epochs=epochs, batch_size=batch, shuffle=False)

model2 = Sequential()
model2.add(Masking(mask_value=-10.0))
model2.add(LSTM(1, input_shape=(batch, timesteps, features), return_sequences=True))
model2.add(Flatten())
model2.add(Dense(hidden_units, activation = "relu"))
model2.add(Dense(1, activation = "linear"))
model2.compile(loss='mse', optimizer=Adam(learning_rate=0.0001))
model2.fit(x_train, y_train[:,1], validation_data=(x_test, y_test[:,1]), epochs=epochs, batch_size=batch, shuffle=False)

我想以某种方式充分利用 LSTM 与时间相关的记忆,以实现良好的回归。

原文链接:https://stackoverflow.com//questions/71674203/lstm-used-for-regression

回复

我来回复
  • AloneTogether的头像
    AloneTogether 评论

    IIUC,您可以从使用两个输出层的简单(天真)方法开始:

    import tensorflow as tf
    
    timesteps, features = 20, 5
    inputs = tf.keras.layers.Input((timesteps, features))
    x = tf.keras.layers.Masking(mask_value=-10.0)(inputs)
    x = tf.keras.layers.LSTM(32, return_sequences=False)(x)
    x = tf.keras.layers.Dense(32, activation = "relu")(x)
    output1 = Dense(1, activation = "linear", name='output1')(x)
    output2 = Dense(1, activation = "linear", name='output2')(x)
    
    model = tf.keras.Model(inputs, [output1, output2])
    model.compile(loss='mse', optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001))
    
    x_train = tf.random.normal((500, timesteps, features))
    y_train = tf.random.normal((500, 2))
    model.fit(x_train, [y_train[:,0],y_train[:,1]] , epochs=5, batch_size=32, shuffle=False)
    
    2年前 0条评论