tensorflow中的关键字global_step使用

 global_step经常在滑动平均,学习速率变化的时候需要用到,这个参数在tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_steps)里面有,系统会自动更新这个参数的值,从1开始。实际,操作在'tf.train.Optimizer.apply_gradients'内部完成 (见方法二)

eg:常见格式

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))
 
# trainable一定要设置成False,这样梯度传播时就不会修改global_step,
global_steps = tf.Variable(0, name="global_step", 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.initialize_all_variables())
	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))

output:
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

eg :方法二    tf.train.global_step(sess, global_step)

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))

# trainable一定要设置成False,这样梯度传播时就不会修改global_step,
global_step = tf.Variable(0, name="global_step", trainable=False)
learning_rate = tf.train.exponential_decay(0.1, global_step, 10, 2, staircase=False)
loss = tf.pow(w * x - y, 2)

# train_step 展开形式
optimizer = tf.train.AdamOptimizer(1e-3)
grads_and_vars = optimizer.compute_gradients(loss)
train_step = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    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])})
        #获得global_step
        current_step = tf.train.global_step(sess, global_step)
        print(current_step)

 

参考:

https://blog.csdn.net/UESTC_C2_403/article/details/72403833

 https://www.cnblogs.com/lainey/p/7927973.html#global_step

你可能感兴趣的:(tensorflow,深度学习)