機械学習/MacでTensorFlowを使う のバックアップ差分(No.7)


  • 追加された行はこの色です。
  • 削除された行はこの色です。
*はじめに [#ja3312a3]

ここでは、Googleが公開しているオープン・ソース・ソフトウェア (OSS) のTensorFlowをMac (OS X) にインストールして使います。


*環境 [#kd6c49b9]

-OS X Yosemite 10.10.5
-Python 3.5.1
-TensorFlow 0.7

*Python3のインストール(アップグレード) [#zf2975fa]

まず、Python3を最新版にアップグレードします。
下記のサイトから''Mac OS X 64-bit/32-bit installer''をダウンロードして、インストール。
-[[Python 3.5.1:https://www.python.org/downloads/release/python-351/]]


*TensorFlowのインストール [#seb185d6]

基本的にはここに書いてある通りなんですが、なぜかsixのeasy_installが先にできなかったので、順序を逆にしました。
-[[Download and Setup:https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html#download-and-setup]] - TensorFlow

#geshi(sh){{
$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp35-none-any.whl
sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp35-none-any.whl
$ sudo easy_install --upgrade six
}}

*Virtualenvのインストール [#ab5cc2c7]

#geshi(sh){{
$ sudo pip3 install --upgrade virtualenv
$ virtualenv --system-site-packages /Users/Shared/tools/tensorflow
$ source /Users/Shared/tools/tensorflow/bin/activate
(tensorflow)$ pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp35-none-any.whl
(tensorflow)$ deactivate
}}
私の研究室では、共有フォルダーのtoolsというフォルダー (/Users/Shared/tools/) に、みんなで使うツールをインストールしています。
/Users/Shared/tools/tensorflow を ~/tensorflow などTensorFlowをインストールしたいフォルダーに変更してください。



*TensorFlowを動かす [#te2b5012]

TesorFlowのサイトに掲載されているHello Worldを動かします。
-[[Test the TensorFlow installation:https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html#test-the-tensorflow-installation]] - TensorFlow

#geshi(sh){{
$ source /Users/Shared/tools/tensorflow/bin/activate
(tensorflow) $ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>> quit()
(tensorflow) $ deactivate
}}


*irisで線形回帰 [#ka2954e2]
*TensorFlowでIrisをやってみる [#ka2954e2]

まずは、おなじみのirisデータセットで線形回帰をやってみましょう。
irisデータセットは、4つの説明変数(花びらの長さ、幅、がく片の長さ、幅)とカテゴリー (setosa, versicolor, virginica) からなる分類問題用のデータセットですが、4つの説明変数を3つの説明変数と1つの目標変数にして、回帰問題用のデータセットにします。
TensorFlowの最初のチュートリアルは手書き数字認識のMNISTですが、irisデータセットで分類をやってみます。

UCI Machine Learning Repositoryからファイルをダウンロードします。

**データの準備 [#p76609a7]
irisデータセットは、4つの説明変数(花びらの長さ、幅、がく片の長さ、幅)とカテゴリー (setosa, versicolor, virginica) からなるデータセットです。

UCI Machine Learning Repositoryからファイル iris.data をダウンロードします。
中身はこんな感じです。
#geshi(txt){{
$ head -5 iris.data
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
}}

ソースコードはこんな感じです。
3クラスの分類なので、カテゴリーのラベルを3次元の0/1ベクトルに変換します。
つまり、setosaは 1,0,0、versicolorは 0,1,0、virginicaは 0,0,1 とします。

以下のページを参考にしました。
-[[TensorFlowで回帰をやってみる:http://qiita.com/syoamakase/items/db883d7ebad7a2220233]] - Qiita
irisデータには、それぞれ、50個ずつ150個のデータが並んでいるので、次のように変換します。
#geshi(sh){{
$ head -50 iris.data | awk -F, '{printf("%s,%s,%s,%s,1,0,0\n",$1,$2,$3,$4);}' > iris.csv
$ head -100 iris.data | tail -50 | awk -F, '{printf("%s,%s,%s,%s,0,1,0\n",$1,$2,$3,$4);}' >> iris.csv
$ head -150 iris.data | tail -50 | awk -F, '{printf("%s,%s,%s,%s,0,0,1\n",$1,$2,$3,$4);}' >> iris.csv
}}

これで、データの準備は完了です。
このデータをdataフォルダーにおいておきます。

**分類用ソースコード [#xeaf4449]

TensorFlowのTutorialsについてる最初の手書き文字データセットMNIST用のサンプルをほとんどそのまま使ったソースコードはこんな感じです。
#geshi(python){{
# Iris classification for TensorFlow
#
#
# Copyright 2016 Tohgoroh Matsui All Rights Reserved.
#
# This includes software developed by Google, Inc.
# licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf
import numpy as np

# 損失関数
def loss(o, y):
  with tf.name_scope('loss') as scope:
    loss = tf.reduce_mean(tf.square(o - y))
  return loss

# for TensorBoard
def variable_summaries(var, name):
  with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.scalar_summary('mean/' + name, mean)
    with tf.name_scope('stddev'):
      stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))
    tf.scalar_summary('sttdev/' + name, stddev)
    tf.scalar_summary('max/' + name, tf.reduce_max(var))
    tf.scalar_summary('min/' + name, tf.reduce_min(var))
    tf.histogram_summary(name, var)

# CSVファイルの読み込み
iris = np.genfromtxt('iris.data', delimiter=",")

# 学習データ (train) とテストデータ (test) に分割
N = 100
x_train, x_test = np.vsplit(iris[:,0:3].astype(np.float32), [N])
y_train, y_test = np.vsplit(iris[:,3:4].astype(np.float32), [N])
# パラメーター
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('samples',       120,    'Number of training samples.')
flags.DEFINE_integer('max_steps',     1001,   'Number of steps to run trainer.')
flags.DEFINE_float(  'learning_rate', 0.001,  'Initial learning rate.')
flags.DEFINE_float(  'dropout',       0.9,    'Keep probability for training dropout.')
flags.DEFINE_string( 'data_dir',      'data', 'Directory for storing data.')
flags.DEFINE_string( 'summaries_dir', 'log',  'Summaries directory.')


# 入力層
n0 = 3
x = tf.placeholder("float", shape=[None, n0])
def train():
  # CSVファイルの読み込み
  iris = np.genfromtxt(FLAGS.data_dir + '/iris.csv', delimiter=",", skip_header=1)

# 中間層1(活性化関数はReLU)
n1 = 64
with tf.name_scope('l1') as scope:
  w1 = tf.Variable(tf.truncated_normal([n0, n1], stddev=0.1), name="w1")
  b1 = tf.Variable(tf.constant(1.0, shape=[n1]), name="b1")
  o1 = tf.nn.relu(tf.matmul(x, w1) + b1)
  # データをシャッフル
  np.random.shuffle(iris)
  
  # 学習データ (train) とテストデータ (test) に分割
  x_train, x_test = np.vsplit(iris[:,0:4].astype(np.float32), [FLAGS.samples])
  y_train, y_test = np.vsplit(iris[:,4:7].astype(np.float32), [FLAGS.samples])

# 中間層2(活性化関数はReLU)
n2 = 64
with tf.name_scope('l2') as scope:
  w2 = tf.Variable(tf.truncated_normal([n1, n2], stddev=0.1), name="w2")
  b2 = tf.Variable(tf.constant(1.0, shape=[n2]), name="b2")
  o2 = tf.nn.relu(tf.matmul(o1, w2) + b2)

# 出力層(線形結合)
n3 = 1
with tf.name_scope('l3') as scope:
  w3 = tf.Variable(tf.truncated_normal([n2, n3], stddev=0.1), name="w3")
  b3 = tf.Variable(tf.constant(1.0, shape=[n3]), name="b3")
  o3 = tf.matmul(o2, w3) + b3
  # セッション
  sess = tf.InteractiveSession()

  # 入力
  with tf.name_scope('input'):
    x  = tf.placeholder(tf.float32, shape=[None, 4])
  
  # 出力
  with tf.name_scope('output'):
    y_ = tf.placeholder(tf.float32, shape=[None, 3])
    
  # ドロップアウト
  with tf.name_scope('dropout'):
    keep_prob = tf.placeholder(tf.float32)
    tf.scalar_summary('dropout_keep_probability', keep_prob)
  
  
  # 重み(係数)
  def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
  
  # バイアス(定数項)
  def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
    
  # for TensorBoard
  def variable_summaries(var, name):
    with tf.name_scope('summaries'):
      mean = tf.reduce_mean(var)
      tf.scalar_summary('mean/' + name, mean)
      with tf.name_scope('stddev'):
        stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))
      tf.scalar_summary('sttdev/' + name, stddev)
      tf.scalar_summary('max/' + name, tf.reduce_max(var))
      tf.scalar_summary('min/' + name, tf.reduce_min(var))
      tf.histogram_summary(name, var)

loss_op  = loss(o3, y_train)
train_op = tf.train.AdagradOptimizer(0.01).minimize(loss_op)
min = float("inf")

  # ニューラル・ネットワークの層
  def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
    with tf.name_scope(layer_name):
      with tf.name_scope('weights'):
        weights = weight_variable([input_dim, output_dim])
        variable_summaries(weights, layer_name + '/weights')
      with tf.name_scope('biases'):
        biases = bias_variable([output_dim])
        variable_summaries(biases, layer_name + '/biases')
      with tf.name_scope('Wx_plus_b'):
        preactivate = tf.matmul(input_tensor, weights) + biases
        tf.histogram_summary(layer_name + '/pre_activations', preactivate)
      activations = act(preactivate, 'activation')
      tf.histogram_summary(layer_name + '/acctivations', activations)
      return activations
      

# 初期化
init_op = tf.initialize_all_variables()
  # 入力層
  hidden1 = nn_layer(x, 4, 64, 'layer1')
  dropped1 = tf.nn.dropout(hidden1, keep_prob)
  
  # 中間層
  hidden2 = nn_layer(dropped1, 64, 64, 'layer2')
  dropped2 = tf.nn.dropout(hidden2, keep_prob)
  
  # 出力層
  y = nn_layer(dropped2, 64, 3, 'layer3', act=tf.nn.softmax)


# セッション
with tf.Session() as sess:
  sess.run(init_op)
  for i in range(100001):
    loss = sess.run(loss_op, feed_dict={x:x_train})
    sess.run(train_op, feed_dict={x:x_train})
    if loss < min:
      min  = loss
      best = sess.run(o3, feed_dict={x:x_test})
  # クロス・エントロピー
  with tf.name_scope('cross_entropy'):
    #diff = y_ * tf.log(y)
    #with tf.name_scope('total'):
    #  cross_entropy = -tf.reduce_mean(diff)
    cross_entropy = -tf.reduce_sum(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))
    tf.scalar_summary('cross entropy', cross_entropy)

  # 学習
  with tf.name_scope('train'):
    train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(cross_entropy)

  # 精度
  with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
      correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    with tf.name_scope('accuracy'):
      accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.scalar_summary('accuracy', accuracy)


  # for TensorBoard
  merged = tf.merge_all_summaries()
  train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train', sess.graph)
  test_writer  = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test')
  tf.initialize_all_variables().run()


  # データとドロップアウト・キープ率の切り替え
  def feed_dict(train):
    if train:
      xs, ys = x_train, y_train
      k  = FLAGS.dropout
    else:
      xs, ys = x_test, y_test
      k  = 1.0
    return {x: xs, y_: ys, keep_prob: k}


  # 学習ルーチン
  for i in range(FLAGS.max_steps):
    summary, acc, cp = sess.run([merged, accuracy, cross_entropy], feed_dict=feed_dict(False))
    test_writer.add_summary(summary, i)
    summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))
    train_writer.add_summary(summary, i)
    if i == 0 or i % np.power(10, np.floor(np.log10(i))) == 0:
      r = np.corrcoef(best.flatten(), y_test.flatten())[0][1]
      print('{}: Loss = {}, R = {}'.format(i, min, r))
      print('Accuracy and Cross Entropy at step %s: %s, %s' % (i, acc, cp))


def main(_):
  if tf.gfile.Exists(FLAGS.summaries_dir):
    tf.gfile.DeleteRecursively(FLAGS.summaries_dir)
  tf.gfile.MakeDirs(FLAGS.summaries_dir)
  train()

if __name__ == '__main__':
  tf.app.run()
}}

実行すると、次のようになります。
MNISTからいくつか変更点があります。
-iris.csvからデータを読み込む
-データをシャッフルして120個を訓練データに、残りの30個をテスト・データにする
-入力は4次元、出力は3次元
-中間層(活性化関数はReLU)を追加
-入力層は4x64ユニット、中間層は64x64ユニット、出力層は64x3ユニット
-クロス・エントロピーの定義を変更(元の定義だとNaNになってしまうことがあるため)
-TensorBoardにグラフを出力しない(エラーになってしまうため)
-10回中9回学習して1回テストを行うのではなく、毎回学習とテストを行う
-標準出力に精度とクロス・エントロピーを出力


**実行 [#f0d2b637]
#geshi(sh){{
(tensorflow) $ python3 tfRegression.py 
0: Loss = 0.9924622178077698, R = -0.31376540185534374
1: Loss = 0.33512526750564575, R = -0.2809933838930298
2: Loss = 0.3103932738304138, R = -0.2605662262997624
3: Loss = 0.29998070001602173, R = -0.19988817587575336
4: Loss = 0.29054442048072815, R = -0.10261393047830965
5: Loss = 0.2815440893173218, R = -0.0030378746322473078
6: Loss = 0.2728765904903412, R = 0.06743248160241787
7: Loss = 0.26452258229255676, R = 0.11218694086822772
8: Loss = 0.25640469789505005, R = 0.14083619953594895
9: Loss = 0.24850055575370789, R = 0.15955179537740188
10: Loss = 0.2407849133014679, R = 0.17325851369183962
20: Loss = 0.17123618721961975, R = 0.2119044088136728
30: Loss = 0.11361732333898544, R = 0.21242390033328104
40: Loss = 0.07034626603126526, R = 0.21351552138337554
50: Loss = 0.04281529411673546, R = 0.2112161148516869
60: Loss = 0.027972474694252014, R = 0.21323103613228045
70: Loss = 0.02100631780922413, R = 0.2133350814777594
80: Loss = 0.01804741658270359, R = 0.21420981216605972
90: Loss = 0.016845906153321266, R = 0.2153937757021539
100: Loss = 0.01631508581340313, R = 0.21719647880323853
200: Loss = 0.01521082129329443, R = 0.2440402497636383
300: Loss = 0.014608824625611305, R = 0.2645834921419263
400: Loss = 0.014166736975312233, R = 0.28157404933081204
500: Loss = 0.013829934410750866, R = 0.2952680005290892
600: Loss = 0.013562848791480064, R = 0.30675864601619857
700: Loss = 0.013374903239309788, R = 0.3164113669058739
800: Loss = 0.013220920227468014, R = 0.3264013737506945
900: Loss = 0.013091418892145157, R = 0.3328174293176383
1000: Loss = 0.012949838303029537, R = 0.33802548623013406
2000: Loss = 0.012516467832028866, R = 0.3696120433568608
3000: Loss = 0.012403683736920357, R = 0.3809822832215991
4000: Loss = 0.012310308404266834, R = 0.38904775116540224
5000: Loss = 0.012245077639818192, R = 0.39475342114404327
6000: Loss = 0.0121763302013278, R = 0.39984150610626895
7000: Loss = 0.012104801833629608, R = 0.40494473659532704
8000: Loss = 0.012028640136122704, R = 0.4100277288577437
9000: Loss = 0.011952037923038006, R = 0.41544311557772695
10000: Loss = 0.011868388392031193, R = 0.42219982940390616
20000: Loss = 0.011160679161548615, R = 0.46866687115166755
30000: Loss = 0.0105402497574687, R = 0.4942357990758039
40000: Loss = 0.010395915247499943, R = 0.5026822424224162
50000: Loss = 0.01029052771627903, R = 0.49972020218055674
60000: Loss = 0.010193731635808945, R = 0.48942378308652223
70000: Loss = 0.010111675597727299, R = 0.4797740509869646
80000: Loss = 0.009985744953155518, R = 0.4579861414250584
90000: Loss = 0.009862487204372883, R = 0.43707398862896835
100000: Loss = 0.009671863168478012, R = 0.4074325332085266
$ source /Users/Shared/tools/tensorflow/bin/activate
(tensorflow) $ python3 tfc_iris.py 
Accuracy and Cross Entropy at step 0: 0.325, 132.007
Accuracy and Cross Entropy at step 1: 0.341667, 128.534
Accuracy and Cross Entropy at step 2: 0.275, 128.389
Accuracy and Cross Entropy at step 3: 0.258333, 126.72
Accuracy and Cross Entropy at step 4: 0.408333, 124.004
Accuracy and Cross Entropy at step 5: 0.283333, 125.581
Accuracy and Cross Entropy at step 6: 0.316667, 122.983
Accuracy and Cross Entropy at step 7: 0.45, 120.915
Accuracy and Cross Entropy at step 8: 0.508333, 119.768
Accuracy and Cross Entropy at step 9: 0.558333, 118.938
Accuracy and Cross Entropy at step 10: 0.616667, 117.524
Accuracy and Cross Entropy at step 20: 0.716667, 103.102
Accuracy and Cross Entropy at step 30: 0.791667, 86.5923
Accuracy and Cross Entropy at step 40: 0.766667, 71.941
Accuracy and Cross Entropy at step 50: 0.883333, 59.7601
Accuracy and Cross Entropy at step 60: 0.9, 51.6576
Accuracy and Cross Entropy at step 70: 0.908333, 45.2846
Accuracy and Cross Entropy at step 80: 0.941667, 39.4717
Accuracy and Cross Entropy at step 90: 0.916667, 34.6735
Accuracy and Cross Entropy at step 100: 0.95, 28.6601
Accuracy and Cross Entropy at step 200: 0.975, 10.2124
Accuracy and Cross Entropy at step 300: 0.975, 8.91163
Accuracy and Cross Entropy at step 400: 0.975, 7.53242
Accuracy and Cross Entropy at step 500: 0.983333, 7.67529
Accuracy and Cross Entropy at step 600: 0.966667, 8.6403
Accuracy and Cross Entropy at step 700: 0.966667, 7.6847
Accuracy and Cross Entropy at step 800: 0.975, 5.9193
Accuracy and Cross Entropy at step 900: 0.983333, 7.32202
Accuracy and Cross Entropy at step 1000: 0.983333, 4.97897
}}


**TensorBoardによるログの確認 [#nd118cd7]
TensorBoardでログを確認します。

次のようにしてTensorBoardを起動すると、6006番ポートでHTTPサーバーが起動します。
#geshi(sh){{
(tensorflow) $ tensorboard --logdir=log
}}

Webブラウザーで [[http://localhost:6006]] にアクセスします。
#ref(./iris_c.png,nolink,25%)

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS