global_step

global_step

來自專欄 TensorFlow

API:

tf.train.global_step(sess, global_step_tensor)

作用:

用來設定總共train多少步

樣例:

# Creates a variable to hold the global_step.global_step_tensor = tf.Variable(10, trainable=False, name=global_step)# Creates a session.sess = tf.Session()# Initializes the variable.print(global_step: %s % tf.train.global_step(sess, global_step_tensor))global_step: 10

Args:

sess: A TensorFlow Session object.

global_step_tensor: Tensor or the name of the operation that contains the global step.

Returns: The global step value.


注意:

如果直接定義global_step = tf.Variable(0, trainable=False),用來記錄當前訓練了多少步,如下圖所示:

import tensorflow as tfimport 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))

API源碼解析

?可以通過tf.train.globalstep定義global_step

?也可以通過直接通過Variable自己定義一個global_step(也可叫其他的名字),然後這個被調用即可

推薦閱讀:

羅永浩為什麼成不了雷軍?
我同樣擁有羅永浩、剽悍一隻貓、Spenser等人的好習慣,可還是輸在了起跑線上
黑莓死了,諾基亞沒落了,羅永浩的鎚子還能走多遠?
用鎚子TNT寫代碼是一種什麼體驗?
鎚子涼了,手機行業還熱嗎?|附:行業報告

TAG:鎚子手機 | 鎚子科技 | 羅永浩 |