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.Output.cs 3.5 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Linq;
  15. using System.Runtime.InteropServices;
  16. using static Tensorflow.Binding;
  17. namespace Tensorflow
  18. {
  19. public partial class Operation
  20. {
  21. public int NumOutputs => _handle == IntPtr.Zero ? -1 : c_api.TF_OperationNumOutputs(_handle);
  22. public TF_DataType OutputType(int index) => c_api.TF_OperationOutputType(_tf_output(index));
  23. public int OutputListLength(string name)
  24. {
  25. int num = c_api.TF_OperationOutputListLength(_handle, name, tf.Status);
  26. tf.Status.Check(true);
  27. return num;
  28. }
  29. protected Tensor[] _outputs;
  30. public virtual Tensor[] outputs => _outputs;
  31. public Tensor output => _outputs.FirstOrDefault();
  32. public int NumControlOutputs => _handle == IntPtr.Zero ? -1 : c_api.TF_OperationNumControlOutputs(_handle);
  33. public int OutputNumConsumers(int index) => c_api.TF_OperationOutputNumConsumers(new TF_Output(_handle, index));
  34. public TF_Output this[int index] => _tf_output(index);
  35. /// <summary>
  36. /// List this operation's output types.
  37. /// </summary>
  38. public TF_DataType[] _output_types
  39. {
  40. get
  41. {
  42. var output_types = range(NumOutputs)
  43. .Select(i => OutputType(i))
  44. .ToArray();
  45. return output_types;
  46. }
  47. }
  48. public unsafe TF_Input[] OutputConsumers(int index, int max_consumers)
  49. {
  50. var handle = Marshal.AllocHGlobal(Marshal.SizeOf<TF_Input>());
  51. int num = c_api.TF_OperationOutputConsumers(new TF_Output(_handle, index), handle, max_consumers);
  52. var consumers = new TF_Input[num];
  53. var inputptr = (TF_Input*)handle;
  54. for (int i = 0; i < num; i++)
  55. consumers[i] = *(inputptr + i);
  56. Marshal.FreeHGlobal(handle);
  57. return consumers;
  58. }
  59. public unsafe Operation[] GetControlOutputs()
  60. {
  61. var control_outputs = new Operation[NumControlOutputs];
  62. if (NumControlOutputs > 0)
  63. {
  64. IntPtr control_output_handle = Marshal.AllocHGlobal(Marshal.SizeOf<IntPtr>() * NumControlOutputs);
  65. c_api.TF_OperationGetControlOutputs(_handle, control_output_handle, NumControlOutputs);
  66. for (int i = 0; i < NumControlOutputs; i++)
  67. {
  68. var handle = control_output_handle + Marshal.SizeOf<IntPtr>() * i;
  69. control_outputs[i] = new Operation(*(IntPtr*)handle);
  70. }
  71. Marshal.FreeHGlobal(control_output_handle);
  72. }
  73. return control_outputs;
  74. }
  75. }
  76. }