SvmLightLib

A Windows DLL wrapper for Thorsten Joachims’ SVM implementations.

Support vector machines (SVMs) are a set of supervised learning methods used for classification and regression. One of the most well-known implementations available freely on the Web is Thorsten Joachims’ SVMlight (accompanied with SVMperf and variants of SVMstruct).

The original library is implemented in C. Several command-line utilities are implemented on top of it to expose the functionality to end users. The utilities are easy to use in contrast to the underlying library which exhibits a relatively steep learning curve. This motivated us to develop a DLL wrapper for the library. The developed DLL interface looks much like that of the command-line utilities. In short, the DLL wrapper has the following properties:

  • It is easy to use, much like the command-line utilities.
  • It does not require writing and reading datasets and models to/from a disk.
  • It supports working with several SVM models.
  • It is thread-safe. The original code was modified so that all global variables were removed. It is thus possible to train several models in parallel (e.g. in different threads).
  • It supports saving models in a binary file format (smaller in size, faster to load).
  • It is written in C++.
  • It is meant to be used from other programming environments (e.g. .NET).

Downloads

Source Code

Binaries

Change Log

Version 0.9 (October 2008)

The first release. Supports binary induction, binary transduction, regression (not tested), and multiclass induction. The binary models are based on SVMlight. The multiclass model is based on SVMstruct and is thus much faster. Saving and loading binary models is not (yet) supported.

Version 1.0 (April 2009)

A C# usage example is included. It is now possible to save and load binary models. Also, it is easier to build 64-bit applications: a 64-bit target platform option is available in the configuration menu.

License

The license statement is included in the corresponding downloadable packages (look for the file named License.txt). In short, SvmLightLib is available for non-commercial use only. It must not be modified and distributed without prior permission of the author of SVMlight and SVMstruct (Thorsten Joachims). None of the authors is responsible for implications from the use of this software.

