0

I am training LSTM for multiple time-series in an array which has a structure: 450x801. There are 450 time series with each of 801 timesteps / time series. The labels are classes with assigned integer from 1 to 6, so the dimension of the label is 450x1. This is my implmentation:

This is my code:

def readData():
labels = pd.read_csv('label.csv', header = None)
labels = labels.values
data = pd.read_csv('data.csv', header = None)
return data, labels

data, labels = readData()

data_train, data_test, labels_train, labels_test = train_test_split(data, labels)

model = Sequential()
model.add(LSTM(units=32, input_shape = (450,801,1)))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

However, I get following errors:

Input 0 is incompatible with layer lstm_3: expected ndim=3, found ndim=4

Any idea how do I solve it?

user2754279
  • 115
  • 1
  • 3
  • 10
  • `input_shape = (450,801)` means each input sample is a timeseries of length 450 and each timestep has 801 features. That does not mean you have 450 timeseries. – today Aug 16 '19 at 09:16
  • For me its not clear what you mean by multiple time-series, do you want to train a model that fits all of these time series, or do you want to predict multiple time series from a single model? – Dr. Snoopy Aug 16 '19 at 09:29
  • There is only 1 feature in each of my timestep, it's a time-series from earthquake data. Let's say there are "different earthquake" which I classified into 6 categories. Any suggestion how do I implement this? – user2754279 Aug 16 '19 at 09:29
  • @user2754279 So to clarify: you have data of 450 earthquakes where each of them is a timeseries of length 801, and you want to classify each of earthquake timeseries into one of six categories. Is that right? – today Aug 16 '19 at 09:32
  • @ Matias: Sorry for confusion. So basically I have a very long time series. It's an earthquake data with a length of let's say 1000000 timesteps. And assume there are 6 categories of earthquakes. So, in total I have 6x1000000 timesteps. There's no way I can train this very long data. The only way I can do is to split these 1000000 timesteps into smaller piece of data consisting 800 timesteps. – user2754279 Aug 16 '19 at 09:34
  • @ today: That's correct. Each of those 450 earthquakes sample belong to the same class of probability. – user2754279 Aug 16 '19 at 09:35
  • @user2754279 Why do you want to classify 450 earthquakes at the same time? Are they related somehow? For example, what if instead you give your model timeseries of earthquake data of shape `(801,1)` to classify them? So your **whole training data** would be of shape `(number_of_samples, 801, 1)`. – today Aug 16 '19 at 09:41
  • @today: Yes, as stated above, the 450 earthquake samples x 801 timesteps belong to the same probability class which is coming from 1000000 timesteps. I know this is only 450x801 = 360.450 (36%) of my data, but of course I need to save some for the validation and test set. In the future I might increase the size to 500 samples x 1200 timesteps for instance. But maybe there is something wrong in my implementation? I post the code below. – user2754279 Aug 16 '19 at 09:58
  • @user2754279 Don't post **edits** or **updates** as *answers*. Instead, **edit your question** and add any new content at the end of it. There is nothing wrong with that implementation as long as the input training data and labels shape match with model's input and output shape. Run `model.summary()` and `print(data.shape)` and `print(labels.shape)` to find out. – today Aug 16 '19 at 10:03
  • So, print(data.shape) gives (600, 801) and print(labels.shape) gives (600,1). These are before the training / validation split. I think it was with 25% validation split. So with 450 training samples and 150 samples. – user2754279 Aug 16 '19 at 10:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198042/discussion-between-user2754279-and-today). – user2754279 Aug 16 '19 at 10:40
  • take a look at the TimeDistributed Layer – ixeption Aug 16 '19 at 11:07

1 Answers1

0

It's working now. The final code:

def readData():
labels = pd.read_csv('label.csv', header = None)
labels = labels.values
labels = to_categorical(labels)
data = pd.read_csv('data.csv', header = None)
return data, labels

data, labels = readData()
data = np.expand_dims(data, axis=-1)

data_train, data_test, labels_train, labels_test = train_test_split(data, labels)

model = Sequential()
model.add(LSTM(units=32, input_shape = (801,1)))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
user2754279
  • 115
  • 1
  • 3
  • 10