Groovy web console

subscribe to the feed Subscribe
to this
site

Perceptron

Published 2 weeks ago by Mittie with tags Math data_science
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
// 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