How do I get the current value of a Variable?

后端 未结 4 706
走了就别回头了
走了就别回头了 2020-12-04 21:49

Suppose we have a variable:

x = tf.Variable(...)

This variable can be updated during the training process using the assign() m

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 22:21

    tf.Print can simplify your life!

    tf.Print will print the value of the tensor(s) you tell it to print at the moment where the tf.Print line is called in your code when your code is evaluated.

    So for example:

    import tensorflow as tf
    x = tf.Variable([1.0, 2.0])
    x = tf.Print(x,[x])
    x = 2* x
    
    tf.initialize_all_variables()
    
    sess = tf.Session()
    sess.run()
    

    [1.0 2.0 ]

    because it prints the value of x at the moment when the tf.Print line is. If instead you do

    v = x.eval()
    print(v)
    

    you will get:

    [2.0 4.0 ]

    because it will give you the final value of x.

提交回复
热议问题