0

I have to train a model to classify time-series data. There are 6 classes in this data, that is why I have encoded them with one-hot-encoder. The only input feature is the "ecg" column which consists of row vectors. The data looks like this;

                                               ecg      0  1  2  3  4  5
0    [[0.1912, 0.3597, 0.3597, 0.3597, 0.3597, 0.35...  1  0  0  0  0  0
1    [[0.2179, 0.4172, 0.4172, 0.4172, 0.4172, 0.41...  1  0  0  0  0  0
2    [[0.1986, 0.3537, 0.3537, 0.3537, 0.3537, 0.35...  0  1  0  0  0  0
3    [[0.2808, 0.5145, 0.5145, 0.5145, 0.5145, 0.51...  0  1  0  0  0  0
4    [[0.1758, 0.2977, 0.2977, 0.2977, 0.2977, 0.29...  0  0  1  0  0  0
5    [[0.2183, 0.396, 0.396, 0.396, 0.396, 0.396, 0...  0  0  1  0  0  0
6    [[0.204, 0.3869, 0.3869, 0.3869, 0.3869, 0.386...  0  0  0  1  0  0
7    [[0.1695, 0.2823, 0.2823, 0.2823, 0.2823, 0.28...  0  0  0  1  0  0
8    [[0.2005, 0.3575, 0.3575, 0.3575, 0.3575, 0.35...  0  0  0  0  1  0
9    [[0.1969, 0.344, 0.344, 0.344, 0.344, 0.344, 0...  0  0  0  0  1  0
10   [[0.2312, 0.4141, 0.4141, 0.4141, 0.4141, 0.41...  0  0  0  0  0  1
11   [[0.1862, 0.3084, 0.3084, 0.3084, 0.3084, 0.30...  0  0  0  0  0  1
12   [[0.2605, 0.47, 0.47, 0.47, 0.47, 0.47, 0.3814...  1  0  0  0  0  0
13   [[0.2154, 0.3733, 0.3733, 0.3733, 0.3733, 0.37...  1  0  0  0  0  0
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .

First of all, I have sliced the dataframe to have my train_x and train_y;

train_x = final_dataset.iloc[:,0] #the only input feature is the first column
train_y = final_dataset.iloc[:,1:] # rest of the columns are class labels

After that, I have created my neural network and added layers in it;

model = Sequential()
model.add(Dense(256, input_shape = (1,))) # there is only one input feature
model.add(Activation('relu'))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(3, activation='softmax'))

As you can see above, I have set the input_shape as 1, because there is only one input feature which is the ecg column. After all of that, I am starting to train my model;

adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)

model.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_x,train_y,epochs = 500, batch_size = 32, validation_split = 0.3)

I have used the categorical-crossentropy as my loss function. When I run my code, I am having the following error;

Error when checking target: expected dense_4 to have shape (3,) but got array with shape (6,)

I am pretty new to Keras, so I couldn't figure it out what causes the problem and How can I fix it. Any help is appreciated, thanks in advance.

Ozan Yurtsever
  • 1,274
  • 8
  • 32
  • 1
    The last layer must have 6 units, not 3. That's because there are 6 distinct classes in your classification task. – today Mar 26 '19 at 17:13
  • It corrected my previous error. But now I am taking a different error; `ValueError: setting an array element with a sequence.` – Ozan Yurtsever Mar 26 '19 at 17:15
  • Make sure the inputs are numpy arrays, not lists. Further, make sure the `input_shape` is set correctly since according to the description of the data you have provided, the input shape is not `(1,)`. – today Mar 26 '19 at 17:17
  • Actually when I use `iloc`, the input will be the type of Series. Is that causing the problem? – Ozan Yurtsever Mar 26 '19 at 17:29
  • No, that does not cause any problems. The thing that causes problems is that the individual elements in that series are lists, not numpy arrays. Convert them to numpy arrays and set the `input_shape` accordingly. Further, if the inputs are timeseries, and you want to classify the whole timeseries, not its individual timesteps, then with this architecture you need to at least have a `Flatten` layer somewhere in your model. That's because the [Dense layer is applied on the last axis](https://stackoverflow.com/a/52092176/2099607). – today Mar 26 '19 at 17:37

1 Answers1

0

The last output layer must have the same dimension as the number of differents classes. In your case you have 6 differents classes so you must have :

model.add(Dense(6, activation='softmax'))

Hope i helped !

Thibault Bacqueyrisses
  • 2,281
  • 1
  • 6
  • 18