Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### 1. 在会话(session)中计算图
- ```python
- g = tf.Graph()
- with tf.Session(graph=g) as sess:
- sess.run(...)
- ```
- #### 2. 加载模型后不用给变量初始化
- ```python
- with tf.Session(graph=g) as sess:
- # # initialize the variables
- # init = tf.global_variables_initializer()
- # sess.run(init)
- # create a saver to save variables in restoring
- saver = tf.train.Saver(tf.global_variables())
- # load the model
- saver.restore(sess, tf.train.latest_checkpoint(modelPath))
- ```
- #### 3. 可同时使用多个session
- ```python
- sess1 = tf.Session(g1)
- sess2 = tf.Session(g2)
- sess1.run(...)
- sess2.run(...)
- ```
- #### 但是,图计算过程中,(代码)左侧有相同变量在两个图中同时计算
- ```python
- g1=tf.Graph()
- g2=tf.Graph()
- with g1.as_default():
- a = tf.constant([1.0, 1.0])
- b = tf.constant([1.0, 1.0])
- result1 = a+c
- with g2.as_default():
- a = tf.constant([2.0, 2.0])
- b = tf.constant([2.0, 2.0])
- result2 = a+c
- sess1 = tf.Session(graph=g1)
- sess2 = tf.Session(graph=g2)
- sess1.run(result1); sess1.run(result2) #正确
- sess1.run(a) #错误
- sess2.run(a) #正确
- # 按顺序执行下来,a,b都应当在g2中计算
- ```
- #### tf.gloabl_variables()得到的是某个graph的变量,需在graph的上下文管理器中运行
- ```python
- with g.as_default():
- tf.global_variables()
- ```
- #### tf.global_variables()---tensorflow, g.get_operations()---graph
Add Comment
Please, Sign In to add comment