Tensorflow學習——1 簡單的例子研究
阿新 • • 發佈:2019-01-29
從入職以來一直在慢慢研究深度學習方面的知識,但是大多是在已有模型進行一些簡單的修改,實際動手寫模型成為了很大的困難,所以在此以一些書和部落格為例子,自己手動從開始好好系統學習一下。
1、下面這個例子主要簡單的介紹了graph和reuse=true的不同情況的區別,參考《Tensorflow實戰Google深度學習框架》p40頁
tensorflow通過tf.Graph函式來生成新的計算圖,不同的計算圖的張量和運算都不會共享。
# _*_ encoding=utf8 _*_
import tensorflow as tf
# p40
a = tf.constant([1.0,2.0], name = "a" )
b = tf.constant([2.0,3.0], name = "b")
result = a+b
print(result)
# Tensor("add:0", shape=(2,), dtype=float32)
print(result.shape)
# (2,)
# p41
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
# 在g1中定義變數v並設定初始化為0
v = tf.get_variable(
"v", initializer=tf.zeros_initializer(shape=[1.0 ]))
#Tensor("add:0", shape=(2,), dtype=float32)
g2 = tf.Graph()
with g2.as_default():
# 在g2中定義變數v並且設定初始值為1
v = tf.get_variable(
"v",initializer=tf.ones_initializer(shape=[1.0]))
# 在圖g1讀取變數v的值
with tf.Session(graph=g1) as sess:
tf.initialize_all_variables().run()
with tf.variable_scope("" , reuse=True):
print(sess.run(tf.get_variable("v")))
# 0. 在圖g1中,變數v的值應該是0,所以這兒輸出的是[0.]
with tf.Session(graph=g2) as sess:
tf.initialize_all_variables().run()
with tf.variable_scope("", reuse=True):
print(sess.run(tf.get_variable("v")))
# 1. 在g2圖中變數v的值是1,所以輸出[1.]
2、下面這個例子是一個最簡單的但是完整的神經網路樣例程式,它的作用就是解決二分類問題
# _*_ encoding=utf8 _*_
import tensorflow as tf
from numpy.random import RandomState
batch_size = 8
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
x = tf.placeholder(tf.float32,shape=(None,2),name='x-input')
y_ = tf.placeholder(tf.float32,shape=(None,1),name='y-input')
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
# 定義損失函式和反向傳播演算法
# 損失函式用來刻畫預測值和真實值得差距 cross_entropy (交叉熵,在tensorflow交叉熵如下表示,y_代表正確結果,y代表預測結果)
#tf.clip_by_value(A, min, max):輸入一個張量A,
#把A中的每一個元素的值都壓縮在min和max之間。
#小於min的讓它等於min,大於max的元素的值等於max
cross_entropy = -tf.reduce_mean(
y_ * tf.log(tf.clip_by_value(y,1e-10,1.0))
)
train_step = tf.train.AdadeltaOptimizer(0.001).minimize(cross_entropy)
# 生成隨機資料集 1為隨機種子 網上說法,種子相同,隨機序列相同
rdm = RandomState(1)
# 產生128行×2列的隨機矩陣 如
# [1.73955667e-01 1.26329519e-01]
# [1.35079158e-01 5.05662166e-01]
# [2.15248053e-02 9.47970211e-01]
# [8.27115471e-01 1.50189807e-02]
dateset_size = 128
X = rdm.rand(dateset_size, 2) # 生成一對隨機的小數列表
# print(X)
#根據每一行的兩個數輸出是1 還是 0 構建一個標準的規則
Y = [[int(x1+x2<1)] for (x1,x2) in X] # 編譯列表,若列表中兩個數的和來執行0表示負樣本,1表示正樣本
# print(Y)
with tf.Session() as sess:
# init_op = tf.initialize_all_variables()
# sess.run(init_op)
sess.run(tf.global_variables_initializer())
print(sess.run(w1))
print(sess.run(w2))
STEPS = 50000
for i in range(STEPS):
start = (i*batch_size) % dateset_size
end = min(start+batch_size,dateset_size)
# 通過選取的樣本來訓練神經網路
sess.run(train_step,feed_dict={x: X[start:end],y_: Y[start:end]})
if i%1000 ==0:
#輸出交叉熵
total_cross_entry = sess.run(
cross_entropy,feed_dict={x:X,y_:Y}
)
print("after %d training step(s),cross entryopy on all is %g"%(i,total_cross_entry))
結果
[[-0.8113182 1.4845988 0.06532937]
[-2.4427042 0.0992484 0.5912243 ]]
[[-0.8113182 ]
[ 1.4845988 ]
[ 0.06532937]]
after 0 training step(s),cross entryopy on all is 0.0677411
after 1000 training step(s),cross entryopy on all is 0.0676762
after 2000 training step(s),cross entryopy on all is 0.0676077
...
after 48000 training step(s),cross entryopy on all is 0.0648452
after 49000 training step(s),cross entryopy on all is 0.0647877
Process finished with exit code 0
訓練神經網路的三個步驟:
- 定義神經網路的結構和前向傳播的輸出結果
- 定義損失函式以及選擇反向傳播的演算法
- 生成session並且在訓練資料上反覆執行反向傳播優化演算法
3、手寫體識別,10分類
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/", one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None, 784]) # 784 輸入層的節點數,,這個就等於圖片的畫素
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) #輸出層的節點數,這個等於類別的數目,0-9
# 計算隱藏層的前項傳播結果 並且使用softmax 預測出這個圖片是幾
y = tf.nn.softmax(tf.matmul(x, W)+b)
y_ = tf.placeholder(tf.float32, [None, 10])
# 損失函式 就y與y_的差距 y_真實值
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
# 反向傳播 ,隨機梯度下降 0.5的學習率
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# 變數初始化
tf.global_variables_initializer().run()
#
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x:batch_xs,y_:batch_ys})
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))