DAY 24
0

## [第24天] Tensorflow MNIST練習(2)

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 讀入 MNIST
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
x_train = mnist.train.images
y_train = mnist.train.labels
x_test = mnist.test.images
y_test = mnist.test.labels

# 設定參數
learning_rate = 0.5
training_steps = 1000
batch_size = 100    # 每次100張圖片否則時間太久
logs_path = 'TensorBoard/'
n_features = x_train.shape[1]
n_labels = y_train.shape[1]

# 建立 Feeds
with tf.name_scope('Inputs'):
x = tf.placeholder(tf.float32, [None, n_features], name = 'Input_Data')
with tf.name_scope('Labels'):
y = tf.placeholder(tf.float32, [None, n_labels], name = 'Label_Data')

# 建立 Variables
with tf.name_scope('ModelParameters'):
W = tf.Variable(tf.zeros([n_features, n_labels]), name = 'Weights')
b = tf.Variable(tf.zeros([n_labels]), name = 'Bias')

# 開始建構深度學習模型
with tf.name_scope('Model'):
# Softmax
prediction = tf.nn.softmax(tf.matmul(x, W) + b) ＃已經等於把隱藏層定義y=wx+b寫進去了
with tf.name_scope('CrossEntropy'):
# Cross-entropy
loss = tf.reduce_mean(-tf.reduce_sum(y * tf.log(prediction), reduction_indices = 1))
tf.summary.scalar("Loss", loss)
with tf.name_scope('Accuracy'):
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))＃T or F
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))   ＃轉化布林取平均
tf.summary.scalar('Accuracy', acc)

# 初始化
init = tf.global_variables_initializer()

# 開始執行運算
sess = tf.Session()
sess.run(init)

# 將視覺化輸出
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter(logs_path, graph = tf.get_default_graph())

# 訓練
for step in range(training_steps):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)  ＃批次學習
sess.run(optimizer, feed_dict = {x: batch_xs, y: batch_ys})
if step % 50 == 0:   ＃每隔50步印出
print(sess.run(loss, feed_dict = {x: batch_xs, y: batch_ys}))
summary = sess.run(merged, feed_dict = {x: batch_xs, y: batch_ys})

print("---")
# 準確率
print("Accuracy: ", sess.run(acc, feed_dict={x: x_test, y: y_test}))

print(sess.run(y[0,:], feed_dict={x: x_test, y: y_test}))

import matplotlib.pyplot as plt
import numpy as np
first_test_img = np.reshape(x_test[0, :], (28, 28))
plt.matshow(first_test_img, cmap = plt.get_cmap('gray'))  #把第一張圖畫出來
plt.show()

sess.close()

guowenchens-iMac:~ neo\$ --logdir='TensorBoard/'