Created
November 14, 2021 20:23
-
-
Save Dierk/14c18c1ca52a3d8edc00b324e76163a6 to your computer and use it in GitHub Desktop.
Standard learning algorithm for a Perceptron that servers as a linear classifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Perceptron | |
// see https://towardsdatascience.com/perceptron-learning-algorithm-d5db0deab975 | |
def weights = [0, 0, 0] // b, xCoeff, yCoeff such that classifier will become b + xCoeff * x + yCoeff * y == 0 | |
def data = [ | |
[1, 0, 0, false], // bias (start with 1), x, y, classification value | |
[1, 1, 0, true], // here: training the "or" function. Expected: [-1, 1, 1] | |
[1, 1, 1, true], | |
[1, 0, 1, true] | |
] | |
def weightedSum = { sample -> | |
def sum = 0 | |
weights.size().times { i -> sum += weights[i]*sample[i] } | |
return sum | |
} | |
def classifiesCorrectly = { sample -> weightedSum(sample) >= 0 == sample.last() } | |
def sampleThatDoesNotFit = { _ -> data.find { sample -> ! classifiesCorrectly(sample) } } | |
def checkAndAdapt = { sample -> | |
def weight = weightedSum(sample) | |
println "sample: $sample weight: $weight" | |
if (weight < 0 && sample.last() ) { | |
weights.size().times { i -> weights[i] += sample[i]} | |
} | |
if (weight >= 0 && !sample.last() ) { | |
weights.size().times { i -> weights[i] -= sample[i]} | |
} | |
println "adapted weights $weights" | |
} | |
while(todo = sampleThatDoesNotFit() ) { | |
checkAndAdapt(todo) | |
} | |
println weights |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment