1
votes

I am trying to make a binary classification on a subset of MNIST dataset. The goal is to predict whether a sample is 6 or 8. So, I have 784 pixel features for each sample and 8201 samples in the dataset. I built a network of one input layer, 2 hidden layers and one output layer. I am using sigmoid as activation function to output layer and relu for the hidden layers. I have no idea why I am getting a 0% accuracy at the end.

#import libraries
from keras.models import Sequential
from keras.layers import Dense
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import os
np.random.seed(7)

os.chdir('C:/Users/olivi/Documents/Python workspace')

#data loading
data = pd.read_csv('MNIST_CV.csv')

#Y target label
Y = data.iloc[:,0]

#X: features
X = data.iloc[:,1:]

X_train, X_test, y_train, y_test = train_test_split(X, Y,test_size=0.25,random_state=42)

# create model
model = Sequential()
model.add(Dense(392,kernel_initializer='normal',input_dim=784, 
activation='relu'))
model.add(Dense(196,kernel_initializer='normal', activation='relu'))
model.add(Dense(98,kernel_initializer='normal', activation='relu'))
model.add(Dense(1, activation='sigmoid'))


model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()


# Training the model
model.fit(X_train, y_train, epochs=100, batch_size=50)

print(model.predict(X_test,batch_size= 50))

score = model.evaluate(X_test, y_test)
print("\n Testing Accuracy:", score[1])
2

2 Answers

2
votes

If you use binary cross-entropy, your labels should be either 0 or 1 (representing "is not number 6" or "is number 6" respectively).

If your Y target labels right now are the values 6 and 8, it'll fail.

0
votes

Once you are choosing a subset of MNIST, you have to be sure how many different classes of digits there is in your sample (both training and test set).

So:

classes=len(np.unique(Y))

Then you should hot encode Y:

Y_train = np_utils.to_categorical(y_train, classes)
Y_test = np_utils.to_categorical(y_test, classes)

After that, change the last layer of your neural net to:

model.add(Dense(classes, activation='sigmoid'))

Finally:

model.predict_classes(X_test,batch_size= 50)

Be sure both training and test set have the same number of classes for Y.

After the prediction, find where 6 and 8's are located using np.where(), select this subsample and test your accuracy.