0

I want to implement a Neural Network for MNIST dataset, and I find a lot of example on the net. But I want study this problem using a different approach: I want create 10 NNs (as the number of classes) in which I classify only a class with the rest of the others (example: first NN analyze only the "1" class vs the others). It's just an exercise (I'm python newbie and I want to learn).

This is my actually python code, do you have any suggests to modify mi code in order to obtain the classes separation that I explained above?

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
import numpy
from keras.utils import np_utils
numpy.random.seed()


(X_train, y_train), (X_test, y_test) = mnist.load_data()

num_pixels = X_train.shape[1] * X_train.shape[2]
X_train.shape[1],X_train.shape[2],X_train.shape[3])
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')

X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
print(y_test)
num_classes = y_test.shape[1]
def baseline_model():
model = Sequential()
model.add(Dense(num_pixels, input_dim=num_pixels,           kernel_initializer='normal', activation='relu'))
model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))        

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model


 model = baseline_model()

 model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=3,     batch_size=200, verbose=2)

 model.summary()
 scores = model.evaluate(X_test, y_test, verbose=1)
 print("Baseline Error: %.2f%%" % (100-scores[1]*100))

1 Answer 1

1

First you should transform y_train and y_test into binary outputs. You will want to do this in a loop for each of the ten classes, but here is how you would do it for one of those, say 5:

y_train_binary = (y_train == 5).astype(np.int)
y_test_binary = (y_test == 5).astype(np.int)

Right now each y_train example is a number from 1 to 10, so here we convert it to 0 or 1 corresponding to whether the positive class is correct.

Then you want to adjust the output of your model to support binary classification. This can be done by using a single dense output neuron with sigmoid activation and binary crossentropy loss:

model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))   
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you very much Imran! i dont't understand why I use 5...can you explain me? Furthermore, np.int was marked as "undefined", I must import any other library? Sorry for my bad question, I'm a newbie for Python
That was just an example - each entry in y_train is a number between 1 and 10. In my example we convert 5 to array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0]). You should wrap everything below load_data() in a loop, outputting a different model for each class.
Thanks, and for the np.int?
(y_train == 5) gives you an array of booleans, and astype(np.int) converts it to 1s and 0s.
Perfect I understand, but my editor mark np as undefined. Why? It's necessary import a library?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.