Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions ml/svm.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module ml

import math

pub struct SVMConfig {
pub mut:
max_iterations int = 1000
learning_rate f64 = 0.01
tolerance f64 = 1e-6
}

pub struct DataPoint {
pub mut:
x []f64
y int
}

pub struct SVMModel {
pub mut:
weights []f64
bias f64
config SVMConfig
}

pub struct SVM {
pub mut:
model &SVMModel = unsafe { nil }
config SVMConfig
}

pub fn SVM.new(config SVMConfig) &SVM {
return &SVM{
config: config
}
}

pub fn (mut s SVM) train(data []DataPoint) {
s.model = train_svm(data, s.config)
}

pub fn (s &SVM) predict(x []f64) int {
return predict(s.model, x)
}

fn vector_dot(x []f64, y []f64) f64 {
mut sum := 0.0
for i := 0; i < x.len; i++ {
sum += x[i] * y[i]
}
return sum
}

pub fn train_svm(data []DataPoint, config SVMConfig) &SVMModel {
mut model := &SVMModel{
weights: []f64{len: data[0].x.len, init: 0.0}
bias: 0.0
config: config
}

for _ in 0 .. config.max_iterations {
mut cost := 0.0
for point in data {
prediction := vector_dot(model.weights, point.x) + model.bias
margin := f64(point.y) * prediction

if margin < 1 {
for i in 0 .. model.weights.len {
model.weights[i] += config.learning_rate * (f64(point.y) * point.x[i] - 2 * config.tolerance * model.weights[i])
}
model.bias += config.learning_rate * f64(point.y)
cost += 1 - margin
} else {
for i in 0 .. model.weights.len {
model.weights[i] -= config.learning_rate * 2 * config.tolerance * model.weights[i]
}
}
}

if cost == 0 {
break
}
}

return model
}

pub fn predict(model &SVMModel, x []f64) int {
prediction := vector_dot(model.weights, x) + model.bias
return if prediction >= 0 { 1 } else { -1 }
}
73 changes: 73 additions & 0 deletions ml/svm_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module ml

import math

fn test_vector_dot() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
result := vector_dot(x, y)
assert math.abs(result - 32.0) < 1e-6
}

fn test_svm_new() {
config := SVMConfig{}
svm := SVM.new(config)
assert svm.config == config
}

fn test_svm_train_and_predict() {
mut svm := SVM.new(SVMConfig{})
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
svm.train(data)

for point in data {
prediction := svm.predict(point.x)
assert prediction == point.y
}
}

fn test_train_svm() {
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
config := SVMConfig{}
model := train_svm(data, config)

for point in data {
prediction := predict(model, point.x)
assert prediction == point.y
}
}

fn test_predict() {
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
config := SVMConfig{}
model := train_svm(data, config)

for point in data {
prediction := predict(model, point.x)
assert prediction == point.y
}
}

fn main() {
test_vector_dot()
test_svm_new()
test_svm_train_and_predict()
test_train_svm()
test_predict()
println('All tests passed successfully!')
}