haskellでtensorflowを使えるようにする手順です。githubに書いてある方法のBuild on MacOSのところです。macOS Sierra(version 10.12.6)でやりました。わざわざhaskellでtensorflowを使ってみようと思うような変人にこんな解説記事は必要ないかもしれませんが、備忘のため。
インストール
準備
stackのバージョンを1.4.0以上にしておく。ただ、1.6.1未満だと(tensorflowとは関係なく)stack自体のエラーが出ることが多いようなので最新にしておくのが良さそう。stack upgrade
で最新にできる。
手順
クローンしてディレクトリ移動
git clone https://github.com/tensorflow/haskell.git tensorflow-haskell
cd tensorflow-haskell/tools
install_macos_dependencies.sh
を実行。これで必要なtensorflowライブラリがインストールされる。
./install_macos_dependencies.sh
途中でエラーが出る場合(ただしこれはhomebrewのエラー)
/usr/local/Homebrew/Library/Homebrew/brew.rb:12:in `<main>': Homebrew must be run under Ruby 2.3! You're running 2.0.0. (RuntimeError)
とりあえず、brew update
してみる。これでもダメだったら
brew update-reset
xcode-select --install
多分これで治る。
クローンしたディレクトリ直下(tensorflow-haskell/
)でビルドしてテスト実行
stack build
stack test
必要なパッケージを色々インストールするので結構時間かかる。
テストが成功すればOK。
デモを動かす
mnistのデモが作られているので実行してみる。
stack exec Main
これで、mnistのデモが実行される。出力はこんな感じ。下の方にテストで認識した数字の結果が表示されている。haskellらしい。
試しにプロジェクトを作成
試しに1つプロジェクトを作ってみる。ここでは、tensorflow-haskell/の下に作る。
stack new Tensorflow-sample new-template
cd Tensorflow-sample
stack.yamlを以下のように編集する(tensorflow-haskell/stack.yamlをコピペしてきて書き換えるのが楽)。
resolver: lts-8.13
packages:
- .
- ../tensorflow
- ../tensorflow-core-ops
- ../tensorflow-logging
- ../tensorflow-opgen
- ../tensorflow-ops
- ../tensorflow-proto
- ../tensorflow-records
- ../tensorflow-records-conduit
- ../tensorflow-test
extra-deps:
- snappy-framing-0.1.1
- snappy-0.2.0.2
# TODO: Remove these once the new versions are in lts-9.
- proto-lens-protobuf-types-0.2.2.0
- proto-lens-0.2.2.0
- proto-lens-descriptors-0.2.2.0
- proto-lens-protoc-0.2.2.3
- lens-labels-0.1.0.2
# For Mac OS X, whose linker doesn't use this path by default
# unless you run `xcode-select --install`.
# TODO: remove this once we stop depending on `snappy`.
extra-lib-dirs:
- /usr/local/lib
extra-include-dirs:
- /usr/local/include
packagesのところだけ書き換えた。
以下、tensorflowで定義した定数を標準出力するだけのコード(app/Main.hs)。
{-# LANGUAGE OverloadedLists #-}
module Main where
import Data.Vector (Vector)
import qualified TensorFlow.Ops as TF --(constant)
import qualified TensorFlow.Session as TF --(runSession, run)
main :: IO ()
main = do
print =<< evalCons
evalCons :: IO (Vector Float)
evalCons = TF.runSession $ do
let a = TF.constant [1] [3 :: Float]
TF.run a
package.yamlに依存パッケージを追記(cabalファイルに直接書いても良い)。
# executablesのところだけ書き換えた。
executables:
Tensorflow-sample-exe:
main: Main.hs
source-dirs: app
dependencies:
- Tensorflow-sample
- vector
- tensorflow
- tensorflow-ops
- tensorflow-proto == 0.1.*
ビルドして実行。
stack build
stack exec Tensorflow-sample-exe
以下、出力。
2018-01-20 17:21:04.395166: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-01-20 17:21:04.396011: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-01-20 17:21:04.396022: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-01-20 17:21:04.396030: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[3.0]
もっと速くできるよという警告がたくさん出てるが、最後に[3.0]
が出力された。
ありがとうございます。
参考になりました。
iMacのmacOS Mojave 10.14.5で
MNIST、サンプルプロジェクトまで動作しました。
閲覧ありがとうございます〜、1年半近く前の記事なので10.14.5でも動いたのは正直驚きです。
参考になってよかったです!