Skip to content

Instantly share code, notes, and snippets.

Last active January 8, 2024 05:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mathias-brandewinder/6443302 to your computer and use it in GitHub Desktop.
Save mathias-brandewinder/6443302 to your computer and use it in GitHub Desktop.
Experimenting with Accord SVM
#r @"..\packages\Accord.\lib\Accord.dll"
#r @"..\packages\Accord.Math.\lib\Accord.Math.dll"
#r @"..\packages\Accord.Statistics.\lib\Accord.Statistics.dll"
#r @"..\packages\Accord.MachineLearning.\lib\Accord.MachineLearning.dll"
open System
open System.IO
open Accord.MachineLearning
open Accord.MachineLearning.VectorMachines
open Accord.MachineLearning.VectorMachines.Learning
open Accord.Statistics.Kernels
The dataset I am using here is a subset of the Kaggle digit recognizer;
download it first on your machine, and correct path accordingly.
Training set of 5,000 examples:
Validation set of 500 examples, to test your model:
let training = @"C:/users/mathias/desktop/dojosample/trainingsample.csv"
let validation = @"C:/users/mathias/desktop/dojosample/validationsample.csv"
let readData filePath =
File.ReadAllLines filePath
|> fun lines -> lines.[1..]
|> (fun line -> line.Split(','))
|> (fun line ->
(line.[0] |> Convert.ToInt32), (line.[1..] |> Convert.ToDouble))
|> Array.unzip
let labels, observations = readData training
let features = 28 * 28
let classes = 10
let algorithm =
fun (svm: KernelSupportVectorMachine)
(classInputs: float[][])
(classOutputs: int[]) (i: int) (j: int) ->
let strategy = SequentialMinimalOptimization(svm, classInputs, classOutputs)
strategy :> ISupportVectorMachineLearning
let kernel = Linear()
let svm = new MulticlassSupportVectorMachine(features, kernel, classes)
let learner = MulticlassSupportVectorLearning(svm, observations, labels)
let config = SupportVectorMachineLearningConfigurationFunction(algorithm)
learner.Algorithm <- config
let error = learner.Run()
printfn "Error: %f" error
let validationLabels, validationObservations = readData validation
let correct = validationLabels validationObservations
|> (fun (l, o) -> if l = svm.Compute(o) then 1. else 0.)
|> Array.average
let view = validationLabels validationObservations
|> fun x -> x.[..20]
|> Array.iter (fun (l, o) -> printfn "Real: %i, predicted: %i" l (svm.Compute(o)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment