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.

CreateOpFromTfOperationTest.cs 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Tensorflow;
  6. namespace TensorFlowNET.UnitTest
  7. {
  8. /// <summary>
  9. /// excerpt of tensorflow/python/framework/ops_test.py
  10. /// # These cases test the private Graph._create_op_from_tf_operation
  11. /// # method. Arguably we should only test the public APIs that depend on this
  12. /// # method. However, this logic is complex and tricky, and it can be difficult to
  13. /// # ascertain if we have adequate coverage (e.g. a graph may run successfully if
  14. /// # the control flow context isn't set properly, but a more complicated use case
  15. /// # that might not be obvious to test will fail). Thus we instead explicitly test
  16. /// # the low-level behavior.
  17. /// </summary>
  18. [TestClass]
  19. public class CreateOpFromTfOperationTest : PythonTest
  20. {
  21. [TestMethod]
  22. public void TestShape()
  23. {
  24. var graph = tf.Graph().as_default();
  25. with<Graph>(graph, g =>
  26. {
  27. var x = constant_op.constant(new [,] { {1, 2, 3}, {4, 5, 6}});
  28. var (c_op, op_desc) = ops._create_c_op(g, ops._NodeDef("Identity", "myop"), new[] {x}, new Operation[0]);
  29. var op = g._create_op_from_tf_operation(c_op);
  30. Assert.AreEqual("myop", op.name);
  31. Assert.AreEqual("Identity", op.type);
  32. Assert.AreEqual(1, len(op.outputs));
  33. assertItemsEqual(new []{2, 3}, op.outputs[0].shape);
  34. });
  35. }
  36. [TestMethod]
  37. public void TestUniqueName()
  38. {
  39. var graph = tf.Graph().as_default();
  40. with<Graph>(graph, g =>
  41. {
  42. //var (c_op,op_desc) = ops._create_c_op(g, ops._NodeDef("Const", "myop"), new Tensor[0], new Operation[0]);
  43. //var (c_op2, op_desc1) = ops._create_c_op(g, ops._NodeDef("Const", "myop_1"), new Tensor[0], new Operation[0]);
  44. //var op = g._create_op_from_tf_operation(c_op);
  45. //var op2 = g._create_op_from_tf_operation(c_op2);
  46. var op = constant_op.constant(0, name:"myop").op;
  47. var op2 = constant_op.constant(0, name: "myop_1").op;
  48. // Create ops with same names as op1 and op2. We expect the new names to be
  49. // uniquified.
  50. var op3 = constant_op.constant(0, name: "myop").op;
  51. var op4 = constant_op.constant(0, name: "myop_1").op;
  52. self.assertEqual(op.name, "myop");
  53. self.assertEqual(op2.name, "myop_1");
  54. self.assertEqual(op3.name, "myop_2");
  55. self.assertEqual(op4.name, "myop_1_1");
  56. });
  57. }
  58. /*
  59. @test_util.run_v1_only("b/120545219")
  60. def testCond(self):
  61. g = ops.Graph()
  62. with g.as_default():
  63. x = test_ops.int_output()
  64. def true_fn():
  65. ops._create_c_op(ops.get_default_graph(),
  66. ops._NodeDef("IntInput", "cond/myop"), [x], [])
  67. new_ops = g._add_new_tf_operations()
  68. self.assertEqual(len(new_ops), 1)
  69. return x
  70. control_flow_ops.cond(x < 10, true_fn, lambda: x)
  71. op = g.get_operation_by_name("cond/myop")
  72. self.assertIsNotNone(op)
  73. self.assertEqual(op.name, "cond/myop")
  74. self.assertEqual(op.type, "IntInput")
  75. self.assertEqual(op.outputs, [])
  76. op_input = op.inputs[0].op
  77. self.assertEqual(op_input.type, "Switch")
  78. self.assertEqual(op_input.inputs[0], x)
  79. self.assertEqual(op.graph, g)
  80. # pylint: disable=protected-access
  81. self.assertIsNotNone(op._get_control_flow_context())
  82. self.assertEqual(op._get_control_flow_context().name,
  83. "cond/cond_text")
  84. # pylint: enable=protected-access
  85. @test_util.run_v1_only("b/120545219")
  86. def testWhileLoop(self):
  87. g = ops.Graph()
  88. with g.as_default():
  89. x = test_ops.int_output()
  90. def body(i):
  91. ops._create_c_op(ops.get_default_graph(),
  92. ops._NodeDef("IntInput", "myloop/myop"), [x], [])
  93. new_ops = g._add_new_tf_operations()
  94. self.assertEqual(len(new_ops), 1)
  95. return i
  96. control_flow_ops.while_loop(lambda i: i < 10, body, [0], name="myloop")
  97. op = g.get_operation_by_name("myloop/myop")
  98. self.assertIsNotNone(op)
  99. self.assertEqual(op.name, "myloop/myop")
  100. self.assertEqual(op.type, "IntInput")
  101. self.assertEqual(op.outputs, [])
  102. op_input = op.inputs[0].op
  103. self.assertEqual(op_input.type, "Enter")
  104. self.assertEqual(list(op_input.inputs), [x])
  105. self.assertEqual(op.graph, g)
  106. # pylint: disable=protected-access
  107. self.assertIsNotNone(op._get_control_flow_context())
  108. self.assertEqual(op._get_control_flow_context().name,
  109. "myloop/while_context")
  110. # pylint: enable=protected-access
  111. @test_util.run_v1_only("b/120545219")
  112. def testWhileLoopWithInternalControlDep(self):
  113. g = ops.Graph()
  114. with g.as_default():
  115. x = test_ops.int_output()
  116. def body(i):
  117. c = constant_op.constant(1.0, name="c")
  118. ops._create_c_op(ops.get_default_graph(),
  119. ops._NodeDef("IntInput", "myloop/myop"), [x], [])
  120. with ops.control_dependencies([c]):
  121. new_ops = g._add_new_tf_operations()
  122. self.assertEqual(len(new_ops), 1)
  123. return i
  124. control_flow_ops.while_loop(lambda i: i < 10, body, [0], name="myloop")
  125. op = g.get_operation_by_name("myloop/myop")
  126. self.assertIsNotNone(op)
  127. c = g.get_operation_by_name("myloop/c")
  128. self.assertIsNotNone(c)
  129. # Internal control dep is preserved
  130. self.assertEqual(op.control_inputs, [c])
  131. @test_util.run_v1_only("b/120545219")
  132. def testWhileLoopWithExternalControlDep(self):
  133. g = ops.Graph()
  134. with g.as_default():
  135. x = test_ops.int_output()
  136. c = constant_op.constant(1.0)
  137. def body(i):
  138. ops._create_c_op(ops.get_default_graph(),
  139. ops._NodeDef("IntInput", "myloop/myop"), [x], [])
  140. with ops.control_dependencies([c]):
  141. new_ops = g._add_new_tf_operations()
  142. self.assertEqual(len(new_ops), 1)
  143. return i
  144. control_flow_ops.while_loop(lambda i: i < 10, body, [0], name="myloop")
  145. op = g.get_operation_by_name("myloop/myop")
  146. self.assertIsNotNone(op)
  147. # External control dep is removed and replaced with internal control dep
  148. self.assertNotEqual(op.control_inputs[0], c.op)
  149. self.assertIsNotNone(op.control_inputs[0]._get_control_flow_context())
  150. */
  151. }
  152. }

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