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.2 kB

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

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