【TensorFlow快速入門系列02】進階操作

1 Tensorboard

1.1 網絡結構

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 = 64
# 一個週期批次數
n_batch = mnist.train.num_examples // batch_size

# 命名空間:輸入
with tf.name_scope('input'):
    x = tf.placeholder(tf.float32,[None,784], name='x-input')
    y = tf.placeholder(tf.float32,[None,10], name='y-input')

# 命名空間:網絡層
with tf.name_scope('layer'):
    with tf.name_scope('weights'):
        W = tf.Variable(tf.truncated_normal([784,10], stddev=0.1))
    with tf.name_scope('biases'):
        b = tf.Variable(tf.zeros([10]) + 0.1)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        prediction = tf.nn.softmax(wx_plus_b)

# 命名空間:損失
with tf.name_scope('loss'):
    loss = tf.losses.mean_squared_error(y, prediction)

# 命名空間:訓練
with tf.name_scope('train'):
    train = tf.train.GradientDescentOptimizer(0.3).minimize(loss)

# 命名空間:精度
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    # 變量初始化
    sess.run(tf.global_variables_initializer())
    # 寫下網絡結構日誌
    writer = tf.summary.FileWriter('logs/',sess.graph) 

查看步驟:

  1. 打開cmd,切換tensorflow環境
  2. 輸入命令:tensorboard --logdir=存放路徑
  3. 打開瀏覽器,輸入cmd生成的網頁地址

在這裏插入圖片描述

1.2 記錄數據

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 = 64
n_batch = mnist.train.num_examples // batch_size

def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        # 平均值
        tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        # 標準差
        tf.summary.scalar('stddev', stddev)
        # 最大值
        tf.summary.scalar('max', tf.reduce_max(var))
        # 最小值
        tf.summary.scalar('min', tf.reduce_min(var))
        # 直方圖
        tf.summary.histogram('histogram', var)

with tf.name_scope('input'):
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')
    
with tf.name_scope('layer'):
    with tf.name_scope('wights'):
        W = tf.Variable(tf.zeros([784,10]),name='W')
        variable_summaries(W)
    with tf.name_scope('biases'):    
        b = tf.Variable(tf.zeros([10]),name='b')
        variable_summaries(b)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W) + b
    with tf.name_scope('softmax'):
        prediction = tf.nn.softmax(wx_plus_b)

with tf.name_scope('loss'):
    loss = tf.losses.mean_squared_error(y, prediction)
    tf.summary.scalar('loss',loss)
    
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.3).minimize(loss)

init = tf.global_variables_initializer()

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar('accuracy',accuracy)
        
# 合併所有的summary
merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('logs/',sess.graph)

    for batch in range(5001):
        batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
        summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})
            
        writer.add_summary(summary,batch)
        if batch % 1000==0:
            acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
            print("Iter " + str(batch) + ",Testing Accuracy " + str(acc))

Iter 0,Testing Accuracy 0.2874
Iter 1000,Testing Accuracy 0.8802
Iter 2000,Testing Accuracy 0.896
Iter 3000,Testing Accuracy 0.9017
Iter 4000,Testing Accuracy 0.9058
Iter 5000,Testing Accuracy 0.9084
在這裏插入圖片描述

2 模型保存與載入

2.1 Checkpoint方式

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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# 定義saver用於載入模型,在指定路徑下最多保留5個模型
saver = tf.train.Saver(max_to_keep=5)

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    # 載入訓練好的模型參數文件
    saver.restore(sess,'models/my_model.ckpt')
    print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
    
    for epoch in range(11):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
        # 保存模型,global_step可以用來表示模型的訓練次數或者訓練週期數
        saver.save(sess,'models/my_model.ckpt',global_step=epoch)

Iter 0,Testing Accuracy 0.9306
Iter 1,Testing Accuracy 0.9304
Iter 2,Testing Accuracy 0.9298
Iter 3,Testing Accuracy 0.9306
Iter 4,Testing Accuracy 0.9321
Iter 5,Testing Accuracy 0.9306
Iter 6,Testing Accuracy 0.9313
Iter 7,Testing Accuracy 0.9305
Iter 8,Testing Accuracy 0.9319
Iter 9,Testing Accuracy 0.9317
Iter 10,Testing Accuracy 0.9326

在這裏插入圖片描述

2.2 Protocol_buffer方式

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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784], name='x-input')
y = tf.placeholder(tf.float32,[None,10], name='y-input')

W = tf.Variable(tf.truncated_normal([784,10],stddev=0.1))
b = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(x,W)+b, name='output')

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(0.001).minimize(loss, name='train')

init = tf.global_variables_initializer()

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32), name='accuracy')

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(11):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
    # 保存模型參數和結構,把變量變成常量
    # output_node_names設置可以輸出的tensor
    output_graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['output','accuracy'])
    # 保存模型到目錄下的models文件夾中
    with tf.gfile.FastGFile('pb_models/my_model.pb',mode='wb') as f:
        f.write(output_graph_def.SerializeToString())

Iter 0,Testing Accuracy 0.9028
Iter 1,Testing Accuracy 0.9142
Iter 2,Testing Accuracy 0.9185
Iter 3,Testing Accuracy 0.923
Iter 4,Testing Accuracy 0.9249
Iter 5,Testing Accuracy 0.9262
Iter 6,Testing Accuracy 0.9278
Iter 7,Testing Accuracy 0.9276
Iter 8,Testing Accuracy 0.9284
Iter 9,Testing Accuracy 0.9292
Iter 10,Testing Accuracy 0.9287

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

