国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

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

2024-07-09 22:43:14
字體:
來源:轉載
供稿:網友

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

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_datamnist = 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.pyExtracting data/train-images-idx3-ubyte.gzExtracting data/train-labels-idx1-ubyte.gzExtracting data/t10k-images-idx3-ubyte.gzExtracting data/t10k-labels-idx1-ubyte.gzWARNING: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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 左贡县| 共和县| 莱阳市| 乌什县| 安康市| 湖南省| 遂川县| 安西县| 安达市| 吉木萨尔县| 正安县| 扎囊县| 五家渠市| 夏河县| 遂昌县| 庄河市| 盘山县| 若羌县| 蒙山县| 济源市| 灵寿县| 九江县| 香港 | 新干县| 大港区| 隆德县| 伊金霍洛旗| 正镶白旗| 德格县| 临沂市| 菏泽市| 凤台县| 汝阳县| 马山县| 临颍县| 凉城县| 龙门县| 固原市| 石柱| 额敏县| 东乡|