Tensorflowで勾配降下法を実現してパラメータ値を更新します。


私は余計なことを言わないで、直接コードをかけましょう。

tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
TensorFlowは、勾配降下法を用いて損失関数の変数を修正し、デフォルトではtf.Varable(tf.zros([784,10])を修正します。
Varableのパラメータです。

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy,var_list=[w,b])
var_も使用できますリストパラメータは、それらのパラメータを更新する値を定義します。

#  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で勾配降下法を実現してパラメーター値を更新しました。つまり、小編集は皆さんに共有しています。参考にしていただければと思います。よろしくお願いします。