Machine Learning in Go: Overview of Libraries for Golang
Although Python remains the king of machine learning, Go (Golang) is increasingly used for production systems, microservices, and high-load applications where speed, concurrency, and ease of deployment are important. In this article, we will review the key libraries for machine learning in Go, their capabilities, code examples, and use cases.
Why Go for ML?
Go does not try to replace Python in research work. Its strengths are integration with infrastructure, low memory consumption, built-in concurrency support (goroutines), and compilation into a single binary. This is an ideal choice for:
- Prediction services (inference) based on trained models
- Real-time streaming data processing
- Building high-throughput data pipelines
1. Gorgonia — Framework for Neural Networks
Gorgonia is the Go analog of TensorFlow and PyTorch. It allows you to create and train neural networks with automatic differentiation.
Installation:
go get gorgonia.org/gorgoniaExample: Simple Logistic Regression
package main
import ( "fmt" "gorgonia.org/gorgonia" "gorgonia.org/tensor")
func main() { g := gorgonia.NewGraph()
// Input data (4x2 matrix) x := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(4, 2), gorgonia.WithName("x")) // Weights w := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(2, 1), gorgonia.WithName("w")) // Bias b := gorgonia.NewScalar(g, tensor.Float64, gorgonia.WithName("b"))
// Linear layer: y = x*w + b xw, _ := gorgonia.Mul(x, w) y, _ := gorgonia.Add(xw, b)
// Activation function (sigmoid) activated, _ := gorgonia.Sigmoid(y)
machine := gorgonia.NewTapeMachine(g) // Fill data gorgonia.Let(x, tensor.New(tensor.WithBacking([]float64{1,2,3,4,5,6,7,8}), tensor.WithShape(4,2))) gorgonia.Let(w, tensor.New(tensor.WithBacking([]float64{0.5, -0.2}), tensor.WithShape(2,1))) gorgonia.Let(b, 0.1)
if err := machine.RunAll(); err != nil { fmt.Println(err) }
fmt.Printf("Result: %v", activated.Value())}2. GoLearn — Classic ML Algorithms
GoLearn (golearn) is a library with implementations of KNN, decision trees, random forest, linear regression, and clustering. The API resembles sklearn.
Installation:
go get -u github.com/sjwhitworth/golearnExample: K-Nearest Neighbors (KNN)
package main
import ( "fmt" "github.com/sjwhitworth/golearn/base" "github.com/sjwhitworth/golearn/evaluation" "github.com/sjwhitworth/golearn/knn")
func main() { // Load data (CSV) rawData, err := base.ParseCSVToInstances("iris.csv", true) if err != nil { panic(err) }
// Create KNN with k=3 and Euclidean metric cls := knn.NewKnnClassifier("euclidean", 3)
// Split into train/test trainData, testData := base.InstancesTrainTestSplit(rawData, 0.67)
// Training cls.Fit(trainData)
// Prediction predictions, err := cls.Predict(testData) if err != nil { panic(err) }
// Accuracy evaluation accuracy, err := evaluation.GetAccuracy(testData, predictions) fmt.Printf("Accuracy: %.2f%%", accuracy*100)}3. Goml — Lightweight Models for Quick Integration
Goml provides simple implementations of linear and logistic regression, Naive Bayes, and neural networks with minimal code.
package main
import ( "fmt" "github.com/cdipaolo/goml/base" "github.com/cdipaolo/goml/text")
func main() { // Data for text classification