CentOSにRuby版TensorFlowをインストールする

Ruby版のTensorFlowであるtensorflow.rbをCentOS 7にインストールしてみました。

github.com

GPUないのでCPUだけ使う版です。

大まかに

  • TensorFlowをコンパイルするためのツールbazelをインストール
  • bazelでTensorFlowをコンパイルしてシェアードライブラリをインストール
  • tensorflow.rbのgemをインストール

といった流れになります。

bazelをインストール

$ sudo yum install gcc-c++ java-1.8.0-openjdk-devel
$ git clone https://github.com/bazelbuild/bazel.git
$ export JAVA_HOME=/usr/lib/jvm/java
$ cd bazel
$ ./compile.sh
...
$ cp output/bazel ~/bin 

TensorFlowをコンパイルしてシェアードライブラリをインストール

$ sudo yum install swig python-devel python-setuptools numpy
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ ./configure

./configureするとPythonのパスやらいろいろ聞いくるので答えます。大抵、デフォルトのままリターン押すだけで良いはず。

$ bazel build //tensorflow:libtensorflow.so
$ sudo cp bazel-bin/tensorflow/libtensorflow.so /usr/lib

tensorflow.rbをインストール

$ sudo yum install ruby-devel
$ sudo gem install bundler
$ git clone https://github.com/somaticio/tensorflow.rb.git
$ cd tensorflow.rb/ext/sciruby/tensorflow_c
$ ruby extconf.rb
$ make
$ sudo make install
$ cd ./../../..
$ bundle install
$ bundle exec rake install

サンプルの実行

以下のサンプルプログラムを実行してみます。

require 'tensorflow'
graph = Tensorflow::Graph.new
input1 = graph.placeholder('input1', Tensorflow::TF_DOUBLE, [2,3])
input2 = graph.placeholder('input2', Tensorflow::TF_DOUBLE, [2,3])
graph.define_op("Add", 'output', [input1, input2], "",nil)

session = Tensorflow::Session.new
session.extend_graph(graph)

input1 = Tensorflow::Tensor.new([[1.0,3.0, 5.0],[2.0,4.0, 7.0]])
input2 = Tensorflow::Tensor.new([[-5.0,1.2,4.5],[8.0,2.3, 3.1]])
result = session.run({"input1" => input1.tensor, "input2" => input2.tensor},["output"],nil)
print result[0], "\n"

実行して以下のような結果になればOK。

[[-4.0, 4.2, 9.5], [10.0, 6.3, 10.1]]