01
Session
每一个Session都维护各自变量的副本。
如下所示:
W = tf.Variable(10) sess1 = tf.Session() sess2 = tf.Session() sess1.run(W.initializer) sess2.run(W.initializer) print sess1.run(W.assign_add(10)) # >> 20 print sess2.run(W.assign_sub(2)) # >> ?
?等号8,sess1和sess2各自维护W,所以sess1中W增加10,不会影响sess2的W,所以它等于10-2=8.
02
Session vs InteractiveSession
有时候我们会看到:InteractiveSession,而不是Session,它们区别是?
One major change is the use of an InteractiveSession, which allows us to run variables without needing to constantly refer to the session object (less typing!).
InteractiveSession()
sess = tf.InteractiveSession() a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # We can just use 'c.eval()' without specifying the context 'sess' print(c.eval()) sess.close()
Session()
sess = tf.Session() a = tf.constant(5.0) b = tf.constant(6.0) c = a * b with tf.Session() as sess: sess.run(print(c.eval()))