入門小案例,分別是回歸模型建立和mnist數據集的模型建立
1、回歸案例:
import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data import pandas as pd #1=================================================================== ''' 總結: 1、這個整體比較簡單,就是先構造整個圖,然后再代入進去計算,注意設置的學習率 ''' #構造數據 x_data = np.float32(np.random.rand(2,100)) y_data = np.dot([1,2],x_data) + 4 #構造線性模型 b = tf.Variable(tf.zeros([1])) w = tf.Variable(tf.random_uniform([1,2])) y = tf.matmul(w,x_data) + b #最小化方差 loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) #初始化變量,並啟動圖 sess = tf.Session() sess.run(tf.global_variables_initializer()) #擬合平面 for step in range(201): sess.run(train) if step % 20 ==0: print(step,sess.run(w),sess.run(b)) # 200 [[1.0000179 2.0000112]] [3.9999843]
2、MNIST普通神經網絡
''' 總結一下需要注意的點: 1、x是[None,784] * w是[784,10] + [10] 這里的10會自動廣播至[None,10]行,最后的結果就是[None,10] 2、softmax之后行列數不變,每一行求和都是1,哪個位置概率最大就是那個位置的值 3、交叉熵的公式是-y實際*log(y_hat) 4、arg_max(y,1)每一行的最大值下標,0的話是每一列的最大值下標,這個怎么跟concat不一樣,感覺很亂 4、tf.equal(tf.arg_max(y,1),tf.arg(y-,1)),兩個樣本的最大標算出來一共是[None,1][None,1],然后匹配是否一致,一致就是0,不一致就是1,得到[None,1] 5、再求均值,求均值之前需要轉float32格式 6、注意后面計算准確率的時候,傳入的變量值是x_test,y_test,這里的y_test回去直接是y_,而計算y時候需要用到x,w,這里x就變成了x_test,不是前面的batc_x了,記住啊 7、使用mnist.trian.next_batch(100)來每次傳入100個值 ''' #加載數據 mnist = input_data.read_data_sets('mnist_data/',one_hot = True) x_train = mnist.train.images y_train = mnist.train.labels x_test = mnist.test.images y_test = mnist.test.labels #定義變量 x = tf.placeholder(tf.float32,shape=[None,784]) y_= tf.placeholder(tf.float32,shape=[None,10]) w = tf.Variable(tf.truncated_normal([784,10])) b = tf.Variable(tf.constant(0.01,shape=[10])) y = tf.nn.softmax(tf.matmul(x,w)+b) #定義損失 loss = -tf.reduce_mean(y_*tf.log(y)) optimizer = tf.train.GradientDescentOptimizer(0.2) train = optimizer.minimize(loss) #初始化變量 sess = tf.Session() sess.run(tf.global_variables_initializer()) #驗證正確率 accuracy_rate = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1)) accuracy = tf.reduce_mean(tf.cast(accuracy_rate,'float32')) #訓練 for step in range(20000): batch_x,batch_y = mnist.train.next_batch(100) sess.run(train,feed_dict={x:batch_x,y_:batch_y}) if step % 1000 == 0: print(step,sess.run(accuracy,feed_dict={x:x_test,y_:y_test})) #最終的正確率大概是在91%
3、MNIST卷積神經網絡
'''
總結一下:
1、首先要弄清楚整個卷積神經網絡的構圖,分別是卷積池化》卷積池化》全連接》輸出
2、樣本是一個長784維的變量,要reshape成為28,28的圖片才能做卷積,
3、第一次用32個5,5的卷積核來處理,由於樣本是灰度圖,所以是[5,5,1,32]所以樣本從28,28變成了28,28,32的數據,這里因為步長[1,1,1,1]
4、然后再用[1,2,2,1]大小的pool處理卷積,步長是[1,2,2,1]所以[28,28,32] 變成了[14,14,32]
5、順序是先做卷積,在套relu,在做pooling
6、同理第二個卷積是這么做的,用[5,5,32,64]大小的權重把剛剛pooling之后的[14,14,32]的變成了[14,14,64]的,再做pooling變成[7,7,64]
7、剛剛前面都是針對一個圖片做的,其實到最后是[None,7,7,64],
8、全連接層之前需要把圖片重新恢復掉,因為現在是[None,7,7,64],所以reshape[-1,7*7*64],-1的意思就是None
9、reshape之后是[None,3136]太大了,全連接處轉成1024,所以用的w是[7*7*64,1024]
10、最后用softmax轉成[None,10]就可以了
'''
#加載數據 x_data = mnist.train.images y_data = mnist.train.labels x_test = mnist.test.images y_test = mnist.test.labels #權重函數 def weights(shape): initial = tf.truncated_normal(shape,stddev=0.1,dtype=tf.float32) return tf.Variable(initial) #偏置項 def bias(shape): initial = tf.constant(0.1,shape=shape,dtype=tf.float32) return tf.Variable(initial) #輸入值 xs = tf.placeholder(tf.float32,shape=[None,784]) ys = tf.placeholder(tf.float32,shape=[None,10]) x_images = tf.reshape(xs,[-1,28,28,1]) #第一層卷積 #con_1 w_con1 = weights([5,5,1,32]) b_con1 = bias([32]) h_con1 = tf.nn.conv2d(x_images,w_con1,[1,1,1,1],padding='SAME') h_relu1 = tf.nn.relu(h_con1 + b_con1) #pool1 h_pool1 = tf.nn.max_pool(h_relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #第二層卷積 #con2 w_con2 = weights([5,5,32,64]) b_con2 = bias([64]) h_con2 = tf.nn.conv2d(h_pool1,w_con2,strides=[1,1,1,1],padding='SAME') h_relu2 = tf.nn.relu(h_con2) #pool2 h_pool2 = tf.nn.max_pool(h_relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #全連接層 w_fc1 = weights([7*7*64,1024]) b_fc1 = bias([1024]) 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) #drop_out keep_pro = tf.placeholder(dtype=tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob=keep_pro) #輸出層 w_fc2 = weights([1024,10]) b_fc2 = bias([10]) h_fc2 = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2) + b_fc2) #損失函數 loss = -tf.reduce_mean(ys*tf.log(h_fc2)) train = tf.train.AdamOptimizer(1e-4).minimize(loss) #初始化變量 sess.run(tf.global_variables_initializer()) #計算誤差 accuracy = tf.equal(tf.arg_max(ys,1),tf.arg_max(h_fc2,1)) accuracy = tf.reduce_mean(tf.cast(accuracy,tf.float32)) #開始訓練 for step in range(5000): batch_x,batch_y = mnist.train.next_batch(100) sess.run(train,feed_dict={xs:batch_x,ys:batch_y,keep_pro:0.8}) if step % 100 == 0 : print(step,sess.run(accuracy,feed_dict={xs:mnist.test.images,ys:mnist.test.labels,keep_pro:1}))
訓練的好慢啊,只有CPU真的這么慢嗎。。。。還是我寫的代碼有問題,不過正確率確實好高,跑一會就97%了