Quick, straightforward, and easy-to-use Go implementations of the Perceptron and Averaged Perceptron binary classifiers.
Quick, straightforward, and easy-to-use Go implementations of the Perceptron and Averaged Perceptron binary classifiers.
While in your project directory:$ git clone https://github.com/haydenhigg/percy
Then, import it as:
import "./percy"
Regularize(arr []float64) []float64
: Regularizes the L2 norm of arr
to 1.RegularizeAll(mat [][]float64) [][]float64
: Regularizes the L2 norm of every row of mat
to 1.NewModel(weights []float64, bias float64) Model
: Creates a model with input weights
and an input bias
.Train(inputs [][]float64, outputs []float64, iters int, alpha float64) Model
: Trains a Perceptron classifier on the training inputs
matrix and outputs
vector for iters
iterations with a learning rate of alpha
. Returns the final model (see below).TrainFromModel(init Model, inputs [][]float64, outputs []float64, iters int, alpha float64) Model
: The same as Train
, but initializes the model to init
rather than a model with weights as a zero-vector and a bias of zero.TrainAveraged(inputs [][]float64, outputs []float64, iters int, alpha float64) Model
: The same as Train
, but trains an Averaged Perceptron classifier instead.TrainAveragedFromModel(init Model, inputs [][]float64, outputs []float64, iters int, alpha float64) Model
: The same as TrainFromWeights
, but trains an Averaged Perceptron classifier instead.(mdl Model) Predict(x []float64) float64
: Returns the predicted output (which will be {-1, 1}) for the model mdl
and the input vector x
.(mdl Model) RawPredict(x []float64) float64
: Returns the output before being binarized to {-1, 1}.The Model
is just a struct containing the fields Weights
([]float64) and Bias
(float64).
package main
import (
"fmt"
"./percy"
)
func main() {
inputs := [][]float64{[]float64{...}, []float64{...}, ...}
outputs := []float64{1, -1, ...}
iters := 200
learningRate := 0.01
trainedModel := percy.Train(percy.RegularizeAll(inputs), outputs, iters, learningRate)
fmt.Println(trainedModel.Predict(percy.Regularize([]float64{...})))
}
inputs
matrix does not have the same length, this will fail; if inputs
and outputs
are different lengths, this will fail; if learningRate
is a negative number, the algorithm will not converge; etc.Regularize
and RegularizeAll
)