BackGround
なんとなくGoDocを眺めていたら、topのPopular PackagesになにやらTensorFlowというpackageを発見。
TensorFlowは常々興味があったので、とりあえずインストールしてみました。
色々調べてみた感じ、あまりGo x TensorFlowな日本語の記事は少なく、Qiitaでもその存在を広めたいので、とりあえずインストール編な記事を書きます。
この記事はTensorFlowオフィシャルと同様のチュートリアルをトライしてみた形となります。
package tensorflow
パッケージ自体は、C言語のTensorFlowライブラリをbindする形で実装されている。
Package tensorflow is a Go binding to TensorFlow.
The API is subject to change and may break at any time.
TensorFlow (www.tensorflow.org) is an open source software library for numerical computation using data flow graphs. This package provides functionality to build and > execute such graphs and depends on TensorFlow being available. For installation instructions see https://www.tensorflow.org/code/tensorflow/go/README.md
(https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go)
なお2017/07/22現在、サポートされているプラットフォームは以下のみ。
- Linux
- Mac OS X
Environment
筆者の環境は以下。
- Mac OS X
- El Capitan ver 10.11.6
- Go
- 1.7.4
Installation
GoのpackageはTensorFlowライブラリに依存する形なので、まずはそれを先にインストールする必要があります。
まずは、ライブラリが使用するプロセッサと、ライブラリのインストールディレクトリを環境変数で設定します。
インストールディレクトリは基本的に/usr/local
で問題ないと思います。
$TF_TYPE="cpu" # cpu or gpu
$TARGET_DIRECTORY='/usr/local'
次にcurl
コマンドでTendorflowライブラリをインストールします。
$curl -L "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-$(go env GOOS)-x86_64-1.1.0.tar.gz" | sudo tar -C $TARGET_DIRECTORY -xz
そして、Goのパッケージをインストールします。
go get github.com/tensorflow/tensorflow/tensorflow/go
ライブラリを問題なく呼出せているかを確認するためにgo test
でテストします。
go test github.com/tensorflow/tensorflow/tensorflow/go
これでインストールは完了です。
Hello Tensorflow(x Go)
とりあえずオフィシャルのHello, Worldなソースを動かしてみましょう。
Tensorflowのversionを出力するのみの簡単なプログラムのようです。
手癖でエラー処理のみ書き換えています。
package main
import (
"fmt"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
"log"
)
func main() {
s := op.NewScope()
c := op.Const(s, "Hello form TensorFlow version"+tf.Version())
graph, err := s.Finalize()
if err != nil {
log.Fatal(err)
}
sess, err := tf.NewSession(graph, nil)
if err != nil {
log.Fatal(err)
}
output, err := sess.Run(nil, []tf.Output{c}, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(output[0].Value())
}
$ go build && ./hello_tf
2017-07-22 19:53:42.754826: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't c
ompiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations
.
2017-07-22 19:53:42.755638: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't c
ompiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 19:53:42.755652: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't c
ompiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-22 19:53:42.755660: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't c
ompiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Hello form TensorFlow version1.1.0
おやおや、エラーか??と、思ったのですが、オフィシャルに、
The program might also generate multiple warning messages of the following form, which you can ignore:
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library
wasn't compiled to use Type instructions, but these are available on your
machine and could speed up CPU computations.
と書いてあります。
なので無視しておきます。
Gopher engaged in Tensorflow!!
というわけでこれからは、GopherなGeekもTensorflowを使っていけるようです。
まあ、なにができるかあまり知りませんが。
しばらくは他の言語のソースを参考にするのも良いかもしれませんね。
次は何か具体的なものを作ってみよう。