2
$\begingroup$

I have a very small dataset, only about 40 rows, that has historical usage data for a few categories (roughly 20). I strongly suspect that these categories are dependent in a partial-zero-sum-game fashion: if the usage of one category goes up, I'm expecting that of another to go down. My goal is to predict the next row of all categories.

I've looked at this post, but it's not predicting multiple variables. I also looked at this post, but it's still univariate output (albeit multiple time steps) and multivariate input. So far, I've been basing my approach on the typical LSTM post here at machinelearningmastery, but it's also a single-output-variable example, and a number of the functions used, such as scaler.inverse_transform don't appear to broadcast very well. I'm even having difficulties trying to scale back my full example to match his!

Any tips for scaling LSTM's up to multivariate output? Can the keras LSTM do this natively? If so, how would the code change?

Thanks for your time!

$\endgroup$

1 Answer 1

2
$\begingroup$

You can simply change the number of units at the last layer if you want to predict multiple outputs (or just said differently, one output with multiple features). For example from the link you shared, you can change the units of the Dense layer:

model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(2))
model.compile(loss='mae', optimizer='adam')

The above model would now predict the next step of an output with 2 "features". Note that you output should be of shape num_samplesx2 now.

$\endgroup$
15
  • $\begingroup$ You wrote, "The above model would not predict..." Did you mean "The above model would now predict..."? $\endgroup$ Jan 19, 2019 at 20:31
  • $\begingroup$ Ah, gotcha. Thanks! I wouldn't have bothered, but it did change the meaning of the sentence a fair bit. I'll try it out! $\endgroup$ Jan 19, 2019 at 20:34
  • $\begingroup$ Yes, it certainly does :). $\endgroup$
    – SaTa
    Jan 19, 2019 at 20:35
  • $\begingroup$ I'm getting an error in the next line: history = model.fit(train_X, train_y, epochs=100, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False). The error is: ValueError: Error when checking target: expected dense_1 to have shape (17,) but got array with shape (1,). Any ideas? $\endgroup$ Jan 19, 2019 at 20:44
  • 1
    $\begingroup$ num_samples is like how many samples you have for training. num_observations is how long is each sample, or how many time steps you have in each sample. $\endgroup$
    – SaTa
    Jan 19, 2019 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.