Multi class classification problem


(Nikolay Tolstokulakov) #1

I am trying adapt ANPR sample from https://matthewearl.github.io/2016/05/06/cnn-anpr/ to Keras. It is multi class classification problem. As I can understand I should use loss=‘sparse_categorical_crossentropy’ for model and class_mode=’sparse’ for ImageDataGenerator. Unfortunately I can’t find documentations and samples about these Keras features. E.g. Keras says: “sparse” will be 1D integer labels. And it is all about class_mode=’sparse’ what I can find. Is integer label encoded with bit mask? Can I use bit mask with length above 32 or 64 bits?
Can somebody clarify these features (loss=‘sparse_categorical_crossentropy’ for model and class_mode=’sparse’ for generator)? Or may be I am wrong, and should I use something else for this task?


(Jeremy Howard) #2

Just use categorical_crossentropy and one hot encoding for now. We explain the sparse version in a later lesson.


(Nikolay Tolstokulakov) #3

Ok thanks


(tursun) #4

@jeremy,
I am Tursun, a student in Malaysia. I have seen your posts in another thread. But I think this thread is right place.

I have this multi-class classification problem.
my data : tursun.csv , available to download from here :


size of my data set : 512*16, last column is 21 classes, they are digits 1-21 (so maybe OneHotEncoding is not needed)
note: number of samples (rows in my data) for each class is different. mostly 20 rows, but sometimes 17 or 31 rows
my network has:
first layer (input) has 15 neurons
second layer (hidden) has 30 neurons
last layer (output) has 21 neurons
in last layer I used “softmax” based on this recommendatins from
https://github.com/fchollet/keras/issues/1013
"The softmax function transforms your hidden units into probability scores of the class labels you have; and thus is more suited to classification problems "
I got error message when execute it in Keras in Anaconda in Ubuntu 16.04:
alueError: Error when checking model target: expected dense_8 to have shape (None, 21) but got array with shape (512, 1)
------------- keras code ----------
from keras.models import Sequential
from keras.layers import Dense
import numpy

fix random seed for reproducibility

numpy.random.seed(7)

load pima indians dataset

dataset = numpy.loadtxt(“tursun_deep_p6.csv”, delimiter=",")

split into input (X) and output (Y) variables

X = dataset[:,0:15]
Y = dataset[:,15]

create model

model = Sequential()
model.add(Dense(30, input_dim=15, activation=‘relu’))
#model.add(Dense(25, activation=‘relu’)) # want to add another hidden layer, can I?
model.add(Dense(21, activation=‘softmax’))

Compile model

model.compile(loss=‘categorical_crossentropy’, optimizer=‘adam’, metrics=[‘accuracy’])

Fit the model

model.fit(X, Y, epochs=150, batch_size=5)

evaluate the model

scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
-------------keras code end ----

I have following questions:

  1. can I use CNN, RNN for this multi class classification problem (I know DBN is YES. One thing about DBN, quite hard to find code example in anywhere, somebody claims DBN is old method back to 2006,then what is good method for multi class classification excep “ctegorical_crossentropy”)?
    I ever used MLP,SVM, KNN,Random Forest for my above database, they are good (MLP , not DL algo).
    now I want to use Deep learning methods, I think if I use deep learning, accuracy would not easily drop even data table become big (population increases)

Thank you


MultiClass Classification using Dense Layers
(Jeremy Howard) #5

These questions are covered in the course - try completing the assignments from each lesson and hopefully you’ll find that you can resolve this yourself! :slight_smile:


(Nikolay Tolstokulakov) #6

I suppose your dataset is to small for effective using with CNN, RNN. Also it is not clear nature of parameters. CNN, RNN can be effectively used when parameters have structure (e.g. time series, geometric distribution like pixels in image)


MultiClass Classification using Dense Layers
(tursun) #7

@sibnick,
These are fluctuations on time. I am agree with you, maybe CNN, RNN are too far from this dataset.
Let me redefine : I want to classify my dataset. Any suggestions except categorical-crossEntrophy? SVM, KNN,MLP, Randor Forest, Naive Bayes classifier? I want to try something new.
Thank you