tf.InteractiveSession()认为自己就是默认会话,是一种交互的方式,可以在没有构建完整的计算图时,就可以运行sessoin。tf.Session在使用时,已经构建好了完整的计算图
sess = tf.InteractiveSession()
a = tf.constant(1)
b = tf.constant(2)
c = a+b
print(c.eval())
sess.close()
使用tf.Session()时要指定sess为默认会话
a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
sess=tf.Session()
print (c.eval())
代码会编译错误,因为不知道默认的会话,(Cannot evaluate tensor using `eval()`: No default session is registered. )
一种解决方法是解决方法 print(c.eval(session=sess))
另一种是使用with tf.Session() as sess: