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.

VariableTest.cs 4.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow;
  3. using static Tensorflow.Python;
  4. namespace TensorFlowNET.UnitTest
  5. {
  6. [TestClass]
  7. public class VariableTest
  8. {
  9. [TestMethod]
  10. public void Initializer()
  11. {
  12. var x = tf.Variable(10, name: "x");
  13. using (var session = tf.Session())
  14. {
  15. session.run(x.initializer);
  16. var result = session.run(x);
  17. Assert.AreEqual(10, (int)result);
  18. }
  19. }
  20. [TestMethod]
  21. public void StringVar()
  22. {
  23. var mammal1 = tf.Variable("Elephant", name: "var1", dtype: tf.chars);
  24. var mammal2 = tf.Variable("Tiger");
  25. }
  26. /// <summary>
  27. /// https://www.tensorflow.org/api_docs/python/tf/variable_scope
  28. /// how to create a new variable
  29. /// </summary>
  30. [TestMethod]
  31. public void VarCreation()
  32. {
  33. tf.Graph().as_default();
  34. with(tf.variable_scope("foo"), delegate
  35. {
  36. with(tf.variable_scope("bar"), delegate
  37. {
  38. var v = tf.get_variable("v", new TensorShape(1));
  39. Assert.AreEqual(v.name, "foo/bar/v:0");
  40. });
  41. });
  42. }
  43. /// <summary>
  44. /// how to reenter a premade variable scope safely
  45. /// </summary>
  46. [TestMethod]
  47. public void ReenterVariableScope()
  48. {
  49. tf.Graph().as_default();
  50. variable_scope vs = null;
  51. with(tf.variable_scope("foo"), v => vs = v);
  52. // Re-enter the variable scope.
  53. with(tf.variable_scope(vs, auxiliary_name_scope: false), v =>
  54. {
  55. var vs1 = (VariableScope)v;
  56. // Restore the original name_scope.
  57. with(tf.name_scope(vs1.original_name_scope), delegate
  58. {
  59. var v1 = tf.get_variable("v", new TensorShape(1));
  60. Assert.AreEqual(v1.name, "foo/v:0");
  61. var c1 = tf.constant(new int[] { 1 }, name: "c");
  62. Assert.AreEqual(c1.name, "foo/c:0");
  63. });
  64. });
  65. }
  66. [TestMethod]
  67. public void ScalarVar()
  68. {
  69. var x = tf.constant(3, name: "x");
  70. var y = tf.Variable(x + 1, name: "y");
  71. var model = tf.global_variables_initializer();
  72. using (var session = tf.Session())
  73. {
  74. session.run(model);
  75. int result = session.run(y);
  76. Assert.AreEqual(result, 4);
  77. }
  78. }
  79. [TestMethod]
  80. public void Assign1()
  81. {
  82. with(tf.Graph().as_default(), graph =>
  83. {
  84. var variable = tf.Variable(31, name: "tree");
  85. var init = tf.global_variables_initializer();
  86. var sess = tf.Session(graph);
  87. sess.run(init);
  88. var result = sess.run(variable);
  89. Assert.IsTrue((int)result == 31);
  90. var assign = variable.assign(12);
  91. result = sess.run(assign);
  92. Assert.IsTrue((int)result == 12);
  93. });
  94. }
  95. [TestMethod]
  96. public void Assign2()
  97. {
  98. var v1 = tf.Variable(10.0f, name: "v1"); //tf.get_variable("v1", shape: new TensorShape(3), initializer: tf.zeros_initializer);
  99. var inc_v1 = v1.assign(v1 + 1.0f);
  100. // Add an op to initialize the variables.
  101. var init_op = tf.global_variables_initializer();
  102. with(tf.Session(), sess =>
  103. {
  104. sess.run(init_op);
  105. // o some work with the model.
  106. inc_v1.op.run();
  107. });
  108. }
  109. /// <summary>
  110. /// https://databricks.com/tensorflow/variables
  111. /// </summary>
  112. [TestMethod]
  113. public void Add()
  114. {
  115. int result = 0;
  116. Tensor x = tf.Variable(10, name: "x");
  117. var init_op = tf.global_variables_initializer();
  118. using (var session = tf.Session())
  119. {
  120. session.run(init_op);
  121. for(int i = 0; i < 5; i++)
  122. {
  123. x = x + 1;
  124. result = session.run(x);
  125. print(result);
  126. }
  127. }
  128. Assert.AreEqual(15, result);
  129. }
  130. }
  131. }