TensorBoard
TensorBoardを起動してみた。
(tensorflow) gocha124noMacBook-Air:~ cha124$ tensorboard -logdir=/Users/cha124/log/tensorboard
Traceback (most recent call last):
File "/Users/cha124/anaconda/bin/tensorboard", line 11, in <module>
sys.exit(main())
File "/Users/cha124/anaconda/lib/python3.6/site-packages/tensorflow/tensorboard/tensorboard.py", line 203, in main
tb = create_tb_app(plugins)
File "/Users/cha124/anaconda/lib/python3.6/site-packages/tensorflow/tensorboard/tensorboard.py", line 105, in create_tb_app
raise ValueError('A logdir must be specified. Run `tensorboard --help` for '
ValueError: A logdir must be specified. Run `tensorboard --help` for details and examples.
(tensorflow) gocha124noMacBook-Air:~ cha124$ tensorboard --logdir=./log/tensorboard
Starting TensorBoard b'54' at http://gocha124noMacBook-Air.local:6006
(Press CTRL+C to quit)
できた。でも起動しただけで「No scalar data was found.」が表示される。
続けてMNISTの分類の結果サマリ表示をやってみる。
tensorflow/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py
サマリ付きのMNISTを実行する。
gocha124noMacBook-Air:mnist cha124$ pwd
/Users/cha124/Downloads/tensorflow-1.2.0/tensorflow/examples/tutorials/mnist
gocha124noMacBook-Air:mnist cha124$ python mnist_with_summaries.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
2017-06-27 21:37:00.740000: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-27 21:37:00.740032: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-27 21:37:00.740040: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-27 21:37:00.740046: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Accuracy at step 0: 0.1334
Accuracy at step 10: 0.7607
Accuracy at step 20: 0.8301
Accuracy at step 30: 0.8548
Accuracy at step 40: 0.8846
Accuracy at step 50: 0.894
Accuracy at step 60: 0.9004
Accuracy at step 70: 0.9016
Accuracy at step 80: 0.9077
Accuracy at step 90: 0.9089
Adding run metadata for 99
Accuracy at step 100: 0.9187
Accuracy at step 110: 0.9186
Accuracy at step 120: 0.9216
Accuracy at step 130: 0.9244
Accuracy at step 140: 0.9269
Accuracy at step 150: 0.928
Accuracy at step 160: 0.931
Accuracy at step 170: 0.9303
Accuracy at step 180: 0.9301
Accuracy at step 190: 0.9304
Adding run metadata for 199
Accuracy at step 200: 0.9321
Accuracy at step 210: 0.9335
Accuracy at step 220: 0.9351
Accuracy at step 230: 0.9356
Accuracy at step 240: 0.9398
Accuracy at step 250: 0.9378
Accuracy at step 260: 0.941
Accuracy at step 270: 0.9437
Accuracy at step 280: 0.9411
Accuracy at step 290: 0.9412
Adding run metadata for 299
Accuracy at step 300: 0.9438
Accuracy at step 310: 0.9461
Accuracy at step 320: 0.9453
Accuracy at step 330: 0.9466
Accuracy at step 340: 0.9443
Accuracy at step 350: 0.9452
Accuracy at step 360: 0.9459
Accuracy at step 370: 0.947
Accuracy at step 380: 0.95
Accuracy at step 390: 0.95
Adding run metadata for 399
Accuracy at step 400: 0.9474
Accuracy at step 410: 0.9483
Accuracy at step 420: 0.9494
Accuracy at step 430: 0.9536
Accuracy at step 440: 0.9522
Accuracy at step 450: 0.9531
Accuracy at step 460: 0.9531
Accuracy at step 470: 0.9552
Accuracy at step 480: 0.9536
Accuracy at step 490: 0.9544
Adding run metadata for 499
Accuracy at step 500: 0.9551
Accuracy at step 510: 0.9546
Accuracy at step 520: 0.9557
Accuracy at step 530: 0.9586
Accuracy at step 540: 0.9575
Accuracy at step 550: 0.9577
Accuracy at step 560: 0.9586
Accuracy at step 570: 0.9577
Accuracy at step 580: 0.957
Accuracy at step 590: 0.959
Adding run metadata for 599
Accuracy at step 600: 0.9576
Accuracy at step 610: 0.9593
Accuracy at step 620: 0.9596
Accuracy at step 630: 0.959
Accuracy at step 640: 0.96
Accuracy at step 650: 0.9599
Accuracy at step 660: 0.9611
Accuracy at step 670: 0.9604
Accuracy at step 680: 0.9626
Accuracy at step 690: 0.9624
Adding run metadata for 699
Accuracy at step 700: 0.9637
Accuracy at step 710: 0.9619
Accuracy at step 720: 0.9643
Accuracy at step 730: 0.9611
Accuracy at step 740: 0.9633
Accuracy at step 750: 0.9643
Accuracy at step 760: 0.9634
Accuracy at step 770: 0.9601
Accuracy at step 780: 0.9616
Accuracy at step 790: 0.9622
Adding run metadata for 799
Accuracy at step 800: 0.9647
Accuracy at step 810: 0.964
Accuracy at step 820: 0.9637
Accuracy at step 830: 0.9643
Accuracy at step 840: 0.964
Accuracy at step 850: 0.9659
Accuracy at step 860: 0.9669
Accuracy at step 870: 0.9663
Accuracy at step 880: 0.9657
Accuracy at step 890: 0.9644
Adding run metadata for 899
Accuracy at step 900: 0.9624
Accuracy at step 910: 0.965
Accuracy at step 920: 0.9664
Accuracy at step 930: 0.9674
Accuracy at step 940: 0.9654
Accuracy at step 950: 0.9671
Accuracy at step 960: 0.9666
Accuracy at step 970: 0.9683
Accuracy at step 980: 0.9659
Accuracy at step 990: 0.9669
Adding run metadata for 999
gocha124noMacBook-Air:mnist cha124$
引数なしで実行する。/tmp/tensorflow/mnistディレクトリに以下出力される。
出力先ディレクトリを指定してTensorBoardを起動する。
tensorboard --logdir=/tmp/tensorflow/mnist
(tensorflow) gocha124noMacBook-Air:~ cha124$ tensorboard --logdir=/tmp/tensorflow/mnist
Starting TensorBoard b'54' at http://gocha124noMacBook-Air.local:6006
(Press CTRL+C to quit)
サマリ表示ができた。何を意味しているのかを調べてみる。
単語 | 調べた意味 |
SCALARS | |
GRAPHS | |
accuracy | 精度 |
cross entropy | 交差エントロピー。よくわかんない |
dropout | 過学習を防ぐディープラーニングのテクニック。一定の確率でランダムにニューロンを無視して学習を進める正則化 |
biases | 偏り。線形モデルを例にするとなんとなくわかる。「y = W * x + b」の表現の b に相当するのかな。 |
weights | 重み。線形モデルを例にするとなんとなくわかる。「y = W * x + b」の表現の W に相当するのかな。 |
さらにGraphを表示してみる。TensorBoardの上部タブのGRAPHSをクリックする。
ニューラルネットの構成が図示される。
できた。
TensorFlowその2
TensorFlowをインストールして、続きにGetting Startd With TensorFlowを試しておいたのでメモ。
Getting Started With TensorFlow | TensorFlow
英語はよくワカらない・・・
TensorFlow Coreチュートリアル。
Computional Graphは、固有名詞。
Computional Graphに、Tensor(0〜複数個)を入力して、出力のTensorを1つ得る。
API | こんな機能 |
tf.contrib | 開発中の機能が含まれる。将来変わる可能性がある |
tf.constant() | 定数のノードを作る |
tf.add() | ノードとノードを組み合わせる |
tf.placeholder() | 取り替えることができる記号を作る |
tf.Variable() | trainable(学習できる)なパラメータ |
tf.global_variables_initializer() | 変数を初期化する |
loss function | 損失関数 |
tf.square() | 自乗(二乗) |
tf.reduce_sum() | |
tf.assign() | trainableなパラメタに固定値をセット |
tf.train.GradientDescentOptimizer(0.01) | Gradient descent(最急降下法)での最適化アルゴリズム。 optimizer.minimize(loss)。 |
>>> node1 = tf.constant(3.0, dtype=tf.float32)
>>> node2 = tf.constant(4.0)
>>> print(node1, node2)
Tensor("Const_1:0", shape=(), dtype=float32) Tensor("Const_2:0", shape=(), dtype=float32)
>>> sess = tf.Session()
>>> print(sess.run([node1, node2]))
[3.0, 4.0]
>>> node3 = tf.add(node1, node2)
>>> print("node3: ", node3)
node3: Tensor("Add:0", shape=(), dtype=float32)
>>> print("sess.run(node3): ",sess.run(node3))
sess.run(node3): 7.0
>>> a = tf.placeholder(tf.float32)
>>> b = tf.placeholder(tf.float32)
>>> adder_node = a + b # + provides a shortcut for tf.add(a, b)
>>> print(sess.run(adder_node, {a: 3, b:4.5}))
7.5
>>> print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))
[ 3. 7.]
>>> add_and_triple = adder_node * 3.
>>> print(sess.run(add_and_triple, {a: 3, b:4.5}))
22.5
>>> W = tf.Variable([.3], dtype=tf.float32)
>>> b = tf.Variable([-.3], dtype=tf.float32)
>>> x = tf.placeholder(tf.float32)
>>> linear_model = W * x + b
>>> init = tf.global_variables_initializer()
>>> sess.run(init)
>>> print(sess.run(linear_model, {x:[1,2,3,4]}))
[ 0. 0.30000001 0.60000002 0.90000004]
>>> y = tf.placeholder(tf.float32)
>>> squared_deltas = tf.square(linearmodel - y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'linearmodel' is not defined
>>> squared_deltas = tf.square(linear_model - y)
>>> loss = tf.reduce_sum(squared_deltas)
>>> print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
23.66
>>> fixW = tf.assign(W, [-1.])
>>> fixb = tf.assign(b, [1.])
>>> sess.run([fixW, fixb])
[array([-1.], dtype=float32), array([ 1.], dtype=float32)]
>>> print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
0.0
>>>
>>>
>>> optimizer = tf.train.GradientDescentOptimizer(0.01)
>>> train = optimizer.minimize(loss)
>>> sess.run(init) # reset values to incorrect defaults.
>>> for i in range(1000):
... sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
...
>>> print(sess.run([W, b]))
[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
できた。
TensorFlow
TensorFlowをさわってみたメモ。うちのMacBookはAirなので性能よくないけどひと通りはさわってみれることを期待して、セットアップから実施してみる。
うちのMacにはAnacondaがインストールしてあるので、そこに追加でセットアップ。
Installing TensorFlow on Mac OS X | TensorFlow
手順通りにセットアップできた。Pythonは3.6なので3.6のをインストール。
conda create - tensorflow
source activate tensorflow
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl
gocha124noMacBook-Air:~ cha124$ conda create -n tensorflow
Fetching package metadata ...........
Solving package specifications:
Package plan for installation in environment /Users/cha124/anaconda/envs/tensorflow:
Proceed ([y]/n)? y
#
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate this environment, use:
# > source deactivate tensorflow
#
gocha124noMacBook-Air:~ cha124$ source activate tensorflow
(tensorflow) gocha124noMacBook-Air:~ cha124$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl
Collecting tensorflow==1.2.0 from https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl
Downloading https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl (33.6MB)
100% |████████████████████████████████| 33.6MB 17kB/s
Collecting markdown==2.2.0 (from tensorflow==1.2.0)
Downloading Markdown-2.2.0.tar.gz (236kB)
100% |████████████████████████████████| 245kB 926kB/s
Collecting six>=1.10.0 (from tensorflow==1.2.0)
Downloading six-1.10.0-py2.py3-none-any.whl
Collecting protobuf>=3.2.0 (from tensorflow==1.2.0)
Downloading protobuf-3.3.0.tar.gz (271kB)
100% |████████████████████████████████| 276kB 845kB/s
Collecting bleach==1.5.0 (from tensorflow==1.2.0)
Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting werkzeug>=0.11.10 (from tensorflow==1.2.0)
Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
100% |████████████████████████████████| 317kB 969kB/s
Collecting wheel>=0.26 (from tensorflow==1.2.0)
Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
100% |████████████████████████████████| 71kB 1.4MB/s
Collecting html5lib==0.9999999 (from tensorflow==1.2.0)
Downloading html5lib-0.9999999.tar.gz (889kB)
100% |████████████████████████████████| 890kB 538kB/s
Collecting backports.weakref==1.0rc1 (from tensorflow==1.2.0)
Downloading backports.weakref-1.0rc1-py3-none-any.whl
Collecting numpy>=1.11.0 (from tensorflow==1.2.0)
Downloading numpy-1.13.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
100% |████████████████████████████████| 4.5MB 120kB/s
Collecting setuptools (from protobuf>=3.2.0->tensorflow==1.2.0)
Downloading setuptools-36.0.1-py2.py3-none-any.whl (476kB)
100% |████████████████████████████████| 481kB 835kB/s
Building wheels for collected packages: markdown, protobuf, html5lib
Running setup.py bdist_wheel for markdown ... done
Stored in directory: /Users/cha124/Library/Caches/pip/wheels/b9/4f/6c/f4c1c5207c1d0eeaaf7005f7f736620c6ded6617c9d9b94096
Running setup.py bdist_wheel for protobuf ... done
Stored in directory: /Users/cha124/Library/Caches/pip/wheels/1b/42/a0/4c7343df5b629ec9c75655468dce7652b28026896b0209ba55
Running setup.py bdist_wheel for html5lib ... done
Stored in directory: /Users/cha124/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built markdown protobuf html5lib
Installing collected packages: markdown, six, setuptools, protobuf, html5lib, bleach, werkzeug, wheel, backports.weakref, numpy, tensorflow
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.2.0 numpy-1.13.0 protobuf-3.3.0 setuptools-36.0.1 six-1.10.0 tensorflow-1.2.0 werkzeug-0.12.2 wheel-0.29.0
記述に従って、pythonでハローワールドを実行してみる。
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
(tensorflow) gocha124noMacBook-Air:~ cha124$ python
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.costant('Hello, TensorFlow!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'costant'
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-06-26 10:04:15.851049: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-26 10:04:15.851146: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-26 10:04:15.851270: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-26 10:04:15.851324: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
できた。
集合知(その3)
10、予測モデルをつくる
一般的な3つの分類アルゴリズム
回帰アルゴリズム
決定木
決定木で作成されたモデルはif-thenルール。
情報エントロピー
- 事例を全て正、または負に分類する属性は完全。エントロピー=(1,0)=0。
- 事例を、等しく、2つに分ける属性は、エントピー=(1,1)=1。
情報利得
- 1つ目の属性でデータを分割した時の情報エントロピーの減少=情報利得
ナイーブベイズ分類
確率論の基本的な概念。ある出来事が起こる確率は、0から1の間の数値。
数値が高いほど、その出来事が起こる確率も高い。
確率=Pr:probability
ベイズの定理
Pr{ A and B } = Pr{ A } Pr{ B | A } = Pr{ B } Pr{ A | B }
条件付き独立性を仮定して、事例ごとに予測をまとめたのがナイーブベイズ分類器?と理解。
ビリーフネットワーク
ビリーフネットワークは、ナイーブベイズ分析をグラフを用いて表記したもの
形式的には非循環有向グラフ(DAG:directed acyclic graph)。ノードは確率変数、ノード間のリンクは親と子のあいだの条件付き依存関係をあらわす。非循環有向ネットワークとも呼ばれる。
各ノードは親ノードがもたらす影響を数値化した条件付き確率表(CPT:conditional probablity table)を持つ。
単結合型と複結合型
単結合ネットワーク向けの推論アルゴリズム
- クラスタリング
- 場合分け
- 確率的シミュレーション
ベイズ法にもとづく確率の解釈
- 確率は一般的には「ある出来事が起こる頻度をあらわす」だが、
- ベイズ的解釈・主観的解釈は「ある出来事が起こるだろうと、ある人が信じる度合い」である
WEKA
分類器クラス
決定木、J48
weka.classifiers.trees.J48
ナイーブベイズ
weka.classifiers.bayes.NaiveBayesSimple
ベイズネット
weka.classifiers.bayes.BayesNet
回帰
線形回帰
多層パーセプトロン(MLP)
入力は第1層の各ノードに入る
1つのノードからの出力は2段階の演算で決定される
- ノードの各入力値に重みを掛けた値を合計
- この値を非線形活性化関数を使ってノードの出力値に変換
活性化関数
- シグモイド(sigmoid)や双曲線正接関数(tan hyperbolic function)
各ノードには閾値があり、ノードが出力するには入力と重みの積の総和が閾値を超える必要がある
通常、ニューラルネットワークの学習
- 誤差逆伝搬(back-propagation)アルゴリズムが使われる
- このアルゴリズムは勾配探索法(gradient search)を用いて、学習データと予測値の二乗誤差の和が最小になるように、ネットワークの重みを探索。
通常、局所的な最適解に収束するため、大域的な最適解に収束する保証はない
データを過学習する危険があるので、交差検定(cross-validation)で確認する
動径基底関数(RBF)
RBFは2層で構成。
- 入力空間から隠れユニット空間への変換は非線形
- 隠れユニット空間から出力空間への変換は線形
隠れノード
出力層ノード
RBFの学習
- 隠れ層での教師なし学習。通常ガウスクラスタの学習にはk平均法アルゴリズム
- 出力層での教師あり学習。出力層での学習は誤差逆伝搬法か逆行列演算のどちらか。
- ガウスクラスタから出力ノードへの結合重みは、線形回帰を用いて学習
学習速度が断然早い
WEKA
weka.classifiers.functions.LinearRegression
weka.classifiers.functions.MultilayerPerceptron
weka.classifiers.functions.RBFNetwork