Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #### 1. 在会话(session)中计算图
  2. ```python
  3. g = tf.Graph()
  4. with tf.Session(graph=g) as sess:
  5. sess.run(...)
  6. ```
  7. #### 2. 加载模型后不用给变量初始化
  8. ```python
  9. with tf.Session(graph=g) as sess:
  10. # # initialize the variables
  11. # init = tf.global_variables_initializer()
  12. # sess.run(init)
  13. # create a saver to save variables in restoring
  14. saver = tf.train.Saver(tf.global_variables())
  15. # load the model
  16. saver.restore(sess, tf.train.latest_checkpoint(modelPath))
  17. ```
  18. #### 3. 可同时使用多个session
  19. ```python
  20. sess1 = tf.Session(g1)
  21. sess2 = tf.Session(g2)
  22. sess1.run(...)
  23. sess2.run(...)
  24. ```
  25. #### 但是,图计算过程中,(代码)左侧有相同变量在两个图中同时计算
  26. ```python
  27. g1=tf.Graph()
  28. g2=tf.Graph()
  29. with g1.as_default():
  30. a = tf.constant([1.0, 1.0])
  31. b = tf.constant([1.0, 1.0])
  32. result1 = a+c
  33. with g2.as_default():
  34. a = tf.constant([2.0, 2.0])
  35. b = tf.constant([2.0, 2.0])
  36. result2 = a+c
  37. sess1 = tf.Session(graph=g1)
  38. sess2 = tf.Session(graph=g2)
  39. sess1.run(result1); sess1.run(result2) #正确
  40. sess1.run(a) #错误
  41. sess2.run(a) #正确
  42. # 按顺序执行下来,a,b都应当在g2中计算
  43. ```
  44. #### tf.gloabl_variables()得到的是某个graph的变量,需在graph的上下文管理器中运行
  45. ```python
  46. with g.as_default():
  47. tf.global_variables()
  48. ```
  49. #### tf.global_variables()---tensorflow, g.get_operations()---graph
Add Comment
Please, Sign In to add comment