You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Graph.md 2.8 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Chapter 3. Graph
  2. TensorFlow uses a **dataflow graph** to represent your computation in terms of the dependencies between individual operations. A graph defines the computation. It doesn't compute anything, it doesn't hold any values, it just defines the operations that you specified in your code.
  3. ### Defining the Graph
  4. We define a graph with a variable and three operations: `variable` returns the current value of our variable. `initialize` assigns the initial value of 31 to that variable. `assign` assigns the new value of 12 to that variable.
  5. ```csharp
  6. with<Graph>(tf.Graph().as_default(), graph =>
  7. {
  8. var variable = tf.Variable(31, name: "tree");
  9. tf.global_variables_initializer();
  10. variable.assign(12);
  11. });
  12. ```
  13. TF.NET simulate a `with` syntax to manage the Graph lifecycle which will be disposed when the graph instance is no long need. The graph is also what the sessions in the next chapter use when not manually specifying a graph because use invoked the `as_default()`.
  14. A typical graph is looks like below:
  15. ![image](../assets/graph_vis_animation.gif)
  16. ### Save Model
  17. Saving the model means saving all the values of the parameters and the graph.
  18. ```python
  19. saver = tf.train.Saver()
  20. saver.save(sess,'./tensorflowModel.ckpt')
  21. ```
  22. After saving the model there will be four files:
  23. * tensorflowModel.ckpt.meta:
  24. * tensorflowModel.ckpt.data-00000-of-00001:
  25. * tensorflowModel.ckpt.index
  26. * checkpoint
  27. We also created a protocol buffer file .pbtxt. It is human readable if you want to convert it to binary: `as_text: false`.
  28. * tensorflowModel.pbtxt:
  29. This holds a network of nodes, each representing one operation, connected to each other as inputs and outputs.
  30. ### Freezing the Graph
  31. ##### *Why we need it?*
  32. When we need to keep all the values of the variables and the Graph structure in a single file we have to freeze the graph.
  33. ```csharp
  34. from tensorflow.python.tools import freeze_graph
  35. freeze_graph.freeze_graph(input_graph = 'logistic_regression/tensorflowModel.pbtxt',
  36. input_saver = "",
  37. input_binary = False,
  38. input_checkpoint = 'logistic_regression/tensorflowModel.ckpt',
  39. output_node_names = "Softmax",
  40. restore_op_name = "save/restore_all",
  41. filename_tensor_name = "save/Const:0",
  42. output_graph = 'frozentensorflowModel.pb',
  43. clear_devices = True,
  44. initializer_nodes = "")
  45. ```
  46. ### Optimizing for Inference
  47. To Reduce the amount of computation needed when the network is used only for inferences we can remove some parts of a graph that are only needed for training.
  48. ### Restoring the Model