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.

Session.md 1.3 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829
  1. # Chapter. Session
  2. TensorFlow **session** runs parts of the graph across a set of local and remote devices. A session allows to execute graphs or part of graphs. It allocates resources (on one or more machines) for that and holds the actual values of intermediate results and variables.
  3. ### Running Computations in a Session
  4. Let's complete the example in last chapter. To run any of the operations, we need to create a session for that graph. The session will also allocate memory to store the current value of the variable.
  5. ```csharp
  6. with<Graph>(tf.Graph(), graph =>
  7. {
  8. var variable = tf.Variable(31, name: "tree");
  9. var init = tf.global_variables_initializer();
  10. var sess = tf.Session(graph);
  11. sess.run(init);
  12. var result = sess.run(variable); // 31
  13. var assign = variable.assign(12);
  14. result = sess.run(assign); // 12
  15. });
  16. ```
  17. The value of our variables is only valid within one session. If we try to get the value in another session. TensorFlow will raise an error of `Attempting to use uninitialized value foo`. Of course, we can use the graph in more than one session, because session copies graph definition to new memory area. We just have to initialize the variables again. The values in the new session will be completely independent from the previous one.