Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

machine learning - How can I input data to LSTM prediction model in Python in an appropriate form?

I would like to input time-series data to LSTM prediction model. In my case, The Starting length of input data is (53,2) and the length of output data is (53,).

Because I wanted to design many-to-one LSTM prediction model, I would like to input two features. But it has error when I execute this code.

The training result just show

Epoch 1/50 
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2). 
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2).
WARNING:tensorflow:Model was constructed with shape (None, 3, 2) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3, 2), dtype=tf.float32, name='lstm_1_input'), name='lstm_1_input', description="created by layer 'lstm_1_input'"), but it was called on an input with incompatible shape (None, 1, 2).
2/2 - 1s - loss: nan - val_loss: nan
Epoch 2/50 ...

And it's seem to have a problem in the shape of data inputted to LSTM. How can I solve this? When I saw this problem, I've tried rearranging data set. Saying, changing the numbers of reshape code X=np.array(X).reshape(53,1,2) to X=np.array(X).reshape(53,2,1). ...

The code written below is the part of my code.

X = np.array(X).reshape(53, 1, 2)
y = y.reshape(53,1,1)


X_train, X_test, y_train, y_test = X[:42], X[43:53], y[:42], y[43:53] 
X_train_valid, y_train_valid = X[33:42], y[33:42]


X_train = X_train.reshape(42,1,2)
y_train = y_train.reshape(42,1,1)


X_train_valid = X_train_valid.reshape(9,1,2)
y_train_valid = y_train_valid.reshape(9,1,1)

X_test = X_test.reshape(10,1,2)
y_test = y_test.reshape(10,1,1)

model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(3,2)))
model.add(Dense(1))
model.summary()
model.compile(optimizer='adam', loss='mse')

history = model.fit(X_train, y_train, epochs = 50, verbose = 2, validation_data = (X_train_valid, y_train_valid))

question from:https://stackoverflow.com/questions/65868104/how-can-i-input-data-to-lstm-prediction-model-in-python-in-an-appropriate-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...