在 Tensorflow 中更改变量的初始值设定项 [英] Change initializer of Variable in Tensorflow
问题描述
我有一个预定义的代码来创建一个 Tensorflow 图.变量包含在变量作用域中,每个变量都有一个预定义的初始值设定项.有没有办法改变变量的初始值设定项?
I have a predefined code that creates a Tensorflow graph. The variables are contained in variable scopes and each has a predefined initializer. Is there any way to change the initializer of the variables?
示例:第一个图定义
with tf.variable_scope('conv1')
w = tf.get_variable('weights')
稍后我想修改变量并将初始值设定项更改为 Xavier:
Later on I would like to modify variable and change the initializer to Xavier:
with tf.variable_scope('conv1')
tf.get_variable_scope().reuse_variable()
w = tf.get_variable('weights',initializer=tf.contrib.layers.xavier_initializer(uniform=False))
然而,当我重用一个变量时,初始化器不会改变.稍后当我执行 initialize_all_variables()
我得到默认值而不是 Xavier如何更改变量的初始值设定项?谢谢
However, when I reuse a variable, the initializer doesn't change.
later on when I do initialize_all_variables()
I get the default values and not Xavier
How can I change the initializer of a variable?
Thanks
推荐答案
问题是无法在设置重用时更改初始化(初始化在第一个块中设置).
The problem is that initialization can't be changed on setting up reuse (the initialization is set during the first block).
因此,只需在第一个变量作用域调用期间使用 xavier 初始化定义它.所以第一次调用将是,然后所有变量的初始化都是正确的:
So, just define it with xavier intialization during the first variable scope call. So the first call would be, then initialization of all variables with be correct:
with tf.variable_scope(name) as scope:
kernel = tf.get_variable("W",
shape=kernel_shape, initializer=tf.contrib.layers.xavier_initializer_conv2d())
# you could also just define your network layer 'now' using this kernel
# ....
# Which would need give you a model (rather just weights)
如果您需要重复使用权重集,第二次调用可以为您提供一份副本.
If you need to re-use the set of weights, the second call can get you a copy of it.
with tf.variable_scope(name, reuse=True) as scope:
kernel = tf.get_variable("W")
# you can now reuse the xavier initialized variable
# ....
这篇关于在 Tensorflow 中更改变量的初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!