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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. tf.Graph().as_default();
  47. var x = tf.constant(2, name: "x");
  48. var y = tf.constant(5, name: "y");
  49. var z = control_flow_ops.cond(tf.less(x, y),
  50. () => tf.multiply(x, 17),
  51. () => tf.add(y, 23));
  52. var result = evaluate<int>(z);
  53. assertEquals(result, 34);
  54. }
  55. [TestMethod]
  56. public void testCondFalse()
  57. {
  58. tf.Graph().as_default();
  59. var x = tf.constant(2);
  60. var y = tf.constant(1);
  61. var z = control_flow_ops.cond(tf.less(x, y),
  62. () => tf.multiply(x, 17),
  63. () => tf.add(y, 23));
  64. var result = evaluate<int>(z);
  65. assertEquals(result, 24);
  66. }
  67. // NOTE: all other python test cases of this class are either not needed due to strong typing or test a deprecated api
  68. }
  69. }

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