vlfeat / matconvnet

MatConvNet: CNNs for MATLAB
Other
1.4k stars 753 forks source link

How can I do binary classification? #47

Closed johnny5550822 closed 8 years ago

johnny5550822 commented 9 years ago

I saw that Matconvnet allow me to choose errortype as 'binary'. Is that the only parameter tht I need to change if I want to do binary classification (my label is 1 or 2).

lenck commented 9 years ago

Hi, based on the cnn_train.m example for training, you need to have labels +-1. But as it is an example training code, there is nothing easier to change the line 274 in cnn_train.m to:

 error = bsxfun(@eq, predictions, labels);

And then you should be able to use 1,2 labels. It's just that it is sort of a common practice to have binary task with labels +-1...

johnny5550822 commented 9 years ago

I see. Great. Thanks!

lenck commented 9 years ago

Hmm, I just realised that I got it slightly wrong :) With the binary classification, the network actually has a single output - that's why you multiply with the label directly... But I just found, that actually having 2-class softmax is the same as logistic regression so in order to use what I've proposed, you will need to have softmaxloss layer at the end with two outputs. But that might have actually been your case, so I hope that it worked :)

johnny5550822 commented 9 years ago

Hmm, so should I still use -1/+1?

lenck commented 9 years ago

I think the easiest would be to use the softmax with two outputs and two labels 1 and 2, then you just need to remove the top5 error evaluation in updateError in cnn_train (as you will have just 2 labels)...

It's actually worth to fix this - to be able to set the n for the error measures...

johnny5550822 commented 9 years ago

Great. Or actually I have data only with 2 labels (1 or 2) and I still set my output as having 5 distinct labels to train the network. I think this will still be ok, am I right?

lenck commented 9 years ago

Hmm, I'm not sure. At least the last layer would be slower, because you would have more parameters to learn (in this case to learn that they should be zero) - so you would deliberately make your problem more complicated. But it's really just about commmenting out lines {271, 272} in cnn_train.m :)

johnny5550822 commented 9 years ago

ok, great! Thanks.

johnny5550822 commented 9 years ago

I comment the {271,272} lines and keep label as 1/2, the output of the error plot look weird to me (it bigger than 1!?)

screenshot from 2015-01-28 14 41 11

cheer37 commented 8 years ago

have you solved your problem? I did binary classification with binary train type and 1/-1 labels. But result doesn't make sense and graph is like you. So, i did train with multiclass type and 1/2 labels. But it does not converge. How did you deal with binary classification with matconvnet?

anewlearner commented 8 years ago

@lenck I did just as what you said,but things seems to be wrong. In cifar, I changed the 10 in penultimate layers to be 2, and have the labels of 1,2. but the error didn't change in epochs. It always be obj:0.693 top1err:0.501. I also tried to use the lables of -1 and 1. And change the error to 'error = bsxfun(@eq, predictions, labels);' But I just get a large error such as 150. Look forward to your reply! Thanks!

Addhi86 commented 7 years ago

@johnny5550822 @lenck It would be helpful for many others to answer how solve Binary classification?

johnny5550822 commented 7 years ago

@Addhi86 Sorry, I have not used matconvnet for a while. Not sure how to handle this.