New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add svm in last layer #2588
Comments
I'm not sure linear in the correct activation on your output: I think it should be tanh so that you get labels [-1,+1]. |
@mundher Do you solve your problem? When i want to use the svm, i always call the |
@alyato I didn't solve the problem. it looks like the hinge loss works only for binary class output |
I tried a few different activations at the last layer, and found that the softmax activation works best with hinge loss. Can anyone explain this? |
You need to regularize the weight. The hinge loss with regularization term forms the complete SVM loss function. Try: from keras.regularizers import l2
model = Sequential()
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=(1, img_rows, img_cols)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes), W_regularizer=l2(0.01))
model.add(Activation('linear'))
model.compile(loss='hinge',
optimizer='adadelta',
metrics=['accuracy']) instead. |
in the code the hinge loss is defined as: However, as I know it, the loss for an SVM should be this: where s_i ist the score of the i-th output unit. As I understand it the hingeloss ignores the additive s_j term and only uses the s_{y_i}-term in the formula. Where is my error? |
@McLawrence the hinge loss implemented in keras is for a specific case of binary classification [A vs ~A]. If this is to be used labels must be in the format of {-1, 1}. You might refer this for reference #2830. And there is also categorical hinge now in losses.py. You can refer that also. |
@huanglianghua , @aniket03 Is it really enough to change the loss in the compile to |
Here are a couple of good references:: I tested the following code with real data without any problem. Of course, the results will be different from the ones from real SVM implementation (e.g., sklearn's SVM). An interesting thing is that this Keras implementation produced a lot better results than the official SVM with specific data sometimes. Of course, another benefit is that you can use GPU to train SVM.
|
How does this align with the use of RandomFourierFeatures for SVM approximation found here? Is RandomFourierFeatures a better, more modern approach? |
The suggested code from @huanglianghua and @statcom seem to work, but I wonder if there is a way of outputting probabilities instead. I tried to use softmax, but the model doesn't improve at all (with 3 classes it gets stuck at 33%; with 2 classes gets stuck at 50%). My goal is to 'exactly' replicate an SVM model, so that I can convert it to .onnx and reuse it. The only settings I got it to work are the following:
Any ideas on how to get a probability at the end? Using argmax seems to work just fine, but how do I interpret the output? I mean, I would like to set some sort of threshold to decide whether a prediction is good or not. Let's say I got 3 classes, but I input something else to the model to predict. I will still get a max value from argmax, even if it's wrong. A probability would let me avoid this by setting some sort of threshold. Any ideas on how to approach this? |
Gettng probabilities out of SVM is usually done by adding a logistic regression after the linear output of the base SVM model. Use a |
Thanks, @erlendd I tried the following approach:
But it still gets stuck at 33% for some reason. I guess the input data would have to be normalised in order to use softmax here, which is not. The input data are raw vectors--and that works totally fine as long as I don't add anything after the linear output. Edit:
|
I want to add svm in last layer of my model
I tried to change the loss to hinge
but the accuracy of training data doesn't change in each iteration
The text was updated successfully, but these errors were encountered: