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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. [TestMethod]
  30. public void ScalarVar()
  31. {
  32. var x = tf.Variable(3);
  33. var y = tf.Variable(6f);
  34. }
  35. /// <summary>
  36. /// https://databricks.com/tensorflow/variables
  37. /// </summary>
  38. [TestMethod]
  39. public void Add()
  40. {
  41. var x = tf.Variable(10, name: "x");
  42. var model = tf.global_variables_initializer();
  43. using (var session = tf.Session())
  44. {
  45. session.run(x.initializer);
  46. for(int i = 0; i < 5; i++)
  47. {
  48. var x1 = x + 1;
  49. var result = session.run(x1);
  50. print(result);
  51. }
  52. }
  53. }
  54. }
  55. }

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