中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Tensorflow中實現梯度下降法更新參數值

發布時間:2020-10-24 01:43:34 來源:腳本之家 閱讀:189 作者:勿在浮沙筑高臺LS 欄目:開發技術

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

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

TensorFlow經過使用梯度下降法對損失函數中的變量進行修改值,默認修改tf.Variable(tf.zeros([784,10]))

為Variable的參數。

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy,var_list=[w,b])

也可以使用var_list參數來定義更新那些參數的值

#導入Minst數據集
import input_data
mnist = input_data.read_data_sets("data",one_hot=True)
 
#導入tensorflow庫
import tensorflow as tf
 
#輸入變量,把28*28的圖片變成一維數組(丟失結構信息)
x = tf.placeholder("float",[None,784])
 
#權重矩陣,把28*28=784的一維輸入,變成0-9這10個數字的輸出
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)
 
#這個是訓練集的正確結果
y_ = tf.placeholder("float",[None,10])
 
#交叉熵,作為損失函數
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()
#創建session以便運算
sess = tf.Session()
sess.run(init)
 
#迭代1000次
for i in range(1000):
 #獲取訓練數據集的圖片輸入和正確表示數字
 batch_xs, batch_ys = mnist.train.next_batch(100)
 #運行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數字
 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})
 
#tf.argmax獲取最大值的索引。比較運算后的結果和本身結果是否相同。
#這步的結果應該是[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先將數據轉換成float,防止求平均不準確。
#tf.reduce_mean由于只有一個參數,就是上面那個數組的平均值。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#輸出
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))

計算結果如下

"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

如果限制,只更新參數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對結果影響不大,如果設置只修改b

#導入Minst數據集
import input_data
mnist = input_data.read_data_sets("data",one_hot=True)
 
#導入tensorflow庫
import tensorflow as tf
 
#輸入變量,把28*28的圖片變成一維數組(丟失結構信息)
x = tf.placeholder("float",[None,784])
 
#權重矩陣,把28*28=784的一維輸入,變成0-9這10個數字的輸出
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)
 
#這個是訓練集的正確結果
y_ = tf.placeholder("float",[None,10])
 
#交叉熵,作為損失函數
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()
#創建session以便運算
sess = tf.Session()
sess.run(init)
 
#迭代1000次
for i in range(1000):
 #獲取訓練數據集的圖片輸入和正確表示數字
 batch_xs, batch_ys = mnist.train.next_batch(100)
 #運行剛才建立的梯度下降算法,x賦值為圖片輸入,y_賦值為正確的表示數字
 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})
 
#tf.argmax獲取最大值的索引。比較運算后的結果和本身結果是否相同。
#這步的結果應該是[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先將數據轉換成float,防止求平均不準確。
#tf.reduce_mean由于只有一個參數,就是上面那個數組的平均值。
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
#輸出
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))

計算結果:

"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中實現梯度下降法更新參數值就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

漠河县| 咸丰县| 衡水市| 巴中市| 大厂| 蓬溪县| 顺昌县| 平陆县| 襄樊市| 昌黎县| 南靖县| 隆昌县| 镇沅| 正蓝旗| 永修县| 金堂县| 宜川县| 抚顺市| 长海县| 石首市| 定陶县| 菏泽市| 西乌珠穆沁旗| 滨州市| 徐汇区| 介休市| 辽阳市| 凤山市| 汝城县| 怀宁县| 沙河市| 文化| 汝州市| 桂东县| 龙胜| 卫辉市| 东港市| 中牟县| 遵化市| 淮北市| 白河县|