In this article I’d like to describe:

- How to build a tensorflow library
- How to generate bindings for Crystal
- How to use tensorflow library in Crystal
- Share the results of my work: tensorflow.cr

- Crystal language: https://crystal-lang.org/
- Crystal bindings generator: https://github.com/crystal-lang/crystal_lib
- Tensorflow: https://www.tensorflow.org/
- Tensorfow repository: https://github.com/tensorflow/tensorflow
- tensorflow.cr repository: https://github.com/fazibear/tensorflow.cr
- rensorflow.cr examples repository: https://github.com/fazibear/tensorflow.cr_examples

Install and build tensorflow To install and compile tensorflow we will need to install all these things:

- bazel
- python
- numpy (python package)

At the beginning, we need to checkout whole tensorflow repository

`git clone https://github.com/tensorflow/tensorflow $ `

Next, we need to change directory and run configure

```
cd tensorflow
$ ./configure $
```

We need to answer few questions about python and features that our compiled library will support. We can just hit enter. After that bazel will download all dependencies and setup all things.

We need to build only a library. Let’s do it.

```
cd tensorflow # yes! tensorflow/tensorflow
$ bazel build :libtensorflow.so $
```

That’s hot. We got it. The last step, install it!

`cp ../bazel-bin/tensorflow/libtensorflow.so /usr/local/lib/ libtensorflow.so`

Library installed.

To work with the tensorflow library, we need to generate bindings. Crystal has an application for that. All you need to do is checkout crystal_lib and add the `lib_tensorflow.cr`

file into examples directory. Remember to replace `{tensorflow_dir}`

with directory when you checked out tensorflow.

```
Include(
@["tensorflow/c/c_api.h",
flags: "
-I/{tensorflow_dir}/tensorflow/
-I/{tensorflow_dir}/tensorflow/bazel-genfiles
-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
",
prefix: %w(TF_ Tf)
)]Link("tensorflow")]
@[LibTensorFlow
lib end
```

Now execute generator:

`crystal src/main.cr -- examples/lib_tensorflow.cr $ `

Done. We’ve got bindings. We can copy the output into our crystal project and start to mess up with tensorflow. Let’s name it `lib_tensorflow.cr`

.

Because tensorflow library doesn’t provide any easy ways to build the graph, we need to use python.

```
import tensorflow as tf
with tf.Session() as sess:
= tf.Variable(5.0, name='a')
a = tf.Variable(6.0, name='b')
b = tf.multiply(a, b, name='c')
c
sess.run(tf.global_variables_initializer())'.', 'graph.pb', as_text=False) tf.train.write_graph(sess.graph_def,
```

This simple script will generate a graph and save it into the `graph.pb`

file.

We’ve generated the graph that needs two variables for input and returns one number. It’s multiplication of inputs. We’ve also named it as `a`

, `b`

, `c`

.

Now let’s move to crystal. We need to require our bindings first.

`'lib_tensorflow' require `

Now we’re ready! We need to initialise a `session`

:

```
LibTensorflow.new_session_options
opts = LibTensorflow.new_status
status = LibTensorflow.new_graph
graph = LibTensorflow.new_session(graph, opts, status) session =
```

As you can see, we need a graph, options, and status. All of these can be done using the function from bindings. Also, we need to check if creating session was successful.

`LibTensorflow.get_code(status) puts `

All we need to do is take a status and check its code. We’re using the function from bindings. The line above should print `Ok`

if everything if fine. If it’s not, we can show more detailed error message like this:

`String.new(LibTensorflow.message(status)) puts `

If we have a session, we need to load a graph into.

```
File.read("./graph.pb")
file = LibTensorflow.new_buffer_from_string(file, file.size)
buffer = LibTensorflow.new_import_graph_def_options
import_opts = LibTensorflow.graph_import_graph_def(graph, buffer, import_opts, status)
```

All we need is to read data from the file, create a buffer with a function from bindings, and pass buffer into function that will import our data.

Again we should remember to check if everything is fine:

`LibTensorflow.get_code(status) puts `

Graph loaded. Now we need to create tensors. Tensors hold input and output data. Tensors will hold data.

One more thing, while initialisation tensor will need a deallocation function. We just create empty function. Creating proper function is beyond the scope of this article.

`Pointer(Void), b : UInt64, c: Pointer(Void)) {} deloc = ->(a : `

Now we’re focus on inputs. We need two values. These are tensors for that:

```
Int64
a_dims = [] of 3.0_f32] of Float32
a_data = [LibTensorflow.new_tensor(
a_tensor = LibTensorflow::Datatype::Float,
a_dims, a_dims.size,
a_data, a_data.size,nil)
deloc, Int64
b_dims = [] of 5.0_f32] of Float32
b_data = [LibTensorflow.new_tensor(
b_tensor = LibTensorflow::Datatype::Float,
b_dims, b_dims.size,
b_data, b_data.size,nil) deloc,
```

And empty one for output:

```
Int64
c_dims = [] of Float32
c_data = [] of LibTensorflow.new_tensor(
c_tensor = LibTensorflow::Datatype::Float,
c_dims, c_dims.size,
c_data, c_data.size,nil) deloc,
```

Another thing is operations. Operations take data from tensors and write data other tensors. Tensors variable is also operation so that we can read data from. We will need three operations. They are defined in graph. Remember `a`

, `b`

, `c`

?

```
LibTensorflow::Output.new
i1 = LibTensorflow.graph_operation_by_name(graph, "a")
i1.oper = 0
i1.index = LibTensorflow::Output.new
i2 = LibTensorflow.graph_operation_by_name(graph, "b")
i2.oper = 0
i2.index = LibTensorflow::Output.new
o1 = LibTensorflow.graph_operation_by_name(graph, "c")
o1.oper = 0 o1.index =
```

To make code more readable, we create some variables for inputs and outputs:

```
LibTensorflow::Output
inputs = [i1, i2] of LibTensorflow::X_Tensor
input_values = [a_tensor, b_tensor] of LibTensorflow::Output
outputs = [o1] of LibTensorflow::X_Tensor outputs_values = [c_tensor] of
```

Now we almost have all data require to run our graph, let’s do it:

```
LibTensorflow.new_buffer
optss = LibTensorflow.new_buffer
meta = LibTensorflow::X_Operation
target = [] of LibTensorflow.session_run(session, nil,
inputs, input_values, inputs.size,
outputs, outputs_values, outputs.size,
target, target.size,nil, status)
```

Yeah! Last check of status:

`LibTensorflow.get_code(status) puts `

Where our output is? Sure, let’s take it from output tensor and print it:

```
0]
o = outputs_values[LibTensorflow.tensor_data(o)
out_data = Float32*)
out_value = out_data.as( puts out_value.value
```

As tensor outputs can be different, we need to cast it to the proper value and follow a pointer.

Now we can run our program.

`crystal src/ok.cr $ `

After few `Ok`

we’ve got `15`

which is correct.

First success!

Of course, It’s not the end. I want to learn more!

I’ve created tensorflow.cr project. For now, it is a simple bindings, but I’m working on a beautiful crystal DSL for library and create an ability to graph generation. It’s a fascinating way to learn how tensorflow works. If you’re interested in helping me with this project just let me know. I’ll be appreciated.

In part of that, there is also tensorflow.cr_examples repository. There is one that I was described in this article, and also other that will use crystal DSL (this part might not work during development process)