Jupyter Notebookでいじって学ぶTensorFlow – MNIST For ML Beginners
をpython3 でやってみたのでめも

環境

linux mate 19
Python 3.6.7
conda 4.5.12

実行

    必要なライブラリを読み込む

必要なライブラリをimport

%matplotlib inline

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
    MNIST画像を読み込む

MNISTは、機械学習などによく使われる手書き文字の画像データセット。最初はダウンロードに少し時間がかかる。

old_v = tf.logging.get_verbosity()
tf.logging.set_verbosity(tf.logging.ERROR)

from tensorflow.contrib.learn.python.learn.datasets import mnist as mnist_loader

mnist = mnist_loader.read_data_sets("MNIST_data/", one_hot=True)

Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

手書き画像をひとつ見て。

plt.imshow(mnist.train.images[8].reshape([28, 28]))
plt.gray()
image.png

ちなみに、たまたま見つけたやばいやつ

plt.imshow(mnist.train.images[65].reshape([28, 28]))
plt.gray()
image.png

これは、、、8?

ラベルをひとつ見てみる。

plt.imshow(mnist.train.labels[8].reshape([1, -1]))
plt.gray()
image.png
plt.imshow(mnist.train.labels[65].reshape([1, -1]))
plt.gray()
image.png

どうやらさっきのよくわからんのは「2」らしい・・・

3. モデルを定義しよう

Softmax回帰の実装

入力x

x = tf.placeholder("float",[None,784])

weightとbiases

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

簡単に言うと、

y = W_{0}x_{0}+W_{1}x_{1}+...+W_{9}x_{9}+b

モデルの定義

y = tf.nn.softmax(tf.matmul(x,W)+b)

4. モデルの訓練方法を定義

訓練方法の実装

placeholderの定義。 計算で求めるyを以下のような式で表されるとする。

y_ = tf.placeholder("float",[None,10])

と、交差エントロピー(誤差)は

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

で表される

訓練方法の定義

learning_rate = 0.01
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

learning_rate の変動で cross_entropy が最小になるように急速降下法で訓練する。

5. モデルを実際に訓練

init = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run(init)

訓練の実行

# 訓練パラメータ
n_train = 1000
n_batch = 100

# グラフ描画用
fig, ax = plt.subplots(1, 1, figsize=(15, 5))
xvalues = np.arange(n_train)
yvalues = np.zeros(n_train)
lines, = ax.plot(xvalues, yvalues, label="cross_entropy")

for i in range(n_train):

    # バッチ学習
    batch_xs, batch_ys = mnist.train.next_batch(n_batch)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    # グラフ描画用
    yvalues[i] = cross_entropy.eval(feed_dict={x: mnist.test.images[0:100], y_: mnist.test.labels[0:100]})
    lines.set_data(xvalues, yvalues)
    ax.set_ylim((yvalues.min(), yvalues.max()))
    plt.legend()

# グラフの描写
plt.pause(.00001)
image.png

6. 作成したモデルを評価

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))

bool値のテンソル。。。つまり、正解を「1」、誤答を「0」として

{{1,1,1,1,0,0,1,1,1,1,10,1,....,1},
{1,0,1,1,1,0,1,1,1,1,10,1,....,1},
{1,1,1,1,1,0,1,1,1,1,10,1,....,1},
{1,1,1,1,0,0,1,1,1,1,10,1,....,1},
{1,1,1,1,0,0,1,1,1,1,10,1,....,1},
{1,1,1,1,0,0,1,1,1,1,10,1,....,1},
{1,1,1,1,0,0,1,1,1,1,10,1,....,1},
{1,1,1,1,0,0,1,1,1,1,10,1,....,1}.....
{1,1,1,1,0,0,1,1,1,1,10,1,....,1}}

みたいな状態。これをfloatにキャストして平均をとる

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

0.9211

w = W.eval().T
fig = plt.figure(figsize=(10, 4))

for i in range(10):
    ax = fig.add_subplot(2, 5, i + 1)
    ax.imshow(w[i].reshape([28, 28]), cmap="seismic")
image.png
    作成したモデルを使って分類
# 手書き画像のインデックス
index_test_image = 5

# 分類
result = y.eval(feed_dict={x: [mnist.test.images[index_test_image]]})
print(result)

# 描画の準備
fig = plt.figure(figsize=(8, 6))

# テスト画像を描画
ax0 = fig.add_subplot(2, 1, 1)
ax0.imshow(mnist.test.images[index_test_image].reshape([28, 28]))

# 分類結果を描画
ax1 = fig.add_subplot(2, 1, 2)
ax1.imshow(result)

[[8.7355545e-08 9.8954684e-01 2.1719618e-03 2.6251078e-03 1.2656899e-05
3.2387452e-05 2.0649401e-05 3.0584279e-03 2.3757515e-03 1.5612924e-04]]

image.png

98.954%の確率で「1」

これとかちょっとおもしろい

# 手書き画像のインデックス
index_test_image = 77

# 分類
result = y.eval(feed_dict={x: [mnist.test.images[index_test_image]]})
print(result)

# 描画の準備
fig = plt.figure(figsize=(8, 6))

# テスト画像を描画
ax0 = fig.add_subplot(2, 1, 1)
ax0.imshow(mnist.test.images[index_test_image].reshape([28, 28]))

# 分類結果を描画
ax1 = fig.add_subplot(2, 1, 2)
ax1.imshow(result)

[[1.1343922e-03 2.7609523e-03 6.7110407e-01 4.1867211e-03 4.3900713e-04
9.8160030e-03 2.5389763e-03 2.6510671e-01 6.9887578e-03 3.5924386e-02]]

image.png

「2」の確率が67,11% 「7」の確率が26.51%

どうやら、「2」のしたの横棒を除けば「7」に見えるらしい

广告
将在 10 秒后关闭
bannerAds