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.

CondTestCases.cs 2.8 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Newtonsoft.Json;
  3. using System;
  4. using Tensorflow;
  5. namespace TensorFlowNET.UnitTest.control_flow_ops_test
  6. {
  7. /// <summary>
  8. /// excerpt of tensorflow/python/framework/ops/control_flow_ops_test.py
  9. /// </summary>
  10. [TestClass]
  11. public class CondTestCases : PythonTest
  12. {
  13. [TestMethod]
  14. public void testCondTrue_ConstOnly()
  15. {
  16. var graph = tf.Graph().as_default();
  17. with(tf.Session(graph), sess =>
  18. {
  19. var x = tf.constant(2, name: "x");
  20. var y = tf.constant(5, name: "y");
  21. var z = control_flow_ops.cond(tf.less(x, y),
  22. () => tf.constant(22, name: "t22"),
  23. () => tf.constant(55, name: "f55"));
  24. int result = z.eval(sess);
  25. assertEquals(result, 22);
  26. });
  27. }
  28. [TestMethod]
  29. public void testCondFalse_ConstOnly()
  30. {
  31. var graph = tf.Graph().as_default();
  32. with(tf.Session(graph), sess =>
  33. {
  34. var x = tf.constant(2, name: "x");
  35. var y = tf.constant(1, name: "y");
  36. var z = control_flow_ops.cond(tf.less(x, y),
  37. () => tf.constant(22, name: "t22"),
  38. () => tf.constant(11, name: "f11"));
  39. int result = z.eval(sess);
  40. assertEquals(result, 11);
  41. });
  42. }
  43. [TestMethod]
  44. public void testCondTrue()
  45. {
  46. var graph = tf.Graph().as_default();
  47. with(tf.Session(graph), sess =>
  48. {
  49. var x = tf.constant(2, name: "x");
  50. var y = tf.constant(5, name: "y");
  51. var z = control_flow_ops.cond(tf.less(x, y),
  52. () => tf.multiply(x, 17),
  53. () => tf.add(y, 23));
  54. int result = z.eval(sess);
  55. assertEquals(result, 34);
  56. });
  57. }
  58. [TestMethod]
  59. public void testCondFalse()
  60. {
  61. var graph = tf.Graph().as_default();
  62. with(tf.Session(graph), sess =>
  63. {
  64. var x = tf.constant(2);
  65. var y = tf.constant(1);
  66. var z = control_flow_ops.cond(tf.less(x, y),
  67. () => tf.multiply(x, 17),
  68. () => tf.add(y, 23));
  69. int result = z.eval(sess);
  70. assertEquals(result, 24);
  71. });
  72. }
  73. // NOTE: all other test python test cases of this class are either not needed due to strong typing or dest a deprecated api
  74. }
  75. }

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