Hope is a Dream. Dream is a Hope.

非公開ブログは再開しました。

Vagrant+UbuntuでTensorFlow1 環境構築(2017年6月版)

Vagrant+UbuntuでTensorFlow1.x 環境構築

ローカルで機械学習環境を構築して,それをそのままAWSにもっていってGPUインスタンスで計算させたいよね. でも毎回環境を構築するのはいやだからインフラ部分もコードで管理したいよね.そんな記事.

対象者

機械学習(TensorFlow)を使い始めてPython好きになった人.

AWSで計算させたいけどインフラ周りが弱い人

環境

・ Ubuntu16.04

Vagrantのインストー

(現在 Vagrant 1.9.6 2017/6/30)

https://www.vagrantup.com/downloads.html

ubuntuなのでDebian系をインストール. ダウンロードしてインストールしたいが,コマンドライン上でやりたいのでファイルのURLだけコピーしてくる

# とりあえず
$ sudo apt-get update
$ sudo apt-get upgrade


# ダウンロード
$ wget https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.deb?_ga=2.120726475.212920124.1498783228-155634792.1498783228

# 変な名前なのでリネーム
$ mv vagrant_1.9.6_x86_64.deb?_ga=2.120726475.212920124.1498783228-155634792.1498783228 vagrant1.9.deb

# インストール
$ dpkg -i vagrant1.9.deb

# ヴァージョンの確認
$ vagrant -V
1.9.6

VirtualBoxのインストー

(現在 Virtualbox 5.1 2017/6/30)

VirtualBoxのインストールは簡単

https://www.virtualbox.org/wiki/Linux_Downloads

# とりあえず
$ sudo apt-get update
$ sudo apt-get upgrade

# インストール
$ sudo apt-get virtualbox

Vagrantプラグインのインストー

あとから使うのでプラグインを入れておく

# AWSを使う場合はAWS用のプロバイダーもインストールします。
$ vagrant plugin install vagrant-aws

# Chefをプロビジョニングするためのプラグインをインストールします。
$ vagrant plugin install vagrant-omnibus

# プラグインの確認
$ vagrant plugin list

Vagrantの仮想イメージをダウンロード

好きな弁当箱をダウンロードしなさい.いろいろあるよ.

https://app.vagrantup.com/bento

$ vagrant box add bento/ubuntu-16.04
# 時間がかかる

$ vagrant box list

(やっと)Vagrantの起動

# 作業ディレクトリを作っておくか
$ sudo mkdir ~/myVagrant
$ cd ~/myVagrant

# ダウンロードしている仮想イメージを確認する
$ vagrant box list

# 初期化
$ vagrant init bento/ubuntu-16.04

これで,~/myVagrantのしたにVagrantファイルができているはず.その下で起動させよう.

# 起動!
$ vagrant up

これだけ

# 起動しているか確認
vagrant status
# SSHで入る
$ vagrant ssh

(やっと)Vagrantで起動した仮想Ubuntu上にTensorFlow環境を作る!!

まずは仮想OSに入ります

$ vagrant ssh

本家をそのままpython環境を構築する. (2017/6/30)

ヴァージョンを確認.おそらく2.7がデフォルトで入っている.

$ python -V
python 2.7.x

もちろん時代はPython3.xなので,3.xで環境構築する

# pipやらvirtualenvやらを入れていく
sudo apt-get install -y python3-pip python3-dev python-virtualenv

# virtualenvで環境を作る.ここではenvを~/tensorflowディレクトリを作って指定している(適当)
virtualenv --system-site-packages -p python3 ~/tensorflow

# 仮想環境に入る
source ~/tensorflow/bin/activate

# pipを使ってtensorflowを入れる
pip3 install --upgrade pip3
pip3 install --upgrade tensorflow
# pip3 install --upgrade tensorflow-gpu

終わり.

tensorflowが入っているかテストしよう.

TensorFLowでとりあえずMnist

http://qiita.com/mine820/items/4c76d8293188f83000d6

以下のコードをPythonコンソールに適当にコピペしまくる.

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

計算結果

0.91 (成功!)

以上.

次回はこの手続きを全てvagrantで行わせます.

楽してAWSで計算させよう.