tf.InteractiveSession()和tf.Session()

tf.InteractiveSession()适合用于python交互环境

tf.Session()适合用于源代码中

1、tf.InteractiveSession()

直接用eval()就可以直接获得结果,无需运行sess.run()

2、tf.Session()一般在代码中使用

import tensorflow as tf
import numpy as np

vector_np = np.array([1,2,4])
vector_tf = tf.constant(vector_np)

with tf.Session() as sess:
    # print (sess.run(tensor_id.get_shape()))
    print (sess.run(vector_tf))

注意:我遇到过这种情况,在pycharm编辑器,先在pycharm交互环境使用了tf.InteractiveSession(),没有用sess.close(),结果又在编辑器里运行代码tf.Session(),

结果可以想象报错了,所有在同一编辑器中不能同时使用启动两个session

猜你喜欢

转载自www.cnblogs.com/heiao10duan/p/9288355.html
tf