Build LSTM model for one inupt and two outputs

165 views Asked by At

I want to build a LSTM model with one input and two outputs. My data is same as figure. My model is as below. But it only predict one output. Data set is here

could you help me to design the model for two outputs? thanks

s1 = MinMaxScaler(feature_range=(-1,1))
Xs = s1.fit_transform(train[['y1','y2','x']])

s2 = MinMaxScaler(feature_range=(-1,1))
Ys = s2.fit_transform(train[['y1', 'y2']])

window = 70
X = []
Y = []
for i in range(window,len(Xs)):
    X.append(Xs[i-window:i,:])
    Y.append(Ys[i])

X, Y = np.array(X), np.array(Y)


model = Sequential()
model.add(LSTM(units=50, return_sequences=True,input_shape=(X.shape[1],X.shape[2])))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics = ['MAE'])

es = EarlyStopping(monitor='loss',mode='min',verbose=1,patience=10)
history = model.fit(X, Y, epochs = 10, batch_size = 250, callbacks=[es], verbose=1)
2

There are 2 answers

0
mb0850 On BEST ANSWER

The output_shape of the last layer of your model should match the shape of your Y-data.

Since you have 2 Y-data, you can change the last Dense layer to have 2 units:

model.add(Dense(units=1))

model.add(Dense(units=2))
1
Morteza Akbari On

You should use the Functional API

For example:

input = Input(shape=(shape, ))

out1 = Dense(1,  activation='linear')(input)
out2 = Dense(1,  activation='linear')(input)
out3 = Dense(1,  activation='linear')(input)

model = Model(inputs=input, outputs=[out1,out2,out3])