# @time : 2020/1/5 22:39
# @author : x1aolata
# @file : mnist_train.py
# @script : 訓練簡單手寫數字識別模型-直接全連線-用於測試模型儲存與轉化
from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.framework import graph_util
# number 1 to 10 data 載入資料集
mnist = input_data.read_data_sets('mnist_data', one_hot=true)
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict=)
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict=)
return result
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)
xs = tf.placeholder(tf.float32, [none, 784], name='x_input') / 255. # 28x28
ys = tf.placeholder(tf.float32, [none, 10], name='y-input')
keep_prob = tf.constant(0.5)
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# print(x_image.shape) # [n_samples, 28,28,1]
## fc1 layer ##
w_fc1 = weight_variable([28 * 28, 1024])
b_fc1 = bias_variable([1024])
# [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
h_fc1 = tf.nn.relu(tf.matmul(xs, w_fc1) + b_fc1)
# h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
## fc2 layer ##
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1, w_fc2) + b_fc2)
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) # loss
train_step = tf.train.adamoptimizer(1e-4).minimize(cross_entropy)
sess = tf.session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
pre_num = tf.argmax(prediction, 1, output_type='int32', name="output")
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict=)
if i % 50 == 0:
print(compute_accuracy(
mnist.test.images, mnist.test.labels))
# 儲存訓練好的模型
# 形參output_node_names用於指定輸出的節點名稱,output_node_names=['output']對應pre_num=tf.argmax(y,1,name="output"),
output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['output'])
with tf.gfile.fastgfile('model/mnist_01.pb', mode='wb') as f: # 』wb』中w代表寫檔案,b代表將資料以二進位制方式寫入檔案。
f.write(output_graph_def.serializetostring())
全連線層的作用 全連線層實現
將圖1 堆疊可以得到含有多個隱藏層的全連線層,如圖2所示。因為當前層的輸入要與前一層的輸出相匹配 所以,要保證當前層的神經元個數與前一層的輸出結點數相同。即,圖二中隱藏層1有256個神經元,隱藏層2有128個神經元,則隱藏層1中每個神經元的輸出節點數為12。下面,以圖2為例,用不同的方式實現全連線層...
全連線層fully connected layer
參考 全連線層 fc,fully connected layer 在整個卷積神經網路中起到分類器的作用,如果說卷積層,池化層和啟用函式層等操作是將原始資料對映到隱層特徵空間的話,全連線層則其到將學到的 分布式特徵表示 對映到樣本標記空間的作用,在實際使用中,全連線層可由卷積操作實現,對前層是全連線的...
全連線層和啟用層
1.全連線層 經過前面若干次卷積 激勵 池化後,終於來到了輸出層,模型會將學到的乙個高質量的特徵全連線層。其實在全連線層之前,如果神經元數目過大,學習能力強,有可能出現過擬合。因此,可以引入dropout操作,來隨機刪除神經網路中的部分神經元,來解決此問題。還可以進行區域性歸一化 資料增強等操作,來...