import tensorflow as tf g1 = tf.Graph() with g1.as_default(): v = tf.get_variable("v", [1], initializer = tf.zeros_initializer()) # 设置初始值为0 g2 = tf.Graph() with g2.as_default(): v = tf.get_variable("v", [1], initializer = tf.ones_initializer()) # 设置初始值为1 with tf.Session(graph = g1) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse=True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph = g2) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse=True): print(sess.run(tf.get_variable("v")))
import tensorflow as tf a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2.0, 3.0], name="b") result = a + b print result sess = tf.InteractiveSession () print(result.eval()) sess.close()
# 创建一个会话。 sess = tf.Session() # 使用会话得到之前计算的结果。 print(sess.run(result)) # 关闭会话使得本次运行中使用到的资源可以被释放。 sess.close()
with tf.Session() as sess: print(sess.run(result))
sess = tf.Session() with sess.as_default(): print(result.eval())
sess = tf.Session() # 下面的两个命令有相同的功能。 print(sess.run(result)) print(result.eval(session=sess))
sess = tf.InteractiveSession () print(result.eval()) sess.close()
config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True) sess1 = tf.InteractiveSession(config=config) sess2 = tf.Session(config=config)