SuperDave
SuperDave

Reputation: 413

LSTM Many to One Prediction Example in Keras

I would like to create an example of many to one prediction model for those new to Keras like me.

Given 4 sine waves 90 degrees out of phase from each other, use three to predict a fourth. The documentation is very unclear and examples are 90 % CNN and the rest are a form of classification too.

So X1 X2 and X3 would be zero, ninety, and two-hundred seventy out of phase. The label data would be One-Eighty out of phase with the first.

So how would one construct the Y? Would it be the same shape as the X, just the 270 degree phase data duplicated? Is there a way to have the Y data as a single input itself? Do you have to use look-back on the Y data? Can you hand I a single Y target for each X that has look-back?

I have seen line one in a posts that suggests you can use features==3 to concatenate the Y data prediction, but again no example(s).

Since I have seen this same question posted many different ways with no solution, I am willing to spend some time creating a best practices example and have created a dataset on Kaggle that could be used. (https://www.kaggle.com/superdave/test-driven-data/downloads/FourOutOfPhaseWaves.csv)

Is there an example or pointers that comes close?

Yes I want to use LSTM's or GRU's. I have tested many single input wave forms and have had excellent results. https://www.kaggle.com/superdave/test-driven-data. I want to post a solution but see that the single biggest missing example is many to one LSTM prediction. A toy example with sine waves should be a great addition to the community.

Upvotes: 0

Views: 2323

Answers (1)

DJK
DJK

Reputation: 9274

A short example of what you are trying to achieve.

x = np.random.rand(1000,3,1)
y = np.random.rand(1000,1)

x is your input data (3d array) containing 3 sine waves. y is your fourth wave in a 2d array.

Then a simple model to follow

model = Sequential()
model.add(LSTM(4,activation='relu',input_shape=(x.shape[1],x.shape[2])))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='rmsprop',loss='mse')
model.fit(x,y,epochs=30)

Upvotes: 1

Related Questions