How do I get the current value of a Variable?

后端 未结 4 703
走了就别回头了
走了就别回头了 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:19

    The only way to get the value of the variable is by running it in a session. In the FAQ it is written that:

    A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.

    So TF equivalent would be:

    import tensorflow as tf
    
    x = tf.Variable([1.0, 2.0])
    
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
        v = sess.run(x)
        print(v)  # will show you your variable.
    

    The part with init = global_variables_initializer() is important and should be done in order to initialize variables.

    Also, take a look at InteractiveSession if you work in IPython.

提交回复
热议问题