mnist = input_data.read_data_sets("MNIST_data",one_hot=True)# 載入模型
with tf.gfile.FastGFile('pb_models/my_model.pb', 'rb') as f:
    # 創建一個圖
    graph_def = tf.GraphDef()
    # 把模型文件載入到圖中
    graph_def.ParseFromString(f.read())
    # 載入圖到當前環境中
    tf.import_graph_def(graph_def, name='')with tf.Session() as sess:
    # 根據tensor的名字獲取到對應的output,":0"是保存模型參數時自動加上的,所以這裏也要寫上
    output = sess.graph.get_tensor_by_name('output:0')
    # 根據tensor的名字獲取到對應的accuracy
    accuracy = sess.graph.get_tensor_by_name('accuracy:0')
    print(sess.run(accuracy,feed_dict={'x-input:0':mnist.test.images,'y-input:0':mnist.test.labels}))

0.9287

3 CNN案例

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 = 64
n_batch = mnist.train.num_examples // batch_size

x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])# 初始化權值
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1) 
    return tf.Variable(initial)# 初始化偏置
def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial)# 卷積層
def conv2d(x,W):
    # x維度:高,寬,通道 
    # W維度:卷積核高,卷積核寬,輸入通道,輸出通道
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')# 池化層
def max_pool_2x2(x):
    # ksize:[1,x,y,1]
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')# x轉4D:批次,高,寬,通道
x_image = tf.reshape(x,[-1,28,28,1])# 初始化第一個卷積層的權值和偏置
W_conv1 = weight_variable([5,5,1,32]) # 5*5的採樣窗口,輸入通道數是1,輸出通道數是32
b_conv1 = bias_variable([32])# 把x_image和權值向量進行卷積,再加上偏置值,然後應用於relu激活函數,再進行max_pooling
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)# 初始化第二個卷積層的權值和偏置
W_conv2 = weight_variable([5,5,32,64]) # 5*5的採樣窗口,輸入通道數是32,輸出通道數是64
b_conv2 = bias_variable([64])# 把h_pool1和權值向量進行卷積,再加上偏置值,然後應用於relu激活函數,再進行max_pooling
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)# 初始化第一個全連接層的權值
# 上一層輸出7*7*64個神經元,全連接層有1024個神經元
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([1024])# 把池化層2的輸出扁平化爲1維
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
# 求第一個全連接層的輸出
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1)# keep_prob用來表示神經元的輸出概率
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)# 初始化第二個全連接層
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])# 計算輸出
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)# 交叉熵損失
cross_entropy = tf.losses.softmax_cross_entropy(y,prediction)
# 使用Adam優化器
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 結果存放在一個布爾列表中
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
# 求準確率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(21):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
​
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
        print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))

Iter 0, Testing Accuracy= 0.9634
Iter 1, Testing Accuracy= 0.9775
Iter 2, Testing Accuracy= 0.9785
Iter 3, Testing Accuracy= 0.9844
Iter 4, Testing Accuracy= 0.9862
Iter 5, Testing Accuracy= 0.9863
Iter 6, Testing Accuracy= 0.9872
Iter 7, Testing Accuracy= 0.9875
Iter 8, Testing Accuracy= 0.9894
Iter 9, Testing Accuracy= 0.9898
Iter 10, Testing Accuracy= 0.9903
Iter 11, Testing Accuracy= 0.9896
Iter 12, Testing Accuracy= 0.9905
Iter 13, Testing Accuracy= 0.9909
Iter 14, Testing Accuracy= 0.9893
Iter 15, Testing Accuracy= 0.9899
Iter 16, Testing Accuracy= 0.9892
Iter 17, Testing Accuracy= 0.9913
Iter 18, Testing Accuracy= 0.9918
Iter 19, Testing Accuracy= 0.9915
Iter 20, Testing Accuracy= 0.9908

4 LSTM案例

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

mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)# 輸入圖片是28*28
n_inputs = 28 
max_time = 28 
lstm_size = 100 # 隱層單元
n_classes = 10 # 10個類別
batch_size = 64 
n_batch = mnist.train.num_examples // batch_size 
​
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
​
weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))# 定義RNN網絡
def RNN(X,weights,biases):
    # 維度:批次,行,列
    inputs = tf.reshape(X,[-1,max_time,n_inputs])
    # 定義LSTM
    lstm_cell = tf.nn.rnn_cell.LSTMCell(lstm_size)
    outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
    results = tf.nn.softmax(tf.matmul(final_state[1],weights) + biases)
    return results
        
prediction= RNN(x, weights, biases)  

loss = tf.losses.softmax_cross_entropy(y,prediction)
train_step = tf.train.AdamOptimizer(1e-3).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

init = tf.global_variables_initializer()with tf.Session() as sess:
    sess.run(init)
    for epoch in range(11):
        for batch in range(n_batch):
            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))

Iter 0, Testing Accuracy= 0.922
Iter 1, Testing Accuracy= 0.9511
Iter 2, Testing Accuracy= 0.9543
Iter 3, Testing Accuracy= 0.9663
Iter 4, Testing Accuracy= 0.97
Iter 5, Testing Accuracy= 0.9705
Iter 6, Testing Accuracy= 0.9734
Iter 7, Testing Accuracy= 0.973
Iter 8, Testing Accuracy= 0.9786
Iter 9, Testing Accuracy= 0.9745
Iter 10, Testing Accuracy= 0.9766

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章