Search code examples
neural-networksvmtorch

Support Vector Machine in Torch7


I have based my model upon the following tutorial:

https://github.com/torch/tutorials/tree/master/2_supervised

For the last stage a neural network is used upon the features extracted from the CNN. I want to use a SVM in the final layer. How can I add that to my existing model ?

It has been shown in some papers that SVM seem to function better than neural network as the final layer in the CNN and therefore I wanted to try them out to increase the accuracy of the model. Also SVM's can be use for one class classification which neural networks lack.I need a one class classifier in the end and so the need for adding an SVM to th CNN.

Kindly help


Solution

  • Edit: My old answer was complete rubbish since you cannot code a (linear) SVM as a complete module. Instead, you can think of an SVM as

    a 1-layer NN with linear activation on the output node and trained via hinge loss

    (see accepted answer's comments.)

    This means that in Torch, you can mock up a (linear) SVM with something like

    linearSVM = nn.Sequential()
    linearSVM:add(nn.Linear(ninputs, 1))
    criterion = nn.MarginCriterion()
    

    see the following question in the Torch7 google code mailing list...