Change initializer of Variable in Tensorflow

前端 未结 1 1006
天命终不由人
天命终不由人 2021-02-15 12:27

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 i

1条回答
  •  被撕碎了的回忆
    2021-02-15 13:08

    The problem is that initialization can't be changed on setting up reuse (the initialization is set during the first block).

    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
        # ....
    

    0 讨论(0)
提交回复
热议问题