TensorFlow中global_step的簡單分析


由global_step =tf.train.get_or_create_global_step()產生的疑問

tf.train.get_or_create_global_step(graph=None)
Returns and create (if necessary) the global step tensor.

Args:

  • graph: The graph in which to create the global step tensor. If missing, use default graph.

Returns:

The global step tensor.

 

global_step在滑動平均、優化器、指數衰減學習率等方面都有用到,這個變量的實際意義非常好理解:代表全局步數,比如在多少步該進行什么操作,現在神經網絡訓練到多少輪等等,類似於一個鍾表。

根據代碼可以發現global_step的初始化值是0:

 global_step=tf.Variable(0, trainable=False)

這個初始化代碼十分簡單,但也是問題所在。如果global_step直接賦值為0了,還怎么表示全局的步數?

當助教的時候,有一個學妹問我關於global_step的操作問題,我一時沒回答上來,global_step到底是怎么完成自動加1的。

關於這個問題在CSDN查到一篇文章 https://blog.csdn.net/uestc_c2_403/article/details/72403833

稍微改了一下人家的代碼:

import tensorflow as tf
import numpy as np
 
x = tf.placeholder(tf.float32, shape=[None, 1], name='x')
y = tf.placeholder(tf.float32, shape=[None, 1], name='y')
w = tf.Variable(tf.constant(0.0))
 
global_steps = tf.Variable(0, trainable=False)
 
 
learning_rate = tf.train.exponential_decay(0.1, global_steps, 10, 2, staircase=False)
loss = tf.pow(w*x-y, 2)
 
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_steps)
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10):
        sess.run(train_step, feed_dict={x:np.linspace(1,2,10).reshape([10,1]),
            y:np.linspace(1,2,10).reshape([10,1])})
        print(sess.run(learning_rate))
        print(sess.run(global_steps))

輸出為:

 

0.107177

1

0.11487
2
0.123114
3
0.131951
4
0.141421
5
0.151572
6
0.16245
7
0.17411
8
0.186607
9
0.2
10

這里的global_tep確實完成自動加1了,但具體為什么自動加一,原文章里面也沒有提到。

經過多次修改代碼驗證之后得出,如果把
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_steps)

后面部分的global_step=global_steps去掉,global_step的自動加一就會失效,輸出如下:
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0
0.1
0

因為指數衰減的學習率是伴隨global_step的變化而衰減的,所以當global_step不改變時,學習率也變成一個定值。

綜上所述:損失函數優化器的minimize()中global_step=global_steps能夠提供global_step自動加一的操作。

這里有個額外的疑慮說明:global_steps是等號右邊,在編程語言里面通常當作定值(即不會受函數影響)賦值給等號左邊的global_step。然而,在這個優化器里面能夠使得右邊的變量自動加一。這確實是編程語言里面少見的,也是需要特別注意的。

---

以上是原文

后期解答: tf.Variable定義的變量是tf自定義的數據結構,以張量形式存在於網絡當中。這也是w, b以及global_steps能被后台改變的原因。上面那個問題也是這個原因,global_steps在右邊,但它不是簡單的value,而是特殊的數據結構(tensorflow框架使然)。

1. global_step

參考stackoverflow答主的回答

global_step refer to the number of batches seen by the graph. Everytime a batch is provided, the weights are updated in the direction that minimizes the loss. global_step just keeps track of the number of batches seen so far. When it is passed in the minimize() argument list, the variable is increased by one. Have a look at optimizer.minimize().

You can get the global_step value using tf.train.global_step().

The 0 is the initial value of the global step in this context.
--------------------- 
作者:digta 
來源:CSDN 
原文:https://blog.csdn.net/u014642834/article/details/78390823

global_step在訓練中是計數的作用,每訓練一個batch就加1
---------------------  
作者:木盞  
來源:CSDN  
原文:https://blog.csdn.net/leviopku/article/details/78508951  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM