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.3 kB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. var inputs = new TF_Output[]
  24. {
  25. new TF_Output(l, 0),
  26. new TF_Output(r, 0),
  27. };
  28. c_api.TF_AddInputList(desc, inputs, inputs.Length);
  29. op = c_api.TF_FinishOperation(desc, s);
  30. s.Check();
  31. }
  32. public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
  33. {
  34. var buffer = new Buffer();
  35. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  36. attr_value = AttrValue.Parser.ParseFrom(buffer.Data);
  37. buffer.Dispose();
  38. return s.Code == TF_Code.TF_OK;
  39. }
  40. public static bool GetNodeDef(Operation oper, ref NodeDef node_def)
  41. {
  42. var s = new Status();
  43. var buffer = new Buffer();
  44. c_api.TF_OperationToNodeDef(oper, buffer, s);
  45. return s.Code == TF_Code.TF_OK;
  46. }
  47. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  48. {
  49. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  50. c_api.TF_SetAttrType(desc, "dtype", dtype);
  51. if(dims != null)
  52. {
  53. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  54. }
  55. op = c_api.TF_FinishOperation(desc, s);
  56. s.Check();
  57. }
  58. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  59. {
  60. Operation op = null;
  61. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  62. return op;
  63. }
  64. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  65. {
  66. var desc = c_api.TF_NewOperation(graph, "Const", name);
  67. c_api.TF_SetAttrTensor(desc, "value", t, s);
  68. s.Check();
  69. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  70. op = c_api.TF_FinishOperation(desc, s);
  71. s.Check();
  72. }
  73. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  74. {
  75. Operation op = null;
  76. ConstHelper(t, graph, s, name, ref op);
  77. return op;
  78. }
  79. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  80. {
  81. return Const(new Tensor(v), graph, s, name);
  82. }
  83. }
  84. }

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

Contributors (1)