LoginSignup
16

More than 5 years have passed since last update.

Ubuntu+Python2.7にTensorFlowを導入

Last updated at Posted at 2016-01-08

0. TensorFlowとは

Googleが提供している機械学習ライブラリ
@shuhei_fさんのTensorFlow ってなんだろ が分かりやすいです。

1. Ubuntuの準備

Vagrant+VirtualBoxでUbuntu環境構築 などでUbuntuを用意する。

2. 環境の確認

Ubuntu 15.04 / Python 2.7.9

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.04
DISTRIB_CODENAME=vivid
DISTRIB_DESCRIPTION="Ubuntu 15.04"

$ python --version
Python 2.7.9

3. Proxyの設定(必要があれば)

$ export http_proxy="$USERNAME:$PASSWORD@$HOST:$PORT"
$ export https_proxy="$USERNAME:$PASSWORD@$HOST:$PORT"

4. pipのインストール(入ってない場合)

$ curl -kL https://raw.github.com/pypa/pip/master/contrib/get-pip.py | sudo python

5. virtualenvのインストール(入ってない場合)

$ sudo pip install virtualenv

6. TensorFlow用環境の作成

$ mkdir ~/tensorflow
$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
$ source bin/activate

7. TensorFlowのインストール

(tensorflow)$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

下記のエラーが出る場合は、「python-dev」が入っていないため、
sudo apt-get install python-devで導入する。

(略)
    SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

    ----------------------------------------
Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-ZZhjh_/numpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-UmWBtU-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-ZZhjh_/numpy

8. TensorFlowの動作確認

hello-tf.py
import tensorflow as tf
import multiprocessing as mp

core_num = mp.cpu_count()
config = tf.ConfigProto(
    inter_op_parallelism_threads=core_num,
    intra_op_parallelism_threads=core_num )
sess = tf.Session(config=config)

hello = tf.constant('hello, tensorflow!')
print sess.run(hello)

a = tf.constant(10)
b = tf.constant(32)
print sess.run(a+b)

(tensorflow)$ python hello-tf.py
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 2
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 2
hello, tensorflow!
42

9. TensorBoard

ポートフォワードの設定

Vagrantfile
config.vm.network :forwarded_port, host: 6006, guest: 6006
$ vagrant reload

TensorBoardの起動

$ tensorboard --logdir=[log_dir]
Starting TensorBoard on port 6006
(You can navigate to http://localhost:6006)

参考

いつの間にかpipのインストールが楽になってた件
TensorFlowで Hello Worldを動かしてみた&その解説

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16