4
votes

I am trying to predict 2 features. This is how my model looks like:

Defining the model

def my_model():
    input_x = Input(batch_shape=(batch_size, look_back, x_train.shape[2]), name='input')
    drop = Dropout(0.5)

    lstm_1 = LSTM(100, return_sequences=True, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='3dLSTM', stateful=True)(input_x)
    lstm_1_drop = drop(lstm_1)
    lstm_2 = LSTM(100, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='2dLSTM', stateful=True)(lstm_1_drop)
    lstm_2_drop = drop(lstm_2)

    y1 = Dense(1, activation='relu', name='op1')(lstm_2_drop)
    y2 = Dense(1, activation='relu', name='op2')(lstm_2_drop)

    model = Model(inputs=input_x, outputs=[y1,y2])
    optimizer = Adam(lr=0.001, decay=0.00001)
    model.compile(loss='mse', optimizer=optimizer,metrics=['mse'])
    model.summary()
    return model

model = my_model()

for j in range(50):
    start = time.time()
    history = model.fit(x_train, [y_11_train,y_22_train], epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
    model.reset_states()
    print("Epoch",j, time.time()-start,"s")

p = model.predict(x_test, batch_size=batch_size)

My data set has 9 features:

x_train (31251, 6, 9)
y_11_train (31251,)
y_22_train (31251,)
x_test (13399, 6, 9)
y_11_test (13399,)
y_22_test (13399,)

I am trying to predict the first(y_11) and second(y_22) feature of my dataset. But I am getting prediction for only first feature not the second one.

Any help on how can I get both the predictions instead of one?

1

1 Answers

2
votes

First of all you should remove multiple inputs of the same thing:

(batch_size, look_back, x_train.shape[2])

Also, try to concatenate your ouputs inside your model like this:

def my_model():
    from keras.layers import concatenate

    lstm_1 = LSTM(100, return_sequences=True, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='3dLSTM', stateful=True)
    lstm_1_drop = drop(lstm_1)
    lstm_2 = LSTM(100, name='2dLSTM', stateful=True)(lstm_1_drop)
    lstm_2_drop = drop(lstm_2)

    y1 = Dense(1, activation='linear', name='op1')(lstm_2_drop)
    y2 = Dense(1, activation='linear', name='op2')(lstm_2_drop)

    y= concatenate([y1,y2])

    model = Model(inputs=input_x, outputs=y)
    optimizer = Adam(lr=0.001, decay=0.00001)
    model.compile(loss='mse', optimizer=optimizer,metrics=['mse'])
    model.summary()
    return model

EDIT I think you should fit like this:

y_11_train = y_11_train.reshape(y_11_train.shape[0],1)
y_22_train = y_22_train.reshape(y_22_train.shape[0],1)
model = my_model()
model.fit(x_train,np.concatenate((y_11_train,y_22_train),axis=1),...)