0

I am trying to build a multi-class classifier using Keras. I am not quite sure I have implemented it correctly. Data is like this

label time-series variables [0:25728}

index 0  1   2   3   4            25728
  0   1  2.5 3.2 1.6 1.05 ........ 2.54
  1   5  3.2 1.6 1.5 1.49 ........ 1.41
  2   1  2.3 3.2 1.5 1.52 ........ 2.11
  3   3  0.2 3.1 1.5 1.89 ........ 0.81
  4   8  1.2 1.1 0.2 1.19 ........ 3.71
  .   5  .    .   .   .   ........   .
  .   7  .    .   .   .   ........   .
1323  5  .    .   .   .   ........   .

Here is the code. I split data by 68 % then reshaping 1D array to a 2D array. as 384*67 = 25728 So forming an image of vector 384 by 67 for one label

x_train =x_train.reshape(x_train.shape[0],384,67)
x_test =x_test.reshape(x_test.shape[0],384,67)

train_label_y = df_train_y[2].values
test_label_y = df_test_y[2].values

train_Y = np.array(train_label_y[:], dtype=int)
test_Y  = np.array(test_label_y[:], dtype=int)

Y_train = convert_to_one_hot(train_Y, C = 8)
Y_test  = convert_to_one_hot(test_Y,  C = 8)

x_train = x_train.reshape(x_train.shape + (1,))
x_test = x_test.reshape(x_test.shape + (1,))

input_shape = x_train.shape[1:]
model = Sequential()
model.add(Conv2D(2, kernel_size=(3, 3), padding='same', input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, kernel_size=(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, kernel_size=(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(8, kernel_size=(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(GlobalAveragePooling2D())
model.add(Dense(8, activation='sigmoid'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
hist = model.fit(x_train, Y_train, batch_size=batch_size, epochs=nb_epochs, verbose=1)
score = model.evaluate(x_test, Y_test)
print("Accuracy: %.2f%%" % (score[1] * 100))

It gives 96.16% accuracy but I don't believe it is true. I want to predict the labels.

  1. How can I predict labels?
  2. What I am doing wrong?

Please help! Thank you.

Ishan Khatri
  • 555
  • 5
  • 10

1 Answers1

0

The reason you are getting weird results is you should use the 'softmax' activation in your last layer.

JHall651
  • 427
  • 1
  • 4
  • 15