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

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

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