This is a generalized math package with clean and transparent API for the Go language. Also available for javascript github/lovesaroha/lmath.js
- Lightweight and Fast.
- Native Go implementation.
- Go 1.9 or higher. We aim to support the 3 latest versions of Go.
Simple install the package to your $GOPATH with the go tool from shell:
go get -u github.com/lovesaroha/lmath
Make sure Git is installed on your machine and in your system's PATH
.
// Random float64 between given range.
random := lmath.Random(-1, 1)
// Return float64 between given range of (200, 1000).
result := lmath.Map(20 , 0, 100, 200, 1000)
// Sigmoid.
result := lmath.Sigmoid(1.5)
// Diff of sigmoid.
dresult := lmath.Dsigmoid(result)
// Relu.
result := lmath.Relu(2)
// Diff of relu.
dresult := lmath.Drelu(result)
// Create a matrix (rows, cols).
matrix := lmath.Matrix(4, 3)
// Print matrix shape.
matrix.Shape()
// Print matrix values.
matrix.Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, -1 , 1)
// Print matrix shape.
matrix.Shape()
// Print matrix values.
matrix.Print()
// Slice of int to matrix and print values.
lmath.ToMatrix([]int{1, 2, 3}).Print()
// 2d slice of int to matrix and print values.
lmath.ToMatrix([][]int{[]int{1, 2},[]int{3, 4}}).Print()
// Slice of float64 to matrix and print values.
lmath.ToMatrix([]float64{4, 5, 6}).Print()
// 2d slice of float64 to matrix and print values.
lmath.ToMatrix([][]float64{[]float64{7, 8},[]float64{9, 0}}).Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
matrixB := lmath.Matrix(3, 4, 0, 10)
// Add and print values.
matrix.Add(matrixB).Print()
// Subtract and print values.
matrix.Sub(matrixB).Print()
// Multiply and print values.
matrix.Mul(matrixB).Print()
// Divide and print values.
matrix.Sub(matrixB).Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
// Add and print values.
matrix.Add(2).Print()
// Subtract and print values.
matrix.Sub(2).Print()
// Multiply and print values.
matrix.Mul(2).Print()
// Divide and print values.
matrix.Sub(2).Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
matrixB := lmath.Matrix(4, 3, 0, 10)
// Dot product and print values.
matrix.Dot(matrixB).Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
// Print values.
matrix.Print()
// Transpose and print values.
matrix.Transpose().Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
// Print values.
matrix.Print()
// Add columns and print values.
matrix.AddCols().Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, 10, 20)
// Print values.
matrix.Print()
// Square and print values.
matrix.Map(func (value float64) float64 {
return value * value
}).Print()
// Create a matrix (rows, cols, minimum , maximum).
matrix := lmath.Matrix(3, 4, -1, 1)
// Print values.
matrix.Print()
// Apply sigmoid and print values.
matrix.Map(lmath.Sigmoid).Print()
// Learning rate.
var learningRate = 0.2
// Inputs and outputs.
inputs := lmath.ToMatrix([][]float64{[]float64{1, 1, 0, 0},[]float64{0, 1, 0, 1}})
outputs := lmath.ToMatrix([][]float64{[]float64{1, 1, 0, 1}})
// Weights and bias.
weights := lmath.Matrix(2, 1, -1, 1)
bias := lmath.Random(-1, 1)
// Train weights and bias (epochs 1000).
for i := 0; i < 1000; i++ {
// Sigmoid(wx + b).
prediction := weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid)
dZ := prediction.Sub(outputs)
weights = weights.Sub(inputs.Dot(dZ.Transpose()).Divide(4).Mul(learningRate))
bias -= dZ.Sum() / 4
}
// Show prediction.
weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid).Print()