tensorflow杂记

1.tf.get_variable或tf.Variable默认trainable=True, 默认将变量添加到图形集合tf.GraphKeys.TRAINABLE_VARIABLES中,用于优化器Optimizer类优化的默认变量列表,也就是要训练的变量列表。

2.tf.control_dependencies:with tf.control_dependencies([update_op]):

                                                        add_with_dependencies = tf.add(a_2, b_2)

在计算图结构中调整优先顺序,先计算变量op,

3.tf.GraphKeys.UPDATE_OPS,UPDATE_OPS是ops的集合(图表运行时执行的操作,如乘法,ReLU等),

保存在训练之前需要完成的操作,配合tf.control_dependencies使用

output = tf.layers.batch_normalization(input, training=is_traing)

update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

#在tf.contrib.layers.batch_norm的参数中可以看到有一项updates_collections的默认值即为tf.GraphKeys.UPDATE_OPS
print(update_ops)
with tf.control_dependencies(update_ops):
    train_op = optimizer.minimize(loss)

4.tf.get_collection

该函数的作用是从一个collection中取出全部变量,形成1个列表,key参数中输入的是collection的名称。该函数常常与tf.get_variable和tf.add_to_collection配合使用。

5.变量作用域和名称作用域tf.variable_scope &&& tf.name_scope:

tf.Variable()可以单独使用,也可以搭配`tf.name_scope`使用,形成命名区域,给变量分类命名

tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。(reuse= True)

  1. 对于使用tf.Variable来说,tf.name_scope和tf.variable_scope功能一样,都是给变量加前缀,相当于分类管理,模块化。
  2. 对于tf.get_variable来说,tf.name_scope对其无效,也就是说tf认为当你使用tf.get_variable时,你只归属于tf.variable_scope来管理共享与否。

with tf.variable_scope(tf.get_variable_scope(), reuse=reuse_variables):

6.tf.GraphKeys.REGULARIZATION_LOSSES,创建一个正则化方法(l1、l2),并把正则化方法应用到参数上。

在使用tf.get_variable()和tf.variable_scope()的时候,它们中有regularizer形参.如果传入这个参数的话,那么变量空间variable_scope内的weights的正则化损失,会被添加到GraphKeys.REGULARIZATION_LOSSES中。

regularizer = layers.l1_regularizer(0.1)
with tf.variable_scope('var', initializer=tf.random_normal_initializer(), 
regularizer=regularizer):
    weight = tf.get_variable('weight', shape=[8], initializer=tf.ones_initializer())

7.tf.contrib.slim->with slim.argscope([list_of_ops], **kwargs),为list_of_ops设置默认值,如果是普通函数,需要

@slim.add_arg_scope

def func(a, b):

       return a+b

with slim.arg_scope([func], a=10)

      x = func(b=1)

     print(x)                              = 10+1

但是平常所用到的slim.conv2d( ),slim.fully_connected( ),slim.max_pool2d( )等函数在他被定义的时候就已经添加了@add_arg_scope。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值