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.cs 1.5 kB

6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Tensorflow
  5. {
  6. public class Session : BaseSession, IPython
  7. {
  8. private IntPtr _handle;
  9. public Status Status = new Status();
  10. public SessionOptions Options { get; }
  11. public Graph graph;
  12. public Session(string target = "", Graph graph = null)
  13. {
  14. if(graph == null)
  15. {
  16. graph = tf.get_default_graph();
  17. }
  18. this.graph = graph;
  19. Options = new SessionOptions();
  20. _handle = c_api.TF_NewSession(graph, Options, Status);
  21. Status.Check();
  22. }
  23. public Session(IntPtr handle)
  24. {
  25. _handle = handle;
  26. }
  27. public Session(Graph graph, SessionOptions opts, Status s = null)
  28. {
  29. if (s == null)
  30. s = Status;
  31. _handle = c_api.TF_NewSession(graph, opts, s);
  32. Status.Check(true);
  33. }
  34. public static implicit operator IntPtr(Session session) => session._handle;
  35. public static implicit operator Session(IntPtr handle) => new Session(handle);
  36. public void close()
  37. {
  38. Dispose();
  39. }
  40. public void Dispose()
  41. {
  42. Options.Dispose();
  43. Status.Dispose();
  44. c_api.TF_DeleteSession(_handle, Status);
  45. }
  46. public void __enter__()
  47. {
  48. }
  49. public void __exit__()
  50. {
  51. }
  52. }
  53. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。