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.

Execute.cs 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Linq;
  4. namespace Tensorflow.Eager
  5. {
  6. public class Execute
  7. {
  8. /// <summary>
  9. /// Execute a TensorFlow operation.
  10. /// </summary>
  11. /// <param name="op_name">
  12. /// Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
  13. /// execute.
  14. /// </param>
  15. /// <param name="num_outputs">
  16. /// The number of outputs of the operation to fetch.
  17. /// </param>
  18. /// <param name="inputs">
  19. /// A list of inputs to the operation. Each entry should be a Tensor, or
  20. /// a value which can be passed to the Tensor constructor to create one.
  21. /// </param>
  22. /// <param name="attrs">
  23. /// A tuple with alternating string attr names and attr values for this
  24. /// operation.
  25. /// </param>
  26. /// <param name="ctx">The value of context.context().</param>
  27. /// <param name="name">Customized name for the operation.</param>
  28. /// <returns>List of output Tensor objects. The list is empty if there are no outputs</returns>
  29. public EagerTensor[] execute(Context ctx, string op_name, int num_outputs,
  30. EagerTensor[] inputs, object[] attrs,
  31. string name = null)
  32. {
  33. ctx.ensure_initialized();
  34. using var status = new Status();
  35. BindingArray results = c_api.TFE_QuickExecute(ctx,
  36. ctx.device_name,
  37. op_name,
  38. inputs.Select(x => x.EagerTensorHandle).ToArray(),
  39. inputs.Length,
  40. op => wrap_tfe_src.SetOpAttrs(op, attrs),
  41. status);
  42. status.Check(true);
  43. return results.Data().Select(x => new EagerTensor(x)).ToArray();
  44. }
  45. public (TF_DataType, EagerTensor[]) args_to_matching_eager(Context ctx, TF_DataType default_dtype = TF_DataType.DtInvalid, object[] args = null)
  46. {
  47. if (args.Length == 0 && default_dtype != TF_DataType.DtInvalid)
  48. return (default_dtype, null);
  49. if (args.Count(x => x is EagerTensor) == args.Length)
  50. return ((args[0] as EagerTensor).dtype, args.Select(x => x as EagerTensor).ToArray());
  51. var dtype = TF_DataType.DtInvalid;
  52. foreach (var x in args)
  53. {
  54. if (x is EagerTensor et)
  55. dtype = et.dtype;
  56. }
  57. if (dtype == TF_DataType.DtInvalid)
  58. {
  59. var ret = new List<EagerTensor>();
  60. foreach (var t in args)
  61. {
  62. ret.Add(ops.convert_to_tensor(t, dtype, preferred_dtype: default_dtype, ctx: ctx) as EagerTensor);
  63. if (dtype == TF_DataType.DtInvalid)
  64. dtype = ret.Last().dtype;
  65. }
  66. return (dtype, ret.ToArray());
  67. }
  68. else
  69. throw new NotImplementedException("");
  70. }
  71. }
  72. }