Go for Machine Learning: Weighing the Possibilities and Limitations
The world of machine learning (ML) is firmly associated with Python. However, in recent years, developers have increasingly been asking the question: "Can Go (Golang) be used for ML?". This language, created within Google, is famous for its performance, simplicity, and excellent support for concurrency. In this article, we will analyze how justified the use of Go is in machine learning tasks, what its strengths are, and where it falls short compared to established solutions.
Why Might Go Be Interesting for ML?
Go was not originally designed as a language for Data Science. Its main advantages are execution speed, efficient memory management, and built-in support for parallelism (goroutines and channels). For production systems that require low latency and high throughput, these qualities are invaluable. Unlike Python, which often serves as "glue" to call C++ libraries, Go allows you to write fast and self-contained services.
A key advantage of Go in ML is the ease of deployment. A single compiled binary does not require installing an interpreter, virtual environments, or dozens of dependencies. This makes Go an ideal choice for creating microservices that perform inference (predictions) of trained models.
Main Go Libraries for Machine Learning
The Go ecosystem for ML is significantly smaller than Python's, but it exists and is actively developing. Here are the key libraries you should know:
- Gorgonia — the main framework for deep learning in Go. It provides low-level primitives for building and training neural networks, reminiscent in spirit of TensorFlow or Theano. Gorgonia can perform automatic differentiation and supports GPU operation.
- GoLearn — an analogue of scikit-learn. It contains implementations of popular algorithms: linear regression, K-nearest neighbors, decision trees, K-means clustering, and data preprocessing methods. The library has a clear API, but its functionality is still more modest than its Python counterparts.
- goml — another library focused on ease of use. It supports linear and logistic regression, neural networks, Support Vector Machines (SVM), and Bayesian classifiers.
- TensorFlow Go API — the official binding from Google. It allows you to load and execute pre-trained TensorFlow models (SavedModel format). This is an excellent option for production inference: you train the model in Python and run it in Go.
- ONNX Runtime Go — a library for working with models in the ONNX (Open Neural Network Exchange) format. It allows you to use models trained in PyTorch, TensorFlow, or other frameworks in Go applications.
Let's look at a simple example of using Gorgonia to create linear regression:
package main
import ( "fmt" "gorgonia.org/gorgonia" "gorgonia.org/tensor")
func main() { g := gorgonia.NewGraph()
// Input data (x) and labels (y) x := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(3, 1), gorgonia.WithName("x")) y := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(3, 1), gorgonia.WithName("y"))
// Model parameters: weight (w) and bias (b) w := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(1, 1), gorgonia.WithName("w"), gorgonia.WithInit(gorgonia.GlorotN(1.0))) b := gorgonia.NewScalar(g, tensor.Float64, gorgonia.WithName("b"), gorgonia.WithInit(gorgonia.Zeroes()))
// Model: y_pred = x * w + b pred := gorgonia.Must(gorgonia.Add(gorgonia.Must(gorgonia.Mul(x, w)), b))
// Loss function: MSE loss := gorgonia.Must(gorgonia.Mean(gorgonia.Must(gorgonia.Square(gorgonia.Must(gorgonia.Sub(pred, y))))))
fmt.Println("Graph built")