TensorFlow基礎(chǔ)

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%左右。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末讳侨,一起剝皮案震驚了整個濱河市呵萨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌跨跨,老刑警劉巖潮峦,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異勇婴,居然都是意外死亡忱嘹,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門耕渴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拘悦,“玉大人,你說我怎么就攤上這事橱脸〈∶祝” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵添诉,是天一觀的道長屁桑。 經(jīng)常有香客問我,道長吻商,這世上最難降的妖魔是什么掏颊? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任糟红,我火速辦了婚禮艾帐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盆偿。我一直安慰自己柒爸,他們只是感情好,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布事扭。 她就那樣靜靜地躺著捎稚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上今野,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天葡公,我揣著相機與錄音,去河邊找鬼条霜。 笑死催什,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的宰睡。 我是一名探鬼主播蒲凶,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拆内!你這毒婦竟也來了旋圆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤麸恍,失蹤者是張志新(化名)和其女友劉穎灵巧,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抹沪,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡孩等,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了采够。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肄方。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蹬癌,靈堂內(nèi)的尸體忽然破棺而出权她,到底是詐尸還是另有隱情,我是刑警寧澤逝薪,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布隅要,位于F島的核電站,受9級特大地震影響董济,放射性物質(zhì)發(fā)生泄漏步清。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一虏肾、第九天 我趴在偏房一處隱蔽的房頂上張望廓啊。 院中可真熱鬧,春花似錦封豪、人聲如沸谴轮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽第步。三九已至疮装,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間粘都,已是汗流浹背廓推。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留翩隧,地道東北人受啥。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像鸽心,于是被迫代替她去往敵國和親滚局。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容