# -*- coding: utf-8 -*-
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("E:/mnist/", one_hot=True)
def weight(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1),#truncated_nomal表示從截斷的正態分佈中輸出隨機值,stddev是標準差,shape表示生成長量的維度。
name ='W')
def bias(shape):
return tf.Variable(tf.constant(0.1, shape=shape) #bias的常數設置爲0.1
, name = 'b')
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], #x是輸入,W是filter,strides卷積時在圖像每一維的步長,這是一個一維的向量,[ 1, strides, strides, 1],第一位和最後一位固定必須是1
padding='SAME') #‘SAME’是考慮邊界,不足的時候用0去填充周圍,‘VALID’則不考慮
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1],
strides=[1,2,2,1],
padding='SAME')
with tf.name_scope('Input_Layer'):
x = tf.placeholder("float",shape=[None, 784] #shape=[None,784] 表示列是784,行數不定,shape是數據形狀
,name="x")
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.name_scope('C1_Conv'):
W1 = weight([5,5,1,16])
b1 = bias([16])
Conv1=conv2d(x_image, W1)+ b1
C1_Conv = tf.nn.relu(Conv1 )
with tf.name_scope('C1_Pool'):
C1_Pool = max_pool_2x2(C1_Conv)
with tf.name_scope('C2_Conv'):
W2 = weight([5,5,16,36])
b2 = bias([36])
Conv2=conv2d(C1_Pool, W2)+ b2
C2_Conv = tf.nn.relu(Conv2)
with tf.name_scope('C2_Pool'):
C2_Pool = max_pool_2x2(C2_Conv)
with tf.name_scope('D_Flat'):
D_Flat = tf.reshape(C2_Pool, [-1, 1764])#7*7*56=1766
with tf.name_scope('D_Hidden_Layer'):
W3= weight([1764, 128])
b3= bias([128])
D_Hidden = tf.nn.relu(
tf.matmul(D_Flat, W3)+b3)
D_Hidden_Dropout= tf.nn.dropout(D_Hidden, ####若在結構上不定義keep_prob,則需要在結構中定義keep_prob = tf.placeholder(tf.float32),###在session()中用feed_dict()喂參數進去
keep_prob=0.8)####在結構上直接定義參數大小,直接賦值
with tf.name_scope('Output_Layer'):
W4 = weight([128,10])
b4 = bias([10])
y_predict= tf.nn.softmax(
tf.matmul(D_Hidden_Dropout,
W4)+b4)
with tf.name_scope("optimizer"):
y_label = tf.placeholder("float", shape=[None, 10],
name="y_label")
loss_function = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits
(logits=y_predict ,
labels=y_label))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001) \
.minimize(loss_function)
#saver_path=saver.save(sess, "E:/123456789")##保存模型
with tf.name_scope("evaluate_model"):
correct_prediction = tf.equal(tf.argmax(y_predict, 1),
tf.argmax(y_label, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
trainEpochs = 30##30個週期,每個週期對訓練的樣本55000樣本進行訓練
batchSize = 100#批尺寸100
totalBatchs = int(mnist.train.num_examples/batchSize)##55000/100=550
epoch_list=[];accuracy_list=[];loss_list=[];
from time import time
startTime=time()
log_dir = "D:/Temp/logs4/"
with tf.Session() as sess:
train_writer = tf.summary.FileWriter(log_dir + "train/", sess.graph) # 記錄默認圖
test_writer = tf.summary.FileWriter(log_dir + "test/")
sess.run(tf.global_variables_initializer())
for epoch in range(trainEpochs):
for i in range(totalBatchs):
batch_x, batch_y = mnist.train.next_batch(batchSize)
sess.run(optimizer,feed_dict={x: batch_x,
y_label: batch_y})
loss,acc = sess.run([loss_function,accuracy],
feed_dict={x: mnist.validation.images,
y_label: mnist.validation.labels})
epoch_list.append(epoch)
loss_list.append(loss);accuracy_list.append(acc)
print("Train Epoch:", '%02d' % (epoch+1), \
"Loss=","{:.9f}".format(loss)," Accuracy=",acc)
constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ["evaluate_model/Mean"])#convert_variables_to_constants函數,會將計算圖中的變量取值以常量的形式保存。在保存模型文件的時候,我們只是導出了GraphDef部分,GraphDef保存了從輸入層到輸出層的計算過程。在保存的時候,通過convert_variables_to_constants函數來指定保存的節點名稱而不是張量的名稱,“add:0”是張量的名稱而"add"表示的是節點的名稱。
with tf.gfile.FastGFile("D:/wsj.pb", mode='wb') as f: #這兩行可以改爲:model_f=tf.gfile.FastGFile("D:/wsj.pb", mode='wb')
f.write(constant_graph.SerializeToString()) #model_f.write(constant_graph.SerializeToString())
#duration =time()-startTime
#print("Train Finished takes:",duration)
直接貼代碼,先記錄下來,防止以後忘了,以後研究好了,繼續不出這篇文章。