tensorflow里的tf.contrib.framework.get_global_step()

The global step tensor must be an integer variable. We first try to find it in the collection GLOBAL_STEP, or by name global_step:0.

代码如下:

import tensorflow as tf
import numpy as np

#用yield可以在函数返回出来?
def test_yield():
    for i in range(10):
        a= sess.run(tf.contrib.framework.get_global_step())

        b=i
        yield a,b
    return b



#a=np.array([[1,1,0,1],[0,0,0,0]])
#b=a.reshape(2,2,2,1)



a=np.array([[1,1,0,1]])
b=a.reshape(2,2,1,1)
#b=a.reshape(2,2,1,2)#input batch为2,通道为1时 ,输出果然是2*3*3*2
filter=tf.Variable(b,dtype=tf.float32,name='filter')

global_step = tf.Variable(5, name='global_step', trainable=False)
a1=np.arange(16)
b1=a1.reshape(1,4,4,1)
#b1=a1.reshape(2,4,4,1) #batch为2
y=[1]
y=np.expand_dims(y, 0)
input=tf.Variable(b1,dtype=tf.float32,name='input')  #还要加个float32

y_pl = tf.placeholder(shape=[None,1], dtype=tf.float32, name="y")

conv1 = tf.contrib.layers.conv2d(input, 2, 2,1, scope='conv_layer1', activation_fn=tf.nn.tanh);
flattened = tf.contrib.layers.flatten(conv1)
fc1 = tf.contrib.layers.fully_connected(flattened, 1)
losses = tf.squared_difference(y_pl, fc1)
loss = tf.reduce_mean(losses)
optimizer = tf.train.RMSPropOptimizer(0.00025, 0.99, 0.0, 1e-6)
#train_op = optimizer.minimize(loss, global_step=tf.Variable(3,name='global_step'))
train_op = optimizer.minimize(loss,global_step=tf.contrib.framework.get_global_step())


op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1],padding='VALID',name='op')
sess=tf.Session()
#sess = tfdbg.LocalCLIDebugWrapperSession(sess)#调试步骤b
#sess.add_tensor_filter("has_inf_or_nan", tfdbg.has_inf_or_nan)#后面是一个函数

sess.run(tf.global_variables_initializer())
#sess.add_tensor_filter("has_inf_or_nan", tfdbg.has_inf_or_nan)#调试步骤c 命令就是run  ,命令有ni 有输入和输出到下一个哪个结点
# https://www.cnblogs.com/hellcat/articles/7812119.html 调试教程

for i in range(1):

    b11,loss2,fc1=sess.run([loss,train_op,fc1],feed_dict={y_pl:y}) #用全部的样本训练
    #total_t = sess.run(tf.contrib.framework.get_global_step())
for a111,b111 in test_yield():
    print(b111)



bb=(sess.run(op))
cc=sess.run(conv1)
total_t = sess.run(tf.contrib.framework.get_global_step())
#cc=bb.reshape(2,3,3)
#dd=bb.reshape(3,3,2)
ee=sess.run(tf.squeeze(op,name='ee'))
print(bb.size)

首先就像文档所说的那样,需要出初始化,两种方式:

global_step = tf.Variable(5, name='global_step', trainable=False)
#然后在需要计数的地方
train_op = optimizer.minimize(loss,global_step=tf.contrib.framework.get_global_step())

第二种:

train_op = optimizer.minimize(loss, global_step=tf.Variable(3,name='global_step'))

运行一次sess.run(train_op),就会记一次。
之前我所看的代码这个数字是48362次,后来仔细检查,才发现原来是读取存下来的checkpoint 了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值