“freeze” some variables/scopes in tensorflow: stop_gradient vs passing variables to minimize

前端 未结 4 2001
无人共我
无人共我 2020-11-30 18:31

I am trying to implement Adversarial NN, which requires to \'freeze\' one or the other part of the graph during alternating training minibatches. I.e. there two sub-networks

4条回答
  •  长情又很酷
    2020-11-30 18:51

    I don't know if my approach has down sides, but I solved this issue for myself with this construct:

    do_gradient = 
    no_gradient = 1 - do_gradient
    wrapped_op = do_gradient * original + no_gradient * tf.stop_gradient(original)
    

    So if do_gradient = 1, the values and gradients will flow through just fine, but if do_gradient = 0, then the values will only flow through the stop_gradient op, which will stop the gradients flowing back.

    For my scenario, hooking do_gradient up to an index of a random_shuffle tensor let me randomly train different pieces of my network.

提交回复
热议问题