最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Tensorflow中實現(xiàn)梯度下降法更新參數(shù)值

 更新時間:2020年01月23日 18:07:20   作者:勿在浮沙筑高臺LS  
今天小編就為大家分享一篇在Tensorflow中實現(xiàn)梯度下降法更新參數(shù)值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,直接上代碼吧!

tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

TensorFlow經(jīng)過使用梯度下降法對損失函數(shù)中的變量進行修改值,默認修改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ù)來定義更新那些參數(shù)的值

#導(dǎo)入Minst數(shù)據(jù)集
import input_data
mnist = input_data.read_data_sets("data",one_hot=True)
 
#導(dǎo)入tensorflow庫
import tensorflow as tf
 
#輸入變量,把28*28的圖片變成一維數(shù)組(丟失結(jié)構(gòu)信息)
x = tf.placeholder("float",[None,784])
 
#權(quán)重矩陣,把28*28=784的一維輸入,變成0-9這10個數(shù)字的輸出
w = tf.Variable(tf.zeros([784,10]))
#偏置
b = tf.Variable(tf.zeros([10]))
 
#核心運算,其實就是softmax(x*w+b)
y = tf.nn.softmax(tf.matmul(x,w) + b)
 
#這個是訓(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之前必須進行的
init = tf.initialize_all_variables()
#創(chuàng)建session以便運算
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)
 #運行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數(shù)字
 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})
 
#tf.argmax獲取最大值的索引。比較運算后的結(jié)果和本身結(jié)果是否相同。
#這步的結(jié)果應(yīng)該是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]這種形式。
#1代表正確,0代表錯誤
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
 
#tf.cast先將數(shù)據(jù)轉(zhuǎn)換成float,防止求平均不準確。
#tf.reduce_mean由于只有一個參數(shù),就是上面那個數(shù)組的平均值。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#輸出
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))

計算結(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對結(jié)果影響不大,如果設(shè)置只修改b

#導(dǎo)入Minst數(shù)據(jù)集
import input_data
mnist = input_data.read_data_sets("data",one_hot=True)
 
#導(dǎo)入tensorflow庫
import tensorflow as tf
 
#輸入變量,把28*28的圖片變成一維數(shù)組(丟失結(jié)構(gòu)信息)
x = tf.placeholder("float",[None,784])
 
#權(quán)重矩陣,把28*28=784的一維輸入,變成0-9這10個數(shù)字的輸出
w = tf.Variable(tf.zeros([784,10]))
#偏置
b = tf.Variable(tf.zeros([10]))
 
#核心運算,其實就是softmax(x*w+b)
y = tf.nn.softmax(tf.matmul(x,w) + b)
 
#這個是訓(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之前必須進行的
init = tf.initialize_all_variables()
#創(chuàng)建session以便運算
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)
 #運行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數(shù)字
 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})
 
#tf.argmax獲取最大值的索引。比較運算后的結(jié)果和本身結(jié)果是否相同。
#這步的結(jié)果應(yīng)該是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]這種形式。
#1代表正確,0代表錯誤
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
 
#tf.cast先將數(shù)據(jù)轉(zhuǎn)換成float,防止求平均不準確。
#tf.reduce_mean由于只有一個參數(shù),就是上面那個數(shù)組的平均值。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#輸出
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))

計算結(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那么對效果影響很大。

以上這篇在Tensorflow中實現(xiàn)梯度下降法更新參數(shù)值就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python3里gbk編碼的問題解決

    python3里gbk編碼的問題解決

    本文主要介紹了python3里gbk編碼的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-08-08
  • numpy中nan_to_num的具體使用

    numpy中nan_to_num的具體使用

    在Numpy中NaN值一般出現(xiàn)在數(shù)據(jù)清洗前,出現(xiàn)這個值說明這個數(shù)據(jù)是缺失的,本文主要介紹了numpy中nan_to_num的具體使用,感興趣的可以了解一下
    2022-08-08
  • Python新手學(xué)習標準庫模塊命名

    Python新手學(xué)習標準庫模塊命名

    在本篇內(nèi)容中,小編給大家分享的是關(guān)于Python標準庫模塊命名詳解內(nèi)容,有需要的朋友們可以參考下。
    2020-05-05
  • 深入探討Python中的RegEx模式匹配

    深入探討Python中的RegEx模式匹配

    正則表達式通??s寫為?regex,是處理文本的有效工具,這篇文章主要來和大家深入探討一下Python中的RegEx模式匹配,感興趣的可以了解一下
    2023-07-07
  • python中open用法的使用指南

    python中open用法的使用指南

    在Python中,open()函數(shù)用于打開文件,并返回一個文件對象,可以使用該對象來讀取或?qū)懭胛募?shù)據(jù),本文主要介紹了python中open用法的使用指南,感興趣的可以了解一下
    2023-12-12
  • Python paramiko 模塊淺談與SSH主要功能模擬解析

    Python paramiko 模塊淺談與SSH主要功能模擬解析

    這篇文章主要介紹了Python paramiko 模塊詳解與SSH主要功能模擬,本文通過圖文并茂的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • python中的map函數(shù)語法詳解

    python中的map函數(shù)語法詳解

    map是python內(nèi)置函數(shù),會根據(jù)提供的函數(shù)對指定的序列做映射,這篇文章主要介紹了python中的map函數(shù)語法詳解,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • python使用xauth方式登錄飯否網(wǎng)然后發(fā)消息

    python使用xauth方式登錄飯否網(wǎng)然后發(fā)消息

    這篇文章主要介紹了python使用xauth方式登錄飯否網(wǎng)然后發(fā)消息示例,需要的朋友可以參考下
    2014-04-04
  • python中for in的用法詳解

    python中for in的用法詳解

    這篇文章主要介紹了python中for in的用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-04-04
  • Python中的TfidfVectorizer參數(shù)使用解析

    Python中的TfidfVectorizer參數(shù)使用解析

    這篇文章主要介紹了Python中的TfidfVectorizer參數(shù)使用解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論

托克逊县| 墨竹工卡县| 苏尼特右旗| 鸡东县| 梁山县| 东台市| 邯郸市| 潜山县| 玉山县| 耿马| 卢龙县| 洪湖市| 即墨市| 邵阳市| 常德市| 大新县| 古田县| 砚山县| 德阳市| 岳阳县| 肥东县| 鹿泉市| 云龙县| 定南县| 怀安县| 通海县| 淳化县| 徐闻县| 泰安市| 海原县| 南陵县| 盐津县| 鄄城县| 永登县| 沾益县| 铅山县| 皮山县| 克东县| 淮滨县| 延长县| 政和县|