TensorFlow框架
TensorFlow官網(wǎng):https://tensorflow.google.cn/
TensorFlow中文社區(qū):http://www.tensorfly.cn/
目錄
一阴幌、Tensorflow基本概念
二、 TensorFlow 程序基本框架
三蹲姐、MNIST數(shù)據(jù)集手寫數(shù)字分類(淺層神經(jīng)網(wǎng)絡(luò))
四提茁、MNIST數(shù)據(jù)集手寫數(shù)字分類(深層神經(jīng)網(wǎng)絡(luò))
五、MNIST數(shù)據(jù)集手寫數(shù)字分類(卷積神經(jīng)網(wǎng)絡(luò))
一殿如、Tensorflow基本概念
1.安裝Tensorflow
- CPU版
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ TensorFlow
- GPU加速版
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ TensorFlow-GPU
2.TensorFlow數(shù)據(jù)類型
(1)Constant常量
node = tf.constant(常量值,tf.數(shù)據(jù)類型)
(2)Variable變量
W=tf.Variable([初始值],tf.數(shù)據(jù)類型)
(3)Placehoder占位符
node = tf.placeholder(tf.數(shù)據(jù)類型)
數(shù)據(jù)類型
3.Op計算節(jié)點
node_C = tf.運算(node_A,node_B)
示例:
node_A = tf.constant(10,tf.float32)
node_B = tf.constant(11,tf.float32)
node_C = tf.add(node_A,node_B)
sess = tf.Session()
print(sess.run(node_C))
運行結(jié)果
4.Session會話
- 1.啟動計算圖
sess = tf.Session()
- 2.關(guān)閉會話
sess.close()
- 3.上下文管理器
with tf.Session() as sess:
sess.run()
- 4.指定運行設(shè)備
with tf.Session() as sess:
with tf.device("/gpu:1"):
sess.run()
"/cpu:0": 機器的 CPU.
"/gpu:0": 機器的第一個 GPU(如果有的話)
"/gpu:1": 機器的第二個 GPU(如果有的話)
以此類推
二贡珊、 TensorFlow 程序基本框架
以線性方程回歸為例:
1.準備數(shù)據(jù)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,100)
y = 2 * x + np.random.randn(100) * 0.3
plt.plot(x,y)
plt.show()
x_test = np.linspace(-1,1,10)
y_test = 2*x_test
plt.plot(x_test,y_test)
plt.show()
2.搭建模型
X = tf.placeholder(dtype=tf.float32, shape=None)
Y = tf.placeholder(dtype=tf.float32, shape=None)
# 前向傳播
W = tf.Variable(tf.random_normal(shape=[1]), name='weight')
b = tf.Variable(tf.zeros(shape=[1]), name='bais')
z = tf.multiply(W, X) + b
# 后向傳播
cost = tf.reduce_mean(tf.square(Y-z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
train_epochs = 20
display_step = 2
init = tf.global_variables_initializer()
cost = tf.reduce_mean(tf.square(Y-z)):求計算值z和真實值Y的均方誤差
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
上面這行代碼, 通過梯度下降法握截,在底層調(diào)整權(quán)重W和偏置b飞崖,使得均方誤差cost最小(也即損失最薪靼)固歪。
3.迭代模型
with tf.Session() as sess:
sess.run(init)
for epoch in range(train_epochs):
for (x_s, y_s) in zip(x, y):
sess.run(optimizer, feed_dict={X: x_s, Y: y_s})
if epoch % display_step==0:
loss = sess.run(cost, feed_dict={X: x_test, Y: y_test})
print('epoch: ', epoch, ' loss:', loss)
# 預測
print("x=0.2, z=", sess.run(z, feed_dict={X: 0.2}))
4.完整流程代碼
以一元二次方程回歸為例:
import numpy as np
import tensorflow as tf
X = np.linspace(-1, 1, 300)[:, np.newaxis].astype('float32')
noise = np.random.normal(0, 0.05, X.shape).astype('float32')
y = np.square(X) - 0.5 + noise
def addConnect(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if not activation_function:
return Wx_plus_b
else:
return activation_function(Wx_plus_b)
connect_1 = addConnect(X, 1, 10, tf.nn.relu)
predict_y = addConnect(connect_1, 10, 1)
loss = tf.reduce_mean(tf.square(y - predict_y))
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
for step in range(201):
session.run(train)
if step % 20 == 0:
print(step, 'loss:', session.run(loss))
if step == 200:
predict_value = session.run(predict_y)
ax = plt.subplot(111)
ax.scatter(X, y)
plt.ylim(-0.65, 0.65)
lines = ax.plot(X, predict_value, 'r-', lw=5)
plt.title('step: %d loss: %.4f' % (step, session.run(loss)))
plt.show()
三、MNIST數(shù)據(jù)集手寫數(shù)字分類(淺層神經(jīng)網(wǎng)絡(luò))
1.數(shù)據(jù)準備
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)
2.數(shù)據(jù)觀察
- 變量mnist的方法和屬性
dir(mnist)[:]
(1)查看數(shù)據(jù)集信息
(2)查看數(shù)據(jù)形狀
images = mnist.train.images
type(images), images.shape
運行結(jié)果
從上面的運行結(jié)果可以看出胯努,在變量mnist.train中總共有55000個樣本牢裳,每個樣本有784個特征。
原圖片形狀為28×28,
28×28=784
叶沛,每個圖片樣本展平后則有784維特征蒲讯。
(3)繪制數(shù)字圖形
import matplotlib.pyplot as plt
image = mnist.train.images[1].reshape(-1, 28)
plt.subplot(131)
plt.imshow(image)
plt.axis('off')
plt.subplot(132)
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(133)
plt.imshow(image, cmap='gray_r')
plt.axis('off')
plt.show()
import matplotlib.pyplot as plt
import math
import numpy as np
def drawDigit(position, image, title):
# 元組解包
plt.subplot(*position)
# reshape(-1, 28):列數(shù)指定為28,行數(shù)自動計算
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
plt.title(title)
def batchDraw(batch_size):
# 特征和標簽
images,labels = mnist.train.next_batch(batch_size)
# 圖片數(shù)量
image_number = images.shape[0]
# 子圖行數(shù)列數(shù):圖片數(shù)量開平方
row_number = math.ceil(image_number ** 0.5)
column_number = row_number
plt.figure(figsize=(row_number, column_number))
for i in range(row_number):
for j in range(column_number):
index = i * column_number + j
if index < image_number:
position = (row_number, column_number, index+1)
image = images[index]
# 標簽值為獨熱編碼灰署,需用np.argmax()方法返回最大值索引(即數(shù)字標簽)
title = 'actual:%d' % (np.argmax(labels[index]))
drawDigit(position, image, title)
batchDraw(196)
plt.show()
3.搭建模型
由之前的數(shù)據(jù)觀察可知判帮,輸入層特征值為784個,則輸入層神經(jīng)元為784個溉箕。
Weights = tf.Variable(tf.zeros([784, 10]))
biases = tf.Variable(tf.zeros([1,10]))
predict_y = tf.nn.softmax(tf.matmul(X_holder, Weights) + biases)
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
此神經(jīng)網(wǎng)絡(luò)只有輸入層和輸出層晦墙,沒有隱藏層(單層神經(jīng)網(wǎng)絡(luò))。
4.迭代模型
for i in range(501):
images, labels = mnist.train.next_batch(batch_size)
session.run(train, feed_dict={X_holder:images, y_holder:labels})
if i % 25 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_value = session.run(accuracy, feed_dict={X_holder:mnist.test.images, y_holder:mnist.test.labels})
print('step:%d accuracy:%.4f' % (i, accuracy_value))
5.完整流程代碼
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)
Weights = tf.Variable(tf.zeros([784, 10]))
biases = tf.Variable(tf.zeros([1,10]))
predict_y = tf.nn.softmax(tf.matmul(X_holder, Weights) + biases)
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
session = tf.Session()
init = tf.global_variables_initializer()
session.run(init)
for i in range(500):
images, labels = mnist.train.next_batch(batch_size)
session.run(train, feed_dict={X_holder:images, y_holder:labels})
if i % 25 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_value = session.run(accuracy, feed_dict={X_holder:mnist.test.images, y_holder:mnist.test.labels})
print('step:%d accuracy:%.4f' % (i, accuracy_value))
6.模型測試
import math
import matplotlib.pyplot as plt
import numpy as np
def drawDigit_test(position, image, title, isTrue):
plt.subplot(*position)
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
if not isTrue:
plt.title(title, color='red')
else:
plt.title(title)
def batchDraw_test(batch_size):
images,labels = mnist.test.next_batch(batch_size)
predict_labels = session.run(predict_y, feed_dict={X_holder:images, y_holder:labels})
image_number = images.shape[0]
row_number = math.ceil(image_number ** 0.5)
column_number = row_number
plt.figure(figsize=(row_number+8, column_number+8))
for i in range(row_number):
for j in range(column_number):
index = i * column_number + j
if index < image_number:
position = (row_number, column_number, index+1)
image = images[index]
actual = np.argmax(labels[index])
predict = np.argmax(predict_labels[index])
isTrue = actual==predict
title = 'actual:%d\npredict:%d' % (actual, predict)
drawDigit_test(position, image, title, isTrue)
batchDraw_test(100)
plt.show()
從上面的運行結(jié)果可以看出肴茄,單層神經(jīng)網(wǎng)絡(luò)預測的準確率不足九成晌畅。
四、MNIST數(shù)據(jù)集手寫數(shù)字分類(深層神經(jīng)網(wǎng)絡(luò))
數(shù)據(jù)準備與數(shù)據(jù)觀察同上寡痰,此處不再贅述抗楔。
1.搭建模型
def addConnect(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size], stddev=0.01))
biases = tf.Variable(tf.zeros([1, out_size]))
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
return Wx_plus_b
else:
return activation_function(Wx_plus_b)
layer_1 = addConnect(X_holder, 784, 300, tf.nn.relu)
layer_2 = addConnect(layer_1, 300, 300, tf.nn.relu)
predict_y = addConnect(layer_2, 300, 10, tf.nn.softmax)
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.AdagradOptimizer(0.3)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
2.迭代模型
for i in range(1001):
images, labels = mnist.train.next_batch(batch_size)
session.run(train, feed_dict={X_holder:images, y_holder:labels})
if i % 50 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_value = session.run(accuracy, feed_dict={X_holder:mnist.test.images, y_holder:mnist.test.labels})
print('step:%d accuracy:%.4f' %(i, accuracy_value))
運行結(jié)果
3.完整流程代碼
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)
def addConnect(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.truncated_normal([in_size, out_size], stddev=0.01))
biases = tf.Variable(tf.zeros([1, out_size]))
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
return Wx_plus_b
else:
return activation_function(Wx_plus_b)
layer_1 = addConnect(X_holder, 784, 300, tf.nn.relu)
layer_2 = addConnect(layer_1, 300, 300, tf.nn.relu)
predict_y = addConnect(layer_2, 300, 10, tf.nn.softmax)
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.AdagradOptimizer(0.3)
train = optimizer.minimize(loss)
session = tf.Session()
init = tf.global_variables_initializer()
session.run(init)
for i in range(1000):
images, labels = mnist.train.next_batch(batch_size)
session.run(train, feed_dict={X_holder:images, y_holder:labels})
if i % 50 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_value = session.run(accuracy, feed_dict={X_holder:mnist.test.images, y_holder:mnist.test.labels})
print('step:%d accuracy:%.4f' % (i, accuracy_value))
4.測試模型
import math
import matplotlib.pyplot as plt
import numpy as np
def drawDigit_test(position, image, title, isTrue):
plt.subplot(*position)
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
if not isTrue:
plt.title(title, color='red')
else:
plt.title(title)
def batchDraw_test(batch_size):
images,labels = mnist.test.next_batch(batch_size)
predict_labels = session.run(predict_y, feed_dict={X_holder:images, y_holder:labels})
image_number = images.shape[0]
row_number = math.ceil(image_number ** 0.5)
column_number = row_number
plt.figure(figsize=(row_number+8, column_number+8))
for i in range(row_number):
for j in range(column_number):
index = i * column_number + j
if index < image_number:
position = (row_number, column_number, index+1)
image = images[index]
actual = np.argmax(labels[index])
predict = np.argmax(predict_labels[index])
isTrue = actual==predict
title = 'actual:%d\npredict:%d' % (actual, predict)
drawDigit_test(position, image, title, isTrue)
batchDraw_test(100)
plt.show()
從上面的運行結(jié)果可以看出,100個數(shù)字中只錯了3個拦坠,準確率為97%左右连躏。
五、MNIST數(shù)據(jù)集手寫數(shù)字分類(卷積神經(jīng)網(wǎng)絡(luò))
數(shù)據(jù)準備與數(shù)據(jù)觀察同上贞滨,此處不再贅述入热。
1.搭建模型
# 將784個特征變形為28×28的矩陣
X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
# 卷積層1
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name='conv1_Weights')
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]), name='conv1_biases')
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 卷積層2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name='conv2_Weights')
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]), name='conv2_biases')
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 全連接層1
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1), name='connect1_Weights')
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]), name='connect1_biases')
connect1_Wx_plus_b = tf.add(tf.matmul(connect1_flat, connect1_Weights), connect1_biases)
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
# 全連接層2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name='connect2_Weights')
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]), name='connect2_biases')
connect2_Wx_plus_b = tf.add(tf.matmul(connect1_activated, connect2_Weights), connect2_biases)
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
# 損失函數(shù)、優(yōu)化器、訓練過程
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.AdamOptimizer(0.0001)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
2.迭代模型
for i in range(1001):
train_images, train_labels = mnist.train.next_batch(200)
session.run(train, feed_dict={X_holder:train_images, y_holder:train_labels})
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_images, test_labels = mnist.test.next_batch(2000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('step:%d train accuracy:%.4f test accuracy:%.4f' % (i, train_accuracy, test_accuracy))
運行結(jié)果
3.完整流程代碼
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)
# -1表示通道數(shù)自適應(yīng)才顿,28,28表示圖像大小為28×28莫湘,1表示輸入特征圖數(shù)為1
X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
#convolutional layer 1
# 5,5表示卷積核大小為5×5,1表示輸入特征圖數(shù)為1郑气,32表示輸出特征圖數(shù)為32
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]))
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#convolutional layer 2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1))
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]))
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#full connected layer 1
# -1表示通道數(shù)自適應(yīng)幅垮,7*7表示單張?zhí)卣鲌D大小為7×7(原圖為28×28經(jīng)過2×2池化核池化2次),64表示輸入特征圖數(shù)為64
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1))
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]))
connect1_Wx_plus_b = tf.add(tf.matmul(connect1_flat, connect1_Weights), connect1_biases)
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
#full connected layer 2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]))
connect2_Wx_plus_b = tf.add(tf.matmul(connect1_activated, connect2_Weights), connect2_biases)
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
#loss and train
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.AdamOptimizer(0.0001)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)
for i in range(1001):
train_images, train_labels = mnist.train.next_batch(200)
session.run(train, feed_dict={X_holder:train_images, y_holder:train_labels})
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_images, test_labels = mnist.test.next_batch(2000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('step:%d train accuracy:%.4f test accuracy:%.4f' % (i, train_accuracy, test_accuracy))
4.保存模型
def save_model(session, model_name):
saver = tf.train.Saver()
save_path = saver.save(session, './models/{}.ckpt'.format(model_name))
print('Save to path:', save_path)
save_model(session, "mnist_cnn_ver001")
5.加載模型
session = tf.Session()
saver = tf.train.Saver()
saver.restore(session, 'models/mnist_cnn.ckpt')
print('load model successful')
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tf.reset_default_graph()
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)
X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
#convolutional layer 1
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name='conv1_Weights')
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]), name='conv1_biases')
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#convolutional layer 2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name='conv2_Weights')
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]), name='conv2_biases')
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#full connected layer 1
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1), name='connect1_Weights')
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]), name='connect1_biases')
connect1_Wx_plus_b = tf.add(tf.matmul(connect1_flat, connect1_Weights), connect1_biases)
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
#full connected layer 2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name='connect2_Weights')
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]), name='connect2_biases')
connect2_Wx_plus_b = tf.add(tf.matmul(connect1_activated, connect2_Weights), connect2_biases)
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
#loss and train
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
optimizer = tf.train.AdamOptimizer(0.0001)
train = optimizer.minimize(loss)
session = tf.Session()
saver = tf.train.Saver()
saver.restore(session, 'save_model/mnist_cnn.ckpt')
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('load model successful')
train_images, train_labels = mnist.train.next_batch(5000)
test_images, test_labels = mnist.test.next_batch(5000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('train accuracy:%.4f test accuracy:%.4f' %(train_accuracy, test_accuracy))
6.模型測試
import math
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
def drawDigit_test(position, image, title, isTrue):
plt.subplot(*position)
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
if not isTrue:
plt.title(title, color='red')
else:
plt.title(title)
def batchDraw_test(batch_size):
images,labels = mnist.test.next_batch(batch_size)
predict_labels = session.run(predict_y, feed_dict={X_holder:images, y_holder:labels})
image_number = images.shape[0]
row_number = math.ceil(image_number ** 0.5)
column_number = row_number
plt.figure(figsize=(row_number+8, column_number+8))
for i in range(row_number):
for j in range(column_number):
index = i * column_number + j
if index < image_number:
position = (row_number, column_number, index+1)
image = images[index]
actual = np.argmax(labels[index])
predict = np.argmax(predict_labels[index])
isTrue = actual==predict
title = 'actual:%d\npredict:%d' %(actual,predict)
drawDigit_test(position, image, title, isTrue)
batchDraw_test(100)
plt.show()
從上面的運行結(jié)果可以看出尾组,100個數(shù)字中只錯了1個忙芒,準確率為99%左右。