Getting LIBSVM to work with Octave on Windows

This post will help you get to the point where you can invoke LIBSVM commands from Octave, on Windows. I did this with an x64 build of Windows 8, Octave 3.6.2 x64, and LIBSVM 3.13. Your mileage may vary.

According to the LIBSVM FAQ, Octave support has been available since version 2.86 (of LIBSVM), and all you have to do was type make inside Octave. The README file in the LIBSVM matlab subfolder adds that prebuilt binaries  are included only for 64-bit Matlab on Windows. Those indeed did not work with Octave. There are instructions on building ‘libsvmread.mex’, ‘libsvmwrite.mex’, ‘svmtrain.mex’, and ‘svmpredict.mex’, but those didn’t work for me right out of the box, and it wasn’t clear what to do with the .mex files once they’re ready.

The LIBSVM distribution includes a matlab subfolder. This folder includes 5 C source files and 1 header file that together define an interface for Matlab and Octave to invoke LIBSVM. LIBSVM itself was compiled from C source code, available elsewhere in the LIBSVM folder structure. Neither Matlab nor Octave can invoke C code directly. It has to be compiled first into a .mex file (or files), which Matlab and Octave know how to deal with. The format and content of .mex files are highly dependent on the operating system, the processor architecture of Matlab/Octave (e.g. x86 or x64), and whether they are intended for use by Matlab or Octave. A .mex file built for use in Matlab, for example, will not work Octave.

At this point it should be clear that to invoke LIBSVM from Octave, we will have to compile C source code to .mex files for our version of Octave. Once the .mex files are available, we will have to put them somewhere Octave will know to look for them in. Compilation of C source code requires a C compiler, and we will use the compiler in Microsoft’s Visual Studio.

Here are the steps I followed:

  1. Make sure you install the Visual Studio version of Octave. I got my copy of octave-3.6.2-vs2010-setup.exe at the SourceForge download page and installed with the default options.
     
  2. Make sure you have a copy of Visual Studio 2010 or 2012. The professional versions of  VS2010 and VS2012 both worked fine. If you don’t have a copy, the Express version should be available for free from Microsoft (I didn’t try it though — let us know if it works!)
     
  3. Download the LIBSVM zip file and expand it in some folder. You can find the zip in the LIBSVM landing page — search for “download”.
     
  4. Open a command prompt window with Run As Administrator. Run vcvarsall.bat from the Visual C++ directory. This updates your path and adds environment variables to allow you to run the Visual C++ compiler from the command line:
    Executing vcvarsall.bat
  5. In the same command prompt window, add the Octave bin folder to your path. This will allow Octave to find the tools it needs to create .mex files. In particular, those tools include mkoctfile.exe and cc-msvc.exe:
    Add the Octave binaries folder to your path
  6. Edit Octave’s math.h file to reference the correct location of Visual Studio’s math.h. On my machine, the file to change is c:\Program Files (x86)\Octave-3.6.2\include\math.h. As of LIBSVM 3.13, the offending reference is at line 74. Octave’s math.h has a hardcoded reference to c:/Program Files/Microsoft Visual Studio 10.0/VC/include/math.h. Change it, if needed, to the correct location on your machine. On mine, it had to be changed to c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/math.h.
     
    Note that you will have to use an editor that understands Unix-style newlines. Windows’ notepad doesn’t, and renders the file as one very long line. I used notepad++. You could also use Visual Studio’s editor to make the change. Another consideration is that you will need administrative privileges to save the modified file. One way to obtain those is to launch the editor from your Run-As-Administrator command prompt.Edit Octave's math.h
  7. Still in the same command prompt, start Octave and navigate to the matlab subfolder of LIBSVM:
    Start Octave and navigate to the matlab subfolder of LIBSVM
  8. In Octave, and when in the matlab subfolder of LIBSVM, type make. This executes the make.m script in the current folder, which builds the .mex files:
    Type 'make' inside Octave to execute make.m
  9. If all went well, the .mex files should now be present in the current directory. You can either leave them there and add the directory to the Octave search path (use the addpath command inside Octave), or move them to a folder Octave already to knows to search in. I did the latter:
    Copy the .mex files to a folder Octave knows to search in
  10. You’re almost done. Start Octave and try the svmtrain command:
    The svmtrain command is now available in Octave

This is as far as I got at this point. svmtrain appears to be available for calling from Octave. I did not try to generate a model from data just yet — I’ll update as soon as I’m confident that works as well.

Advertisements

11 Responses to Getting LIBSVM to work with Octave on Windows

  1. Leslie says:

    Thanks! I needed to compile LIBSVM for 32-bit windows. I downloaded Visual Studio Express 2012 for desktop. That worked. I am using Octave-3.2.4 (not especially for Visual Studio). That worked. I did not need to edit math.h .

    Got error messages when I ran make. But the mex files were there and they appear to work!
    warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘int’
    warning: initialization from incompatible pointer type in function ‘mexFunction’
    warning: initialization from incompatible pointer type in function ‘print_null’
    warning: control reaches end of non-void function

    THANKS A MILLION!

  2. Anonymous says:

    I had libsvm installed for octave based on this blog a few weeks ago, thanks, on a window7 32-bit machine.

    Now that I purchased a window8 64-bit machine, one of the first things was to install libsvm for octave again. The problem appeared to be from the command prompt ( it would not recognize octave). I tried octave 3.6.2 and 3.2.4, both were “octave is not recognized as an internal or external command, operable program, or batch file”. Anyone encountered the same problem please give a hint of how to solve it. Thank you.

    • Anonymous says:

      I did encounter the same problem with you. Then, I realized that I had skipped step 5 in the above instruction. When I followed step 5, everything is okay. Hope this helps!

  3. Anonymous says:

    you are the man!!!!
    great work!
    works like a charm.
    thanks…

  4. Anonymous says:

    The background information you supplied was 100% correct and the build instructions worked perfectly. Thanks very much!

  5. Nhan D. says:

    Thank you very much! It worked. I have trained and tested data successfully!

  6. Anonymous says:

    Very thanks to your information.
    I made mex files according to your blog, and works fine.
    But to prepare Visual Studio is too hard for many people, so I prepared mex files on Github.

    https://github.com/icoxfog417/libsvm_mex

  7. Great tutorial, I’ve managed to install LIBSVM in no time!

    I’ve asked LIBSVM guys to have a look at your tutorial and fix their README properly.

  8. Anonymous says:

    Thanks. These instructions work for me with 64 bit Windows 8.1, octave-3.6.4 and libsvm3.20 and Visual Studio 2013. Edited math.h #include to

  9. Anonymous says:

    c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/include/math.h

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: