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 3.3 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. public void SetInputs(KeyValuePair<Operation, Tensor>[] inputs)
  39. {
  40. DeleteInputValues();
  41. inputs_.Clear();
  42. foreach (var input in inputs)
  43. {
  44. inputs_.Add(new TF_Output(input.Key, 0));
  45. input_values_.Add(input.Value);
  46. }
  47. }
  48. private void DeleteInputValues()
  49. {
  50. //clearing is enough as they will be disposed by the GC unless they are referenced else-where.
  51. input_values_.Clear();
  52. }
  53. public void SetOutputs(TF_Output[] outputs)
  54. {
  55. ResetOutputValues();
  56. outputs_.Clear();
  57. foreach (var output in outputs)
  58. {
  59. outputs_.Add(output);
  60. output_values_.Add(IntPtr.Zero);
  61. }
  62. }
  63. private void ResetOutputValues()
  64. {
  65. //clearing is enough as they will be disposed by the GC unless they are referenced else-where.
  66. output_values_.Clear();
  67. }
  68. public unsafe void Run(Status s)
  69. {
  70. var inputs_ptr = inputs_.ToArray();
  71. var input_values_ptr = input_values_.Select(x => (IntPtr)x).ToArray();
  72. var outputs_ptr = outputs_.ToArray();
  73. var output_values_ptr = output_values_.Select(x => IntPtr.Zero).ToArray();
  74. IntPtr[] targets_ptr = new IntPtr[0];
  75. c_api.TF_SessionRun(session_, null, inputs_ptr, input_values_ptr, inputs_ptr.Length,
  76. outputs_ptr, output_values_ptr, outputs_.Count,
  77. targets_ptr, targets_.Count,
  78. IntPtr.Zero, s.Handle);
  79. s.Check();
  80. for (var i = 0; i < outputs_.Count; i++)
  81. output_values_[i] = output_values_ptr[i];
  82. }
  83. public IntPtr output_tensor(int i)
  84. {
  85. return output_values_[i];
  86. }
  87. public void CloseAndDelete(Status s)
  88. {
  89. DeleteInputValues();
  90. ResetOutputValues();
  91. }
  92. }
  93. }