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 3.7 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow;
  6. using NumSharp.Core;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. [TestClass]
  10. public class VariableTest : Python
  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", "var1", 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. with(tf.variable_scope("foo"), delegate
  37. {
  38. with(tf.variable_scope("bar"), delegate
  39. {
  40. var v = tf.get_variable("v", new TensorShape(1));
  41. Assert.AreEqual(v.name, "foo/bar/v:0");
  42. });
  43. });
  44. }
  45. [TestMethod]
  46. public void ReenterVariableScope()
  47. {
  48. }
  49. [TestMethod]
  50. public void ScalarVar()
  51. {
  52. var x = tf.constant(3, name: "x");
  53. var y = tf.Variable(x + 1, name: "y");
  54. var model = tf.global_variables_initializer();
  55. using (var session = tf.Session())
  56. {
  57. session.run(model);
  58. int result = session.run(y);
  59. Assert.AreEqual(result, 4);
  60. }
  61. }
  62. [TestMethod]
  63. public void Assign1()
  64. {
  65. with(tf.Graph().as_default(), graph =>
  66. {
  67. var variable = tf.Variable(31, name: "tree");
  68. var init = tf.global_variables_initializer();
  69. var sess = tf.Session(graph);
  70. sess.run(init);
  71. var result = sess.run(variable);
  72. Assert.IsTrue((int)result == 31);
  73. var assign = variable.assign(12);
  74. result = sess.run(assign);
  75. Assert.IsTrue((int)result == 12);
  76. });
  77. }
  78. [TestMethod]
  79. public void Assign2()
  80. {
  81. var v1 = tf.Variable(10.0f, name: "v1"); //tf.get_variable("v1", shape: new TensorShape(3), initializer: tf.zeros_initializer);
  82. var inc_v1 = v1.assign(v1 + 1.0f);
  83. // Add an op to initialize the variables.
  84. var init_op = tf.global_variables_initializer();
  85. with(tf.Session(), sess =>
  86. {
  87. sess.run(init_op);
  88. // o some work with the model.
  89. inc_v1.op.run();
  90. });
  91. }
  92. /// <summary>
  93. /// https://databricks.com/tensorflow/variables
  94. /// </summary>
  95. [TestMethod]
  96. public void Add()
  97. {
  98. int result = 0;
  99. Tensor x = tf.Variable(10, name: "x");
  100. var init_op = tf.global_variables_initializer();
  101. using (var session = tf.Session())
  102. {
  103. session.run(init_op);
  104. for(int i = 0; i < 5; i++)
  105. {
  106. x = x + 1;
  107. result = session.run(x);
  108. print(result);
  109. }
  110. }
  111. Assert.AreEqual(15, result);
  112. }
  113. }
  114. }

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