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

7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 bool GetNodeDef(Operation oper, ref NodeDef node_def)
  40. {
  41. var s = new Status();
  42. var buffer = new Buffer();
  43. c_api.TF_OperationToNodeDef(oper, buffer, s);
  44. return s.Code == TF_Code.TF_OK;
  45. }
  46. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  47. {
  48. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  49. c_api.TF_SetAttrType(desc, "dtype", dtype);
  50. if(dims != null)
  51. {
  52. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  53. }
  54. op = c_api.TF_FinishOperation(desc, s);
  55. s.Check();
  56. }
  57. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  58. {
  59. Operation op = null;
  60. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  61. return op;
  62. }
  63. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  64. {
  65. var desc = c_api.TF_NewOperation(graph, "Const", name);
  66. c_api.TF_SetAttrTensor(desc, "value", t, s);
  67. s.Check();
  68. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  69. op = c_api.TF_FinishOperation(desc, s);
  70. s.Check();
  71. }
  72. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  73. {
  74. Operation op = null;
  75. ConstHelper(t, graph, s, name, ref op);
  76. return op;
  77. }
  78. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  79. {
  80. return Const(new Tensor(v), graph, s, name);
  81. }
  82. }
  83. }

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

Contributors (1)