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.

c_test_util.cs 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using Tensorflow;
  6. using Buffer = Tensorflow.Buffer;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. /// <summary>
  10. /// Port from `tensorflow\c\c_test_util.cc`
  11. /// </summary>
  12. public static class c_test_util
  13. {
  14. public static Operation Add(Operation l, Operation r, Graph graph, Status s, string name = "add")
  15. {
  16. Operation op = null;
  17. AddOpHelper(l, r, graph, s, name, ref op, true);
  18. return op;
  19. }
  20. public static void AddOpHelper(Operation l, Operation r, Graph graph, Status s, string name, ref Operation op, bool check)
  21. {
  22. var desc = c_api.TF_NewOperation(graph, "AddN", name);
  23. c_api.TF_AddInputList(desc, new TF_Output[]
  24. {
  25. new TF_Output(l, 0),
  26. new TF_Output(r, 0),
  27. }, 2);
  28. op = c_api.TF_FinishOperation(desc, s);
  29. s.Check();
  30. }
  31. public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
  32. {
  33. var buffer = new Buffer();
  34. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  35. attr_value = AttrValue.Parser.ParseFrom(buffer.Data);
  36. buffer.Dispose();
  37. return s.Code == TF_Code.TF_OK;
  38. }
  39. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  40. {
  41. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  42. c_api.TF_SetAttrType(desc, "dtype", dtype);
  43. if(dims != null)
  44. {
  45. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  46. }
  47. op = c_api.TF_FinishOperation(desc, s);
  48. s.Check();
  49. }
  50. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  51. {
  52. Operation op = null;
  53. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  54. return op;
  55. }
  56. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  57. {
  58. var desc = c_api.TF_NewOperation(graph, "Const", name);
  59. c_api.TF_SetAttrTensor(desc, "value", t, s);
  60. s.Check();
  61. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  62. op = c_api.TF_FinishOperation(desc, s);
  63. s.Check();
  64. if(op == null)
  65. {
  66. throw new Exception("c_api.TF_FinishOperation failed.");
  67. }
  68. }
  69. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  70. {
  71. Operation op = null;
  72. ConstHelper(t, graph, s, name, ref op);
  73. return op;
  74. }
  75. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  76. {
  77. return Const(new Tensor(v), graph, s, name);
  78. }
  79. }
  80. }

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

Contributors (1)