I'm using keras. I have one classification problem. The output should be either 0 or 1. I trained my model and I'm getting 86.59 accuracy. But when i check the predicted output what I'm seeing is all ones. I tried creating a categorical classifier with two nodes and tried the same. The test accuracy is 86.59% but when I check the output the prediction contains only one node with value one for the entire dataset.

This is the code

from keras.models import Sequential

model = Sequential()
from keras.utils import to_categorical
y_traine = to_categorical(y_train)
y_teste = to_categorical(y_test)

from keras.layers import Dense
x_train = x_train.reshape(1300,64)
model.add(Dense(units=64, activation='relu', input_dim=64))
model.add(Dense(units=2, activation='softmax'))
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_traine, epochs=50, batch_size=32)
x_test = x_test.reshape(len(x_test),64)
loss_and_metrics = model.evaluate(x_test, y_teste, batch_size=128)
classes = model.predict(x_test, batch_size=128)
print (loss_and_metrics)
print (classes)


[0.32096896952051834, 0.875968992248062]

[[0.84422934 0.1557707 ]
 [0.8332991  0.16670085]
 [0.86778754 0.13221247]
 [0.9261704  0.07382962]
 [0.85143256 0.14856751]

What I'm doing wrong here? Why I'm getting training accuracy as 86% if my predictions are wrong?


As with most problems like this, it is always best to see the dataset upfront to gain a full understanding.

That said, if your categorical dependent variable is between 0 and 1, have you ensured that the independent variables in your dataset have also been scaled in this way? From looking at your code, it doesn't look like this is the case.

If your data has not been transformed to a common scale, then the neural network won't necessarily give you accurate results.

In this regard, you might try scaling your x data with MinMaxScaler if you haven't done so already and see what you com up with.

For instance, suppose you have variables x1, x2, and x3.

import numpy as np    
from sklearn.preprocessing import MinMaxScaler


x_train,x_test,y_train,y_test = train_test_split(x_scaled,y,test_size=0.2)

Essentially, you are scaling the x variables between 0 and 1, so that the x variables now have the same scale as the y variable. It might be an idea to try this if you haven't already and see what you come up with.

  • $\begingroup$ Yes, my data are normalized between 1 and 0. $\endgroup$ Sep 26 '18 at 5:58

Did you check how many samples are there for each class. I suspect imbalance class problem here. If you have majority of images with class=1. Classifier will be biased towards predicting everything as class 1. Try to balance your classes by either duplicating minor class images or deleting major class images.

  • $\begingroup$ Thanks for the help. I think it is the problem here. I will try the steps you suggested. $\endgroup$ Sep 25 '18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.