Tensorflowの画像認識のチュートリアル
キータにも書いたが、自分のメモとしてTensorflowの画像認識のチュートリアルのサンプルに結果表示をいれたものをメモしておきたい。
コードの説明はコメントの形で埋め込んだ。
#coding:UTF-8 # rdtensorflowsample.swift # KatagiriSo import input_data import numpy as np import matplotlib.pyplot as plt mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) import tensorflow as tf ## 画像は28*28ピクセル = 784ピクセル ## この画像から0,...,9という番号を判断したい。 Pixels = 28*28 Pattern = 10 LearningRate = 0.01 # 入力変数定義 float32型で、(None, 784)の2階のテンソルを作る。 with tf.name_scope("input"): x = tf.placeholder(tf.float32, [None, Pixels],name="input") with tf.name_scope("Weight"): # 重み 初期値ゼロで(784,10)の2階のテンソルを作る。 W = tf.Variable(tf.zeros([Pixels, Pattern]), name="weights") # バイアス 初期値0で(10)の1階のテンソルをつくる b = tf.Variable(tf.zeros([Pattern]), name="bias") with tf.name_scope("Output"): # ソフトマックス活性化関数 y = tf.nn.softmax(tf.matmul(x, W) + b, name="softmax") with tf.name_scope("optimizer"): # 正解 right_answer = tf.placeholder(tf.float32, [None, Pattern], name="answer") # 交差エントロピーの定義 cross_entropy = -tf.reduce_sum(right_answer*tf.log(y), name="loss") # 勾配降下法で重みを調整する # 学習比率 0.01 optimaizerNode = tf.train.GradientDescentOptimizer(LearningRate).minimize(cross_entropy) #optimaizerNode = tf.train.AdamOptimizer().minimize(cross_entropy) ## 結果表示 ## with tf.name_scope("result"): # それぞれの分布から最も確率の高いもの同士を見比べ同じであるかを調べる。 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(right_answer,1)) # 正答率を計算する。 accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"), name="accuracy") # グラフ表示用 tf.scalar_summary("entropy", cross_entropy) tf.scalar_summary("accuracy", accuracy) tf.histogram_summary("weights", W) summary_op = tf.merge_all_summaries() #初期化 init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) summary_writer = tf.train.SummaryWriter('data',sess.graph) # 学習中 for i in range(1000): # データを取得 1回で100個のデータ(バッチ)を持ってくる batch_images, batch_answers = mnist.train.next_batch(100) # ステップを実行 placeholderであるx,right_anserにデータを入れてoptimaizerNodeを実行する。 sess.run(optimaizerNode, feed_dict={x: batch_images, right_answer: batch_answers}) # 100回に一度現状の重みでの交差エントロピーを計算しグラフ表示用に追加する。 if i % 100 == 0: loss = sess.run(cross_entropy, feed_dict={x: batch_images, right_answer: batch_answers}) acc = sess.run(accuracy, feed_dict={x: batch_images, right_answer: batch_answers}) print("交差エントロピー%f, 精度 %f" % (loss, acc)) summary_str = sess.run(summary_op, feed_dict={x: batch_images, right_answer: batch_answers}) summary_writer.add_summary(summary_str, i) # 精度表示 result = sess.run(accuracy, feed_dict={x: mnist.test.images, right_answer: mnist.test.labels}) print("最終結果 精度 %f" % result) # 重み w_val = sess.run(W) b_val = sess.run(b) #print("重み") #print(w_val[40]) #print("バイアス") #print(b_val) fig = plt.figure(figsize=(8,6)) for c,(image, ans) in enumerate(zip(mnist.test.images, mnist.test.labels)): #image = mnist.test.images[0] #ans = mnist.test.labels[0] if c > 20-1: break subplot = fig.add_subplot(4,5,c+1) subplot.set_xticks([]) subplot.set_yticks([]) # 28*28 濃淡0から1, グレースケール、ピクセルのデータ補完を無効化 subplot.imshow(image.reshape(28,28), vmin=0,vmax=1, cmap=plt.cm.gray_r, interpolation="nearest") # 今の重みで計算してみる y_val = (sess.run(tf.argmax(y,1), feed_dict={x:[image]}))[0] ans_val = (sess.run(tf.argmax(right_answer,1), feed_dict={right_answer:[ans]}))[0] #print(y_val) title = "pred %d ans %d" % (y_val, ans_val) subplot.set_title(title) plt.show()
また、機械学習の初歩の理論的な説明を書いてみたものはこちら
https://www.researchgate.net/publication/313842779_jixiexuexinochubunoshaojie