TensorFlowとは
Google製のディープラーニングを試せるライブラリです。
※個人的なメモです。CPUアーキテクチャの種類、PythonのバージョンやGPUの利用によっては手順が変わってくるので基本は公式のドキュメントを参照したほうが良いです。
実施環境
- x64 アーキテクチャ
- Ubuntu Trusty (14.04 LTS)
- GPU無し
導入手順
pipのインストール
1 |
sudo apt-get install python-pip python-dev |
tensorflowのインストール(CPU python2.7版)
1 |
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl |
ディレクトリの確認
1 |
python -c 'import os; import inspect; import tensorflow; print(os.path.dirname(inspect.getfile(tensorflow)))' |
mnistの実行
1 |
python -m tensorflow.models.image.mnist.convolutional |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting data/train-images-idx3-ubyte.gz Extracting data/train-labels-idx1-ubyte.gz Extracting data/t10k-images-idx3-ubyte.gz Extracting data/t10k-labels-idx1-ubyte.gz Initialized! Step 0 (epoch 0.00), 7.0 ms Minibatch loss: 12.053, learning rate: 0.010000 Minibatch error: 90.6% Validation error: 84.6% ... Step 8500 (epoch 9.89), 569.6 ms Minibatch loss: 1.601, learning rate: 0.006302 Minibatch error: 0.0% Validation error: 0.9% Test error: 0.8% |
gitのインストール
1 |
sudo apt-get install git |
TensorFlow repositoryのclone
1 |
git clone --recurse-submodules https://github.com/tensorflow/tensorflow |
JDK8のインストール
1 2 3 |
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer |
その他Bazelのインストールに必要なパッケージの導入
1 |
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip |
Bazelのインストール
1 2 3 |
wget https://github.com/bazelbuild/bazel/releases/download/0.2.1/bazel-0.2.1-installer-linux-x86_64.sh chmod +x bazel-0.2.1-installer-linux-x86_64.sh ./bazel-0.2.1-installer-linux-x86_64.sh --user |
環境設定
1 2 |
echo "export PATH=\"\$PATH:\$HOME/bin\"" >> .bashrc source .bashrc |
その他必要資材のインストール
1 |
sudo apt-get install python-numpy swig python-dev |
Tensorflowのconfigure
1 2 3 4 5 6 |
cd ~/tensorflow ./configure Please specify the location of python. [Default is /usr/bin/python]: Do you wish to build TensorFlow with GPU support? [y/N] N No GPU support will be enabled for TensorFlow Configuration finished |
Tensorflowの設定
1 2 3 4 5 6 7 |
cd ~/tensorflow/ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package mkdir _python_build cd _python_build ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/* . ln -s ../tensorflow/tools/pip_package/* . sudo python setup.py develop |
bazel buildでエラーが発生したverbose_failuresオプションでエラー箇所がわかるようなのでオプションをつけて再実行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
ERROR: /home/dorako321/tensorflow/tensorflow/core/kernels/BUILD:256:1: C++ compilation of rule '//tensorflow/core/kernels:mirror_pad_op' failed: gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 -DNDEBUG -ffunction-sections ... (remaining 70 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 4. In file included from external/eigen_archive/eigen-eigen-6e521c802bf5/unsupported/Eigen/CXX11/Tensor:82:0, from ./third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1, from ./tensorflow/core/kernels/mirror_pad_op.h:19, from tensorflow/core/kernels/mirror_pad_op.cc:20: external/eigen_archive/eigen-eigen-6e521c802bf5/unsupported/Eigen/CXX11/src/Tensor/TensorUInt128.h: In constructor 'Eigen::internal::TensorUInt128<HIGH, LOW>::TensorUInt128(const T&)': external/eigen_archive/eigen-eigen-6e521c802bf5/unsupported/Eigen/CXX11/src/Tensor/TensorUInt128.h:56:76: warning: typedef 'UnsignedT' locally defined but not used [-Wunused-local-typedefs] typedef typename conditional<sizeof(T) == 8, uint64_t, uint32_t>::type UnsignedT; ^ external/eigen_archive/eigen-eigen-6e521c802bf5/unsupported/Eigen/CXX11/src/Tensor/TensorUInt128.h:57:78: warning: typedef 'UnsignedLow' locally defined but not used [-Wunused-local-typedefs] typedef typename conditional<sizeof(LOW) == 8, uint64_t, uint32_t>::type UnsignedLow; ^ gcc: internal compiler error: Killed (program cc1plus) Please submit a full bug report, with preprocessed source if appropriate. See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions. Target //tensorflow/tools/pip_package:build_pip_package failed to build Use --verbose_failures to see the command lines of failed build steps. |
verboseオプション付き
1 2 |
cd ~/tensorflow/ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package --verbose_failures |
検索してみると、どうやら単純にメモリ不足でエラーとなっている模様。というわけでVMの割当メモリを増やして再度ビルド。
TensorFlowのニューラルネットモデルの学習
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
cd ~/tensorflow/tensorflow/models/image/mnist python convolutional.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting data/train-images-idx3-ubyte.gz Extracting data/train-labels-idx1-ubyte.gz Extracting data/t10k-images-idx3-ubyte.gz Extracting data/t10k-labels-idx1-ubyte.gz Initialized! Step 0 (epoch 0.00), 5.8 ms Minibatch loss: 12.053, learning rate: 0.010000 Minibatch error: 90.6% Validation error: 84.6% ... Step 8500 (epoch 9.89), 524.5 ms Minibatch loss: 1.594, learning rate: 0.006302 Minibatch error: 0.0% Validation error: 0.8% Test error: 0.8% |
動きました