This document discusses machine learning projects in Go. It begins by explaining what machine learning is and provides an example of linear regression. It then outlines the typical steps for a machine learning project, including gathering data, defining models, training and testing models, and deploying models. The document discusses options for developing machine learning models in Go, including using TensorFlow bindings, Gorgonia for building graphs, and Gonum libraries. It provides examples of implementing convolutional neural networks and recurrent neural networks in these Go frameworks. While developing neural networks from scratch in Go is challenging, the document concludes that Go is suitable for machine learning if using existing TensorFlow models or libraries like Gorgonia and Gonum for other model types.
14. Example machine learning: linear regression
14
How much time for a 6km walk
aprox?
1. 200 mins
2. 10 mins
3. 120 mins
15. How do you do a real machine learning project?
15
16. Steps of a data science project
16
Gather Data
Profile Data
Clean/
Manipulate Data
Prepare
Training and
Test Data
Define Model(s)
Train Model(s)
Test Model(s)
Export/Compile
Model(s)
Deploy Model
Inference
Deploy
Online/Prod
Model Training
Automation,
Monitoring and
Updating
Identify the
problem
Explore,
select data
Train and
test
model(s)
Deploy
and use
model(s)
17. Machine learning project code
17
https://papers.nips.cc/paper/5656-hidden-technical-debt-in-machine-learning-systems.pdf
Only a small fraction of real-world ML systems is composed of the ML code.
19. 19
Gather Data
Profile Data
Clean/
Manipulate Data
Prepare
Training and
Test Data
Define Model(s)
Train Model(s)
Test Model(s)
Export/Compile
Model(s)
Deploy Model
Inference
Deploy
Online/Prod
Model Training
Automation,
Monitoring and
Updating
Identify the
problem
Explore,
select data
Train and
test
model(s)
Deploy
and use
model(s)
23. 23
Gather Data
Profile Data
Clean/
Manipulate Data
Prepare
Training and
Test Data
Define Model(s)
Train Model(s)
Test Model(s)
Export/Compile
Model(s)
Deploy Model
Inference
Deploy
Online/Prod
Model Training
Automation,
Monitoring and
Updating
Identify the
problem
Explore,
select data
Train and
test
model(s)
Deploy
and use
model(s)
25. 25
Inputs Outputs
{“text”: “a move to stop” }
Deng, Y., Kanervisto, A.,
Image-to-Markup Generation with
Coarse-to-Fine Attention. In
International Conference on Machine
Learning (pp. 980-989)
27. TensorFlow
● Library for ML
● Use a graph based flow
● Maybe the most popular
framework
27
op1
Tensor1
op2
op3
op4
op5Tensor2
Tensor3
Tensor4
Session
1.
2.
28. From the TensorFlow’s web site:
“These APIs are particularly well-suited to loading models
created in Python and executing them within a Go
application”
TensorFlow official bindings
28
30. Trying to implement a CNN in Go-TensorFlow
conv1 := op.Conv2D(root.SubScope("conv1"), inputLayer, filterTensor, []int64{5, 5, 1, 1}, "VALID")
pool1 := op.MaxPool(root.SubScope("pool1"), conv1, []int64{2, 2, 1, 1}, []int64{2, 2, 1, 1}, "VALID")
…
conv2_no_heigh := op.Reshape(root, pool2, op.Shape(root, pool2))
loss, backprop := op.SoftmaxCrossEntropyWithLogits(root, conv2_no_heigh, labels)
….
30
Only one optimizer: Stochastic Dual Coordinate Ascent
(SDCA).
Convolution and
sampling for a layer
in my CNN
Calculating the difference
between what I got and
what I expected
31. Model development and
testing
● You can create a graph with the Go
bindings, but it is complicated
● There are some elements like “session”
and “scope”. Not very intuitive from Go
point of view
● We don’t have all functions and I’ll have
to implement them from scratch
31
op1
Tensor1
op2
op3
op4
op5Tensor2
Tensor3
Tensor4
33. If we have to develop from scratch, why not to use a
native Go library?
33
34. Gorgonia
● It is similar to theano (Python)
● Works with graphs (as Tensorflow does)
● Support many Go versions (including the most recent)
34
35. The CNN with Gorgonia
g := gorgonia.NewGraph()
x := gorgonia.NewTensor(g, dt, 4, gorgonia.WithShape(bs, 3 , 32, maxImageLenght),
gorgonia.WithName("x"))
y := gorgonia.NewMatrix(g, dt, gorgonia.WithShape(bs, 10), gorgonia.WithName("y"))
Also tensor
Multidimensional array
(more accurate to ml
operations)
35
Graph initialization
38. The encoder in TensorFlow
38
lstm_cell_list_fw = [tf.contrib.rnn.LSTMCell(num_units=num_hidden,
forget_bias=1.0,
state_is_tuple=True,
activation=tf.nn.relu6) for i in range(num_layers)]
lstm_cell_list_bw = ….
multi_rnn_cell_fw = tf.contrib.rnn.MultiRNNCell(lstm_cell_list_fw, state_is_tuple=True)
multi_rnn_cell_bw = ….
outputs, output_state = tf.nn.bidirectional_dynamic_rnn(multi_rnn_cell_fw,
multi_rnn_cell_bw,
inputs=input_layer,
dtype=tf.float32,
sequence_length=sequence_lengths_placeholder,
time_major=False,
scope="encoder_rnn_bidir")
Basic Cell creation
RNN creation
Bidirectional RNN
creation
39. The encoder in Go
39
https://github.com/owulveryck/min-char-rnn
https://github.com/owulveryck/lstm
https://github.com/gorgonia/gorgonia/tree/master/examples/charRNN
Only do under the consent of a responsible data scientist
40. 40
Inputs Outputs
{“text”: “a move to stop” }
Convolutional
Neural Network
(CNN)
Encoder:
Recurrent
Neural
Network (RNN)
Decoder:
Recurrent
Neural
Network (RNN)
you have to create them from scratch
41. Implications
41
● If I want to create a neural network in go, I
have to do it from scratch
● For other machine learning models, we can
use gorgonia and gonum
42. 42
Gather Data
Profile Data
Clean/
Manipulate Data
Prepare
Training and
Test Data
Define Model(s)
Train Model(s)
Test Model(s)
Export/Compile
Model(s)
Deploy Model
Inference
Deploy
Online/Prod
Model Training
Automation,
Monitoring and
Updating
Identify the
problem
Explore,
select data
Train and
test
model(s)
Deploy
and use
model(s)
43. 43Deep learning frameworks. Source: den Bakker 2017
Open Neural Network
Exchange (ONNX)
Frameworks interoperability
En création par Olivier Wulveryck
https://github.com/owulveryck/onnx-go
45. Saving the model TF (1.X) using Python
builder = tf.saved_model.builder.SavedModelBuilder("ocrModel")
builder.add_meta_graph_and_variables(sess, ["serve"])
builder.save()
45
Creates a folder
“ocrModel”
Saves the graph
and variables
46. Loading and running a model in Go
model := tf.LoadSavedModel(modelPath, []string{"serve"}, nil) // load model from file
...
tensors := map[tf.Output]*tf.Tensor{
graph.Operation(input_layer").Output(0): tensor,
}
outputs := []tf.Output{
graph.Operation("decoder/decoder_prediction/kernel/Adam_1").Output(0),
}
result, runErr := model.Session.Run(
tensors,
outputs,
nil,
)
46
Load model in
modelPath
Tensor with name
“input_layer” in my graph
Output for the operation
“decoder/decoder_prediction
/kernel/Adam_1”
Run the model
47. Running the model: TensorBoard to identify tensors
names
47
Tensors’
names
48. Some problems
● Errors of type:
● The response of my model was one-hot encoded
○ I had to transform it by hand because, there is no Go library to do that
48
panic: nil-Operation. If the Output was created with a Scope
object, see Scope.Err() for details.
51. Other tools
51
Gather Data
Profile Data
Clean/
Manipulate Data
Prepare
Training and
Test Data
Define Model(s)
Train Model(s)
Test Model(s)
Export/Compile
Model(s)
Deploy Model
Inference
Deploy
Online/Prod
Model Training
Automation,
Monitoring and
Updating
Identify the
problem
Explore,
select data
Train and
test
model(s)
Deploy
and use
model(s)
Event driving
55. 55
Go tools for Data Science
● Gonum libraries
● http://github.com/kniren/gota/dataframe
● Notebook jupyter
● Gorgonia
● ONNX for Go
● Official tensorflow bindings for Go
● Pachyderm
56. Conclusions
● If you are using existing Tensorflow models
○ You are good to go, you can use the official bindings or tfgo
● If you want to develop from scratch
○ You can use Gorgonia and Gonum
○ If you want to have it easy and have the support and the knowledge of a community, use
Python
● If you are creating a model (not neural network)
○ You can use Go libraries (but most are still in development)
○ Things are getting better though :D
56
57. 57From tweet of AmyCharlotte Kean
“Tastes better than it
looks”