4 Comments »

  1. Milan said

    Hi.
    I do not know if you have time for this – but I try.
    I know only programming in Visual Basic 6 (or in VBA). I know a little how to declare dll in it, but as I know I should declare functions. For instance function ENSolveH from dll epanet2.dll like this:
    Declare Function ENsolveH Lib “C:\Program Files\Epanet2\epanet2.dll” () As Long
    But I do not know which are the functions or what I should call from SvmLightLib…
    Please just give me direction – maybe I find then.
    Thanks,
    Milan

    • Miha said

      Hi Milan!

      I prepared a code snippet that works in Visual Basic 2005 (with some minor modifications, it should also work in VBA). You will probably need to change the path to the DLL file. Note that this snippet doesn’t do anything useful – it just creates a short feature vector and gets its size back from the DLL. To see how to use these functions for some real action, please take a look at SvmLightLib1.0Src-1.zip\SvmLightLib\SvmLightLibDemo\SvmLightLibDemo.cs. It is in C# but should give you directions on how to use the library in VBA. If you get stuck, let me know :-)

      *** Code snippet: ***

      Option Explicit On

      Module SvmLightLib

      Private Declare Function NewFeatureVector Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_count As Integer, ByVal features As Integer(), ByVal weights As Single(), ByVal label As Double) As Integer
      Private Declare Sub DeleteFeatureVector Lib “c:\svmlib\svmlightlib.dll” (ByVal id As Integer)
      Private Declare Function GetFeatureVectorFeatureCount Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer) As Integer
      Private Declare Function GetFeatureVectorFeature Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer, ByVal feature_idx As Integer) As Integer
      Private Declare Function GetFeatureVectorWeight Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer, ByVal feature_idx As Integer) As Single
      Private Declare Function GetFeatureVectorLabel Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer) As Double
      Private Declare Sub SetFeatureVectorLabel Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer, ByVal label As Double)
      Private Declare Function GetFeatureVectorClassifScoreCount Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer) As Integer
      Private Declare Function GetFeatureVectorClassifScore Lib “c:\svmlib\svmlightlib.dll” (ByVal feature_vector_id As Integer, ByVal classif_score_idx As Integer) As Double

      Private Declare Sub _TrainModel Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String)
      Private Declare Function TrainModel Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String, ByVal feature_vector_count As Integer, ByVal feature_vectors As Integer()) As Integer
      Private Declare Sub SaveModel Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal file_name As String)
      Private Declare Function LoadModel Lib “c:\svmlib\svmlightlib.dll” (ByVal file_name As String) As Integer
      Private Declare Sub SaveModelBin Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal file_name As String)
      Private Declare Function LoadModelBin Lib “c:\svmlib\svmlightlib.dll” (ByVal file_name As String) As Integer
      Private Declare Sub _Classify Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String)
      Private Declare Sub Classify Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal feature_vector_count As Integer, ByVal feature_vectors As Integer())
      Private Declare Sub DeleteModel Lib “c:\svmlib\svmlightlib.dll” (ByVal id As Integer)

      Private Declare Sub _TrainMulticlassModel Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String)
      Private Declare Function TrainMulticlassModel Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String, ByVal feature_vector_count As Integer, ByVal feature_vectors As Integer()) As Integer
      Private Declare Sub SaveMulticlassModel Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal file_name As String)
      Private Declare Function LoadMulticlassModel Lib “c:\svmlib\svmlightlib.dll” (ByVal file_name As String) As Integer
      Private Declare Sub SaveMulticlassModelBin Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal file_name As String)
      Private Declare Function LoadMulticlassModelBin Lib “c:\svmlib\svmlightlib.dll” (ByVal file_name As String) As Integer
      Private Declare Sub _MulticlassClassify Lib “c:\svmlib\svmlightlib.dll” (ByVal args As String)
      Private Declare Sub MulticlassClassify Lib “c:\svmlib\svmlightlib.dll” (ByVal model_id As Integer, ByVal feature_vector_count As Integer, ByVal feature_vectors As Integer())
      Private Declare Sub DeleteMulticlassModel Lib “c:\svmlib\svmlightlib.dll” (ByVal id As Integer)

      Sub Main()
      Dim id As Integer
      Dim features() As Integer = {1, 2, 3}
      Dim weights() As Single = {4, 5, 6}
      id = NewFeatureVector(3, features, weights, 0)
      Console.WriteLine(id) ‘ should say 1
      Console.WriteLine(GetFeatureVectorFeatureCount(id)) ‘ should say 3
      End Sub

      End Module

      • Milan said

        Hi Miha.
        I tried your snipet, but I must do some adjustments, because of VB6. For one function my code looks like this:

        Private Declare Function NewFeatureVector Lib “c:\svmlib\svmlightlib.dll” (ByVal featurecount As Integer, features() As Integer, weights() As Single, ByVal label As Double) As Integer

        Sub Main()
        Dim id As Integer
        Dim Features1(1 To 3) As Integer
        Dim Weights1(1 To 3) As Single

        ‘assigning some values to vectors above:
        For id = 1 To 3
        Features1(id) = id
        Weights1(id) = id + 3
        Next

        id = NewFeatureVector(3, Features1, Weights1, 0)

        End Sub

        It always say that are bad DLL calling conventions. Maybe it is because you had features and weights declared ByVal, but this is not possible in VB6 for arrays. I also tried declare this 2 variables as variant and then in Sub Main assign to this variant the same arrays as above. I also give dimensions of arrays from 0 to 2. But nothing works.
        If you have time look what could be wrong I would be happy.
        Thanks,
        Milan

  2. Miha said

    Hi Milan!

    I have recompiled the library to change the calling convention to stdcall and to remove dependencies to MFC DLLs. You can download the new DLL from here.

    The code snippet below now works in VB6. Note that I had to change all “Integer” to “Long”. You will have to do the same. Let me know if this works for you :-)

    Private Declare Function NewFeatureVector Lib “e:\svmlightlib.dll” (ByVal featurecount As Long, features() As Long, weights() As Single, ByVal label As Double) As Long
    Private Declare Function GetFeatureVectorFeatureCount Lib “e:\svmlightlib.dll” (ByVal vecid As Long) As Long

    Sub Main()
    Dim id As Long
    Dim Features1(1 To 3) As Long
    Dim Weights1(1 To 3) As Single

    ‘assigning some values to vectors above:
    For id = 1 To 3
    Features1(id) = id
    Weights1(id) = id + 3
    Next

    id = NewFeatureVector(3, Features1, Weights1, 0)
    MsgBox (Str$(id)) ‘ should say 1

    MsgBox (Str$(GetFeatureVectorFeatureCount(id))) ‘ should say 3
    End Sub

RSS feed for comments on this post

Leave a Comment