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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using FluentAssertions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NumSharp;
  4. using System.Linq;
  5. using Tensorflow;
  6. using static Tensorflow.Binding;
  7. namespace TensorFlowNET.UnitTest.Basics
  8. {
  9. [TestClass]
  10. public class VariableTest
  11. {
  12. [Ignore]
  13. [TestMethod]
  14. public void NewVariable()
  15. {
  16. var x = tf.Variable(10, name: "x");
  17. Assert.AreEqual(0, x.shape.ndim);
  18. Assert.AreEqual(10, (int)x.numpy());
  19. }
  20. [TestMethod]
  21. public void StringVar()
  22. {
  23. var mammal1 = tf.Variable("Elephant", name: "var1", dtype: tf.@string);
  24. var mammal2 = tf.Variable("Tiger");
  25. }
  26. [TestMethod]
  27. public void VarSum()
  28. {
  29. var x = tf.constant(3, name: "x");
  30. var y = tf.Variable(x + 1, name: "y");
  31. Assert.AreEqual(4, (int)y.numpy());
  32. }
  33. [Ignore]
  34. [TestMethod]
  35. public void Assign1()
  36. {
  37. var variable = tf.Variable(31, name: "tree");
  38. var unread = variable.assign(12);
  39. Assert.AreEqual(12, (int)unread.numpy());
  40. }
  41. [TestMethod]
  42. public void Assign2()
  43. {
  44. var v1 = tf.Variable(10.0f, name: "v1");
  45. var v2 = v1.assign(v1 + 1.0f);
  46. Assert.AreEqual(v1.numpy(), v2.numpy());
  47. Assert.AreEqual(11f, (float)v1.numpy());
  48. }
  49. [TestMethod]
  50. public void Accumulation()
  51. {
  52. var x = tf.Variable(10, name: "x");
  53. for (int i = 0; i < 5; i++)
  54. x.assign(x + 1);
  55. Assert.AreEqual(15, (int)x.numpy());
  56. }
  57. [TestMethod]
  58. public void ShouldReturnNegative()
  59. {
  60. var x = tf.constant(new[,] { { 1, 2 } });
  61. var neg_x = tf.negative(x);
  62. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 1, 2 }, neg_x.shape));
  63. Assert.IsTrue(Enumerable.SequenceEqual(new[] { -1, -2 }, neg_x.numpy().ToArray<int>()));
  64. }
  65. }
  66. }