問題:我有T個時間步的S個序列,每個時間步包含F個特征,因此 (S x T x F) 的資料集和 S 中的每個 s 由 2 個值(Target_1和Target_2)描述
目標:使用 LSTM 對架構進行建模/訓練,以便學習/實作函式逼近器模型 M 并給定序列s,以預測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 與時間相關的記憶,以實作良好的回歸。
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452261.html
上一篇:無法更新python
