问题
I am trying to run a RNN/LSTM network on some time series sets. It should be mentioned that the time series are being classified. I have ~600 different time series, and each of these has 930 timesteps with features in them. I have structured my data into a numpy 3D array that is structured like:
X = [666 observations/series, 930 timesteps in each observation, 15 features]
Y = [666 observations/series, 930 timesteps in each observation, 2 features]
For training and validation data I split the data 70/30. So Train_X = [466, 930, 15] and Train_Y = [200, 930, 2].
My network is getting an error that is saying that it expected the input to be 2 dimensions and that it got an array with shape (466, 930, 2). My code is as follows:
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Bidirectional
Train_X = new_ped_data[0:466]
Test_X = new_ped_data[466:]
Train_Y = new_ped_valid_data[0:466]
Test_Y = new_ped_valid_data[466:]
model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True),
input_shape=Train_X.shape[1:]))
model.add(Bidirectional(LSTM(10)))
model.add(Dense(5))
model.compile(loss='mae',
optimizer='rmsprop')
model.fit(Train_X, Train_Y, epochs = 30, batch_size = 32,
validation_data =(Test_X, Test_Y))
I am just trying to get the model running. Once I do, then I'll tweak the architecture and fit parameters. I should mention that one of the classification outputs might not be necessary. Any suggestions as to how I set up the architecture so that if a time series is fed in I will get the classification values of the network for each timestep?
Error was: ValueError: Error when checking target: expected dense_9 to
have 2 dimensions, but got array with shape (466, 930, 2)
回答1:
Your output has also sequential nature. LSTM
by default has a flag return_sequences=False
. This makes your sequence to be squashed to a vector after the second LSTM
layer. In order to change that try:
model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True),
input_shape=Train_X.shape[1:]))
model.add(Bidirectional(LSTM(10, return_sequences=True)))
model.add(Dense(5))
model.compile(loss='mae',
optimizer='rmsprop')
来源:https://stackoverflow.com/questions/48253348/rnn-time-series-predictions-with-multiple-time-series-dimension-with-keras-tens