当前位置: 代码迷 >> 综合 >> tensorflow杂记
  详细解决方案

tensorflow杂记

热度:19   发布时间:2024-01-25 12:57:29.0

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

 

  相关解决方案