How can I copy a variable in tensorflow

前端 未结 4 962
清歌不尽
清歌不尽 2020-12-24 13:19

In numpy I can create a copy of the variable with numpy.copy. Is there a similar method, that I can use to create a copy of a Tensor in TensorFlow?

4条回答
  •  情书的邮戳
    2020-12-24 13:57

    You asked how to copy a variable in the title, but how to copy a tensor in the question. Let's look at the different possible answers.

    (1) You want to create a tensor that has the same value that is currently stored in a variable that we'll call var.

    tensor = tf.identity(var)
    

    But remember, 'tensor' is a graph node that will have that value when evaluated, and any time you evaluate it, it will grab the current value of var. You can play around with control flow ops such as with_dependencies() to see the ordering of updates to the variable and the timing of the identity.

    (2) You want to create another variable and set its value to the value currently stored in a variable:

    import tensorflow as tf
    var = tf.Variable(0.9)
    var2 = tf.Variable(0.0)
    copy_first_variable = var2.assign(var)
    init = tf.initialize_all_variables()
    sess = tf.Session()
    
    sess.run(init)
    
    print sess.run(var2)
    sess.run(copy_first_variable)
    print sess.run(var2)
    

    (3) You want to define a variable and set its starting value to the same thing you already initialized a variable to (this is what nivwu.. above answered):

    var2 = tf.Variable(var.initialized_value())
    

    var2 will get initialized when you call tf.initialize_all_variables. You can't use this to copy var after you've already initialized the graph and started running things.

提交回复
热议问题