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.

Operation.Input.cs 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Tensorflow
  7. {
  8. // from ops.py
  9. public partial class Operation
  10. {
  11. public TF_Output Input(int index) => c_api.TF_OperationInput(new TF_Input(_handle, index));
  12. public TF_DataType InputType(int index) => c_api.TF_OperationInputType(new TF_Input(_handle, index));
  13. public int InputListLength(string name) => c_api.TF_OperationInputListLength(_handle, name, status);
  14. public int NumInputs => c_api.TF_OperationNumInputs(_handle);
  15. private TF_DataType[] _input_types => _inputs._inputs.Select(x => x.dtype).ToArray();
  16. private InputList _inputs;
  17. public InputList inputs
  18. {
  19. get
  20. {
  21. if (_inputs == null)
  22. {
  23. var retval = new Tensor[NumInputs];
  24. for (int i = 0; i < NumInputs; i++)
  25. {
  26. var tf_outputs = Input(i);
  27. var op = new Operation(tf_outputs.oper);
  28. retval[i] = op.outputs[tf_outputs.index];
  29. }
  30. _inputs = new InputList(retval);
  31. }
  32. return _inputs;
  33. }
  34. }
  35. public int NumControlInputs => c_api.TF_OperationNumControlInputs(_handle);
  36. /// <summary>
  37. /// The `Operation` objects on which this op has a control dependency.
  38. ///
  39. /// Before this op is executed, TensorFlow will ensure that the
  40. /// operations in `self.control_inputs` have finished executing.This
  41. /// mechanism can be used to run ops sequentially for performance
  42. /// reasons, or to ensure that the side effects of an op are observed
  43. /// in the correct order.
  44. /// </summary>
  45. public Operation[] control_inputs
  46. {
  47. get
  48. {
  49. return GetControlInputs();
  50. }
  51. }
  52. public unsafe Operation[] GetControlInputs()
  53. {
  54. var control_inputs = new Operation[NumControlInputs];
  55. if (NumControlInputs > 0)
  56. {
  57. IntPtr control_input_handle = Marshal.AllocHGlobal(Marshal.SizeOf<IntPtr>() * NumControlInputs);
  58. c_api.TF_OperationGetControlInputs(_handle, control_input_handle, NumControlInputs);
  59. for (int i = 0; i < NumControlInputs; i++)
  60. {
  61. var handle = control_input_handle + Marshal.SizeOf<IntPtr>() * i;
  62. control_inputs[i] = new Operation(*(IntPtr*)handle);
  63. }
  64. }
  65. return control_inputs;
  66. }
  67. }
  68. }

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