# To use the value of a tf.Variable in a Tesnorflow graph , simply treat it like a normal tf.Tensor import tensorflow as tf v = tf.get_variable("v", shape=(), initializer=tf.zeros_initializer()) w = v + 1 # w is a tf.Tensor which is computed based on the value of v # Any time a variable is used in an expression it gets automatically # converted to a tf.Tensor representing its value # To assign a value to a variable , use the methods assign , assign_add # and friends in the tf.Variable class . For example v1 = tf.get_variable("v1", shape=(), initializer=tf.zeros_initializer()) assignment = v1.assign_add(1) with tf.Session() as sess: tf.global_variables_initializer().run() print(sess.run(assignment))
下面是输出的结果
2018-02-17 10:55:36.215165: I C: f_jenkinsworkspace el-winMwindowsPY35 ensorflowcoreplatformcpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 1.0