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.

CSession.cs 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Tensorflow;
  5. using Tensorflow.Util;
  6. namespace TensorFlowNET.UnitTest
  7. {
  8. /// <summary>
  9. /// tensorflow\c\c_test_util.cc
  10. /// TEST(CAPI, Session)
  11. /// </summary>
  12. public class CSession
  13. {
  14. private IntPtr session_;
  15. private List<TF_Output> inputs_ = new List<TF_Output>();
  16. private List<Tensor> input_values_ = new List<Tensor>();
  17. private List<TF_Output> outputs_ = new List<TF_Output>();
  18. private List<Tensor> output_values_ = new List<Tensor>();
  19. private List<IntPtr> targets_ = new List<IntPtr>();
  20. public CSession(Graph graph, Status s, bool user_XLA = false)
  21. {
  22. lock (Locks.ProcessWide)
  23. {
  24. var config = new ConfigProto {InterOpParallelismThreads = 4};
  25. session_ = new Session(graph, config, s);
  26. }
  27. }
  28. public void SetInputs(Dictionary<Operation, Tensor> inputs)
  29. {
  30. DeleteInputValues();
  31. inputs_.Clear();
  32. foreach (var input in inputs)
  33. {
  34. inputs_.Add(new TF_Output(input.Key, 0));
  35. input_values_.Add(input.Value);
  36. }
  37. }
  38. private void DeleteInputValues()
  39. {
  40. //clearing is enough as they will be disposed by the GC unless they are referenced else-where.
  41. input_values_.Clear();
  42. }
  43. public void SetOutputs(TF_Output[] outputs)
  44. {
  45. ResetOutputValues();
  46. outputs_.Clear();
  47. foreach (var output in outputs)
  48. {
  49. outputs_.Add(output);
  50. output_values_.Add(IntPtr.Zero);
  51. }
  52. }
  53. private void ResetOutputValues()
  54. {
  55. //clearing is enough as they will be disposed by the GC unless they are referenced else-where.
  56. output_values_.Clear();
  57. }
  58. public unsafe void Run(Status s)
  59. {
  60. var inputs_ptr = inputs_.ToArray();
  61. var input_values_ptr = input_values_.Select(x => (IntPtr) x).ToArray();
  62. var outputs_ptr = outputs_.ToArray();
  63. var output_values_ptr = output_values_.Select(x => IntPtr.Zero).ToArray();
  64. IntPtr[] targets_ptr = new IntPtr[0];
  65. c_api.TF_SessionRun(session_, null, inputs_ptr, input_values_ptr, inputs_ptr.Length,
  66. outputs_ptr, output_values_ptr, outputs_.Count,
  67. targets_ptr, targets_.Count,
  68. IntPtr.Zero, s);
  69. s.Check();
  70. output_values_[0] = output_values_ptr[0];
  71. }
  72. public IntPtr output_tensor(int i)
  73. {
  74. return output_values_[i];
  75. }
  76. public void CloseAndDelete(Status s)
  77. {
  78. DeleteInputValues();
  79. ResetOutputValues();
  80. }
  81. }
  82. }