在Tensorflow中實(shí)現(xiàn)梯度下降法更新參數(shù)值
我就廢話不多說(shuō)了,直接上代碼吧!
tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
TensorFlow經(jīng)過(guò)使用梯度下降法對(duì)損失函數(shù)中的變量進(jìn)行修改值,默認(rèn)修改tf.Variable(tf.zeros([784,10]))
為Variable的參數(shù)。
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy,var_list=[w,b])
也可以使用var_list參數(shù)來(lái)定義更新那些參數(shù)的值
#導(dǎo)入Minst數(shù)據(jù)集 import input_data mnist = input_data.read_data_sets("data",one_hot=True) #導(dǎo)入tensorflow庫(kù) import tensorflow as tf #輸入變量,把28*28的圖片變成一維數(shù)組(丟失結(jié)構(gòu)信息) x = tf.placeholder("float",[None,784]) #權(quán)重矩陣,把28*28=784的一維輸入,變成0-9這10個(gè)數(shù)字的輸出 w = tf.Variable(tf.zeros([784,10])) #偏置 b = tf.Variable(tf.zeros([10])) #核心運(yùn)算,其實(shí)就是softmax(x*w+b) y = tf.nn.softmax(tf.matmul(x,w) + b) #這個(gè)是訓(xùn)練集的正確結(jié)果 y_ = tf.placeholder("float",[None,10]) #交叉熵,作為損失函數(shù) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) #梯度下降算法,最小化交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) #初始化,在run之前必須進(jìn)行的 init = tf.initialize_all_variables() #創(chuàng)建session以便運(yùn)算 sess = tf.Session() sess.run(init) #迭代1000次 for i in range(1000): #獲取訓(xùn)練數(shù)據(jù)集的圖片輸入和正確表示數(shù)字 batch_xs, batch_ys = mnist.train.next_batch(100) #運(yùn)行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數(shù)字 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys}) #tf.argmax獲取最大值的索引。比較運(yùn)算后的結(jié)果和本身結(jié)果是否相同。 #這步的結(jié)果應(yīng)該是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]這種形式。 #1代表正確,0代表錯(cuò)誤 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) #tf.cast先將數(shù)據(jù)轉(zhuǎn)換成float,防止求平均不準(zhǔn)確。 #tf.reduce_mean由于只有一個(gè)參數(shù),就是上面那個(gè)數(shù)組的平均值。 accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) #輸出 print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))
計(jì)算結(jié)果如下
"C:\Program Files\Anaconda3\python.exe" D:/pycharmprogram/tensorflow_learn/softmax_learn/softmax_learn.py Extracting data\train-images-idx3-ubyte.gz Extracting data\train-labels-idx1-ubyte.gz Extracting data\t10k-images-idx3-ubyte.gz Extracting data\t10k-labels-idx1-ubyte.gz WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 2018-05-14 15:49:45.866600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2018-05-14 15:49:45.866600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 0.9163 Process finished with exit code 0
如果限制,只更新參數(shù)W查看效果
"C:\Program Files\Anaconda3\python.exe" D:/pycharmprogram/tensorflow_learn/softmax_learn/softmax_learn.py Extracting data\train-images-idx3-ubyte.gz Extracting data\train-labels-idx1-ubyte.gz Extracting data\t10k-images-idx3-ubyte.gz Extracting data\t10k-labels-idx1-ubyte.gz WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 2018-05-14 15:51:08.543600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2018-05-14 15:51:08.544600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 0.9187 Process finished with exit code 0
可以看出只修改W對(duì)結(jié)果影響不大,如果設(shè)置只修改b
#導(dǎo)入Minst數(shù)據(jù)集 import input_data mnist = input_data.read_data_sets("data",one_hot=True) #導(dǎo)入tensorflow庫(kù) import tensorflow as tf #輸入變量,把28*28的圖片變成一維數(shù)組(丟失結(jié)構(gòu)信息) x = tf.placeholder("float",[None,784]) #權(quán)重矩陣,把28*28=784的一維輸入,變成0-9這10個(gè)數(shù)字的輸出 w = tf.Variable(tf.zeros([784,10])) #偏置 b = tf.Variable(tf.zeros([10])) #核心運(yùn)算,其實(shí)就是softmax(x*w+b) y = tf.nn.softmax(tf.matmul(x,w) + b) #這個(gè)是訓(xùn)練集的正確結(jié)果 y_ = tf.placeholder("float",[None,10]) #交叉熵,作為損失函數(shù) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) #梯度下降算法,最小化交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy,var_list=[b]) #初始化,在run之前必須進(jìn)行的 init = tf.initialize_all_variables() #創(chuàng)建session以便運(yùn)算 sess = tf.Session() sess.run(init) #迭代1000次 for i in range(1000): #獲取訓(xùn)練數(shù)據(jù)集的圖片輸入和正確表示數(shù)字 batch_xs, batch_ys = mnist.train.next_batch(100) #運(yùn)行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數(shù)字 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys}) #tf.argmax獲取最大值的索引。比較運(yùn)算后的結(jié)果和本身結(jié)果是否相同。 #這步的結(jié)果應(yīng)該是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]這種形式。 #1代表正確,0代表錯(cuò)誤 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) #tf.cast先將數(shù)據(jù)轉(zhuǎn)換成float,防止求平均不準(zhǔn)確。 #tf.reduce_mean由于只有一個(gè)參數(shù),就是上面那個(gè)數(shù)組的平均值。 accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) #輸出 print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))
計(jì)算結(jié)果:
"C:\Program Files\Anaconda3\python.exe" D:/pycharmprogram/tensorflow_learn/softmax_learn/softmax_learn.py Extracting data\train-images-idx3-ubyte.gz Extracting data\train-labels-idx1-ubyte.gz Extracting data\t10k-images-idx3-ubyte.gz Extracting data\t10k-labels-idx1-ubyte.gz WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 2018-05-14 15:52:04.483600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2018-05-14 15:52:04.483600: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 0.1135 Process finished with exit code 0
如果只更新b那么對(duì)效果影響很大。
以上這篇在Tensorflow中實(shí)現(xiàn)梯度下降法更新參數(shù)值就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- tensorflow 實(shí)現(xiàn)自定義梯度反向傳播代碼
- 有關(guān)Tensorflow梯度下降常用的優(yōu)化方法分享
- TensorFlow梯度求解tf.gradients實(shí)例
- 基于TensorFlow中自定義梯度的2種方式
- tensorflow 查看梯度方式
- tensorflow求導(dǎo)和梯度計(jì)算實(shí)例
- Tensorflow的梯度異步更新示例
- Tensorflow實(shí)現(xiàn)部分參數(shù)梯度更新操作
- 運(yùn)用TensorFlow進(jìn)行簡(jiǎn)單實(shí)現(xiàn)線性回歸、梯度下降示例
- Tensorflow 卷積的梯度反向傳播過(guò)程
相關(guān)文章
python實(shí)現(xiàn)微信接口(itchat)詳細(xì)介紹
這篇文章主要介紹了python實(shí)現(xiàn)微信接口(itchat)詳細(xì)介紹,小編覺(jué)得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10selenium動(dòng)態(tài)數(shù)據(jù)獲取的方法實(shí)現(xiàn)
本文主要介紹了selenium動(dòng)態(tài)數(shù)據(jù)獲取的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07python組合無(wú)重復(fù)三位數(shù)的實(shí)例
今天小編就為大家分享一篇python組合無(wú)重復(fù)三位數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11在Python Flask App中獲取已發(fā)布的JSON對(duì)象的解決方案
這篇文章主要介紹了在Python Flask App中獲取已發(fā)布的JSON對(duì)象的解決方案,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08Python三維繪圖之Matplotlib庫(kù)的使用方法
這篇文章主要給大家介紹了關(guān)于Python三維繪圖之Matplotlib庫(kù)的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python測(cè)試開(kāi)發(fā)django之使用supervisord?后臺(tái)啟動(dòng)celery?服務(wù)(worker/beat)
Supervisor是用Python開(kāi)發(fā)的一個(gè)client/server服務(wù),是Linux/Unix系統(tǒng)下的一個(gè)進(jìn)程管理工具,不支持Windows系統(tǒng),這篇文章主要介紹了python測(cè)試開(kāi)發(fā)django之使用supervisord?后臺(tái)啟動(dòng)celery?服務(wù)(worker/beat),需要的朋友可以參考下2022-07-07超詳細(xì)注釋之OpenCV按位AND OR XOR和NOT
這篇文章主要介紹了OpenCV按位AND OR XOR和NOT運(yùn)算,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09