15
Google_TensorFlow でででででで長長長 1

Google TensorFlowで遊んでみた①

Embed Size (px)

Citation preview

Page 1: Google TensorFlowで遊んでみた①

Google_TensorFlowで遊んでみた①長谷川

1

Page 2: Google TensorFlowで遊んでみた①

TensorFlow って何? TensorFlow で何ができるの? チュートリアルやってみたhttps://www.tensorflow.org/versions/0.6.0/tutorials/index.html

2

もくじ

Page 3: Google TensorFlowで遊んでみた①

Data flow グラフを用いた数値計算のための OSS 。 グラフの Node( 節点 ) は数学的操作 グラフの Edge( 枝 ) はその間で通信される多次元データ配列 ( テンソル )  を表している。⇒【詳細は次のページ】 機械学習とディープニューラルネットワークの研究のために開発されたが、その他の様々な用途にも使われている。公式https://www.tensorflow.org/動画https://youtu.be/oZikw5k_2FM

TensorFlowって何?

3

Page 4: Google TensorFlowで遊んでみた①

有向グラフ (Node, Edge) で数学的計算を記述する。 Node

◦ 数学的計算、変数の読み書き、 データの入出力。 Edge

◦ Node 間の入出力。 Node を異なるデバイスに割り当て、非同期に並列実行が可能。

4

Data Flowグラフって何?

Edge

Node

Node

Page 5: Google TensorFlowで遊んでみた①

よくわからないのでチュートリアルで確かめてみよう。

5

TensorFlowで何ができるの?

Page 6: Google TensorFlowで遊んでみた①

【環境】ubuntu 14.04 x86_64 、 Python 3.4pip-installation でインストールhttps://www.tensorflow.org/versions/master/get_started/os_setup.html#pip-installation【トラブル】最初に CentOS でやったときpip で TensorFlow インストール時に下記のエラーメッセージが表示された。tensorflow-0.6.0-cp34-none-linux_x86_64.whl is not a supported wheel on this platform.「 tensorflow-0.6.0-cp34-none-linux_x86_64.whl 」をダウンロードして、「 tensorflow-0.6.0-cp35-none-linux_x86_64.whl 」にリネームしてインストールした。

6

チュートリアルやってみた

Page 7: Google TensorFlowで遊んでみた①

①MNIST For ML Beginners機械学習 (Machine Learning) 初心者向けの手書き数字の分類チュートリアル。MNIST はシンプルな画像の集合で、下記のような手描きの数字で構成される。

Softmax Regression( ソフトマックス回帰 ) で数字を推定する。7

チュートリアルやってみた①MNIST For ML Beginners

Page 8: Google TensorFlowで遊んでみた①

手順 1MNIST データをダウンロードmnist.tran : training データ 55,000mnist.test : test データ 10,000mnist.validation : validation データ 5,000手順 228pixel×28pixel の画像を 28×28=784 の配列に変換。配列の要素は 0( 白 ) ~ 1( 黒 ) の実数。

8

チュートリアルやってみた①MNIST For ML Beginners

Page 9: Google TensorFlowで遊んでみた①

手順 2 続きmnist.trans.xs 、 mnist.trans.ys を下記のように定義する。

モデル化完了。次はどのように推定するか。9

チュートリアルやってみた①MNIST For ML Beginners

各画像の数字各画像の配列データ

Page 10: Google TensorFlowで遊んでみた①

Softmax Regressions 1/3 STEP1 : Evidence の入力 STEP2 : Evidence を確率に変換するその数字である確率が高い / 弱い Evidence に分類される。

10

チュートリアルやってみた①MNIST For ML Beginners

赤:その数字である確率が高い Evidence青:その数字である確率が低い Evidence

Page 11: Google TensorFlowで遊んでみた①

Softmax Regressions 2/3Wi :重みbi :数字 i のバイアスj :画像 x の配列のインデックスy :予測確率確率分布に正規化式変形

11

チュートリアルやってみた①MNIST For ML Beginners

Page 12: Google TensorFlowで遊んでみた①

Softmax Regressions   3/3先ほどの式をモデル化、数式化するとこうなる。

計算効率化のため、ベクトル化する。

12

チュートリアルやってみた①MNIST For ML Beginners

Page 13: Google TensorFlowで遊んでみた①

ソース 1/3>>> import tensorflow as tf>>> import os#MNIST データダウンロード用の py をダウンロード>>> os.system("curl https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py -o input_data.py") % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 7309 100 7309 0 0 11158 0 --:--:-- --:--:-- --:--:-- 111580>>> import input_data#MNIST データダウンロード&格納>>> mnist = input_data.read_data_sets('MNIST_data', one_hot=True)Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.Extracting MNIST_data/train-images-idx3-ubyte.gzSuccessfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.Extracting MNIST_data/train-labels-idx1-ubyte.gzSuccessfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.Extracting MNIST_data/t10k-images-idx3-ubyte.gzSuccessfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

13

チュートリアルやってみた①MNIST For ML Beginners

Page 14: Google TensorFlowで遊んでみた①

ソース 2/3>>> 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>>> cross_entropy = -tf.reduce_sum(y_*tf.log(y))#backpropagation algorithm>>> train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)# 初期化>>> init = tf.initialize_all_variables()>>> sess = tf.Session()>>> sess.run(init)

14

チュートリアルやってみた①MNIST For ML Beginners

Page 15: Google TensorFlowで遊んでみた①

ソース 3/3#training>>> for i 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))#TRUE/FALSE を 1/0 に変換>>> accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))# 精度を表示>>> print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})0.9113※range を 5000 にしたら 0.9243 になった

15

チュートリアルやってみた①MNIST For ML Beginners