#用tf.Session()函数
import tensorflow as tf
sess=tf.Session()
w1=tf.Variable(5,dtype=tf.int32)
sess.run(tf.global_variables_initializer())
print(sess.run(w1))
#或者用with结构
import tensorflow as tf
w1=tf.Variable(5,dtype=tf.int32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(w1))
import tensorflow as tf
biases=tf.Variable(tf.zeros([2,3]))#定义一个2x3的全0矩阵
sess=tf.InteractiveSession()#使用InteractiveSession函数
biases.initializer.run()#使用初始化器 initializer op 的 run() 方法初始化 'biases'
print(sess.run(biases))#输出变量值
sess=tf.InteractiveSession()
w=tf.Variable(1,dtype=tf.int32)
tf.global_variables_initializer().run()
print(sess.run(w))
2.tensorflow中一般的tensor的值
import tensorflow as tf
x = tf.constant(1)
with tf.Session() as sess:
print sess.run(x)
import tensorflow as tf
x = tf.constant(1)
sess = tf.InteractiveSession()
print (x.eval())