rere
rere

Reputation: 11

good accuracy , but bad prediction with keras

I need help with this please, I using keras classifier the problem is that I got high accuracy but very bad prediction with the same data , the prediction classes should be 0,1,2,3,3,4,0. but I got all zeros, here is my code

from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers
model = Sequential()
model.add(Dense(units=14, activation='relu', input_shape=(14,)))
model.add(Dropout(0.5))
model.add(Dense(units=14, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=14, activation='relu'))
model.add(Dense(units=5, activation='sigmoid'))
#model.add(Dense(units=5, activation='relu'))
#monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=0, mode='auto')
#checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) # save best model  
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.001, nesterov=True)
model.compile(optimizer=sgd,
               loss='categorical_crossentropy',
               metrics=['accuracy'])

model.fit(x_vals_train,y_train,validation_data=(x_vals_test,y_test),verbose=0,epochs=10)
#Evaluate the accuracy of our trained model
score = model.evaluate(x_vals_test, y_test,
                       batch_size=32, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
27134/27134 [==============================] - 1s 29us/step
Test score: 0.10602876026708943
Test accuracy: 0.9448293653718581


the predition
testset = np.loadtxt('G:/project/test.pcap_z/all_data_amount_7.csv', delimiter=',')

xtest = testset[:,0:14]

#x_test1 = np.nan_to_num(normalize_cols(xtest))

y_pred = model.predict(x_test1)
y_pred =y_pred.astype(int)
y_pred

array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

please ,any help will be really appreciated.

Upvotes: 1

Views: 1032

Answers (2)

Luluz
Luluz

Reputation: 105

It could be y_pred = y_pred.astype(int) that erased all the data because the when float get turned into int it always rounds down.

Upvotes: 0

Primusa
Primusa

Reputation: 13498

For classification you should be using one-hot vectors as your y and softmax activation as the last layer in your neural network.

To convert your y to one-hot format:

from keras.np_utils import to_categorical
y = to_categorical(y)

And add another Dense layer with softmax activation:

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

Now instead of outputting a strict class it will output a vector of the probability of each class. To retrieve a prediction you can use np.argmax() on outputted vectors, which gets the class that has the highest probability.

Upvotes: 1

Related Questions