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.

ConstantTest.cs 4.0 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using NumSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using Tensorflow;
  9. using static Tensorflow.Python;
  10. namespace TensorFlowNET.UnitTest
  11. {
  12. [TestClass]
  13. public class ConstantTest
  14. {
  15. Status status = new Status();
  16. [TestMethod]
  17. public void ScalarConst()
  18. {
  19. var tensor1 = tf.constant(8); // int
  20. var tensor2 = tf.constant(6.0f); // float
  21. var tensor3 = tf.constant(6.0); // double
  22. }
  23. [TestMethod]
  24. public void StringConst()
  25. {
  26. string str = "Hello, TensorFlow.NET!";
  27. var tensor = tf.constant(str);
  28. with(tf.Session(), sess =>
  29. {
  30. var result = sess.run(tensor);
  31. Assert.IsTrue(result.Data<string>()[0] == str);
  32. });
  33. }
  34. [TestMethod]
  35. public void ZerosConst()
  36. {
  37. // small size
  38. var tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small");
  39. with(tf.Session(), sess =>
  40. {
  41. var result = sess.run(tensor);
  42. Assert.AreEqual(result.shape[0], 3);
  43. Assert.AreEqual(result.shape[1], 2);
  44. Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 0, 0, 0, 0, 0, 0 }, result.Data<int>()));
  45. });
  46. // big size
  47. tensor = tf.zeros(new Shape(200, 100), TF_DataType.TF_INT32, "big");
  48. with(tf.Session(), sess =>
  49. {
  50. var result = sess.run(tensor);
  51. Assert.AreEqual(result.shape[0], 200);
  52. Assert.AreEqual(result.shape[1], 100);
  53. var data = result.Data<int>();
  54. Assert.AreEqual(0, data[0]);
  55. Assert.AreEqual(0, data[500]);
  56. Assert.AreEqual(0, data[result.size - 1]);
  57. });
  58. }
  59. [TestMethod]
  60. public void NDimConst()
  61. {
  62. var nd = np.array(new int[][]
  63. {
  64. new int[]{ 3, 1, 1 },
  65. new int[]{ 2, 1, 3 }
  66. });
  67. var tensor = tf.constant(nd);
  68. with(tf.Session(), sess =>
  69. {
  70. var result = sess.run(tensor);
  71. var data = result.Data<int>();
  72. Assert.AreEqual(result.shape[0], 2);
  73. Assert.AreEqual(result.shape[1], 3);
  74. Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 3, 1, 1, 2, 1, 3 }, data));
  75. });
  76. }
  77. [TestMethod]
  78. public void Multiply()
  79. {
  80. var a = tf.constant(3.0);
  81. var b = tf.constant(2.0);
  82. var c = a * b;
  83. var sess = tf.Session();
  84. double result = sess.run(c);
  85. sess.close();
  86. Assert.AreEqual(6.0, result);
  87. }
  88. [TestMethod]
  89. public void StringEncode()
  90. {
  91. /*string str = "Hello, TensorFlow.NET!";
  92. var handle = Marshal.StringToHGlobalAnsi(str);
  93. ulong dst_len = c_api.TF_StringEncodedSize((UIntPtr)str.Length);
  94. Assert.AreEqual(dst_len, (ulong)23);
  95. IntPtr dst = Marshal.AllocHGlobal((int)dst_len);
  96. ulong encoded_len = c_api.TF_StringEncode(handle, (ulong)str.Length, dst, dst_len, status);
  97. Assert.AreEqual((ulong)23, encoded_len);
  98. Assert.AreEqual(status.Code, TF_Code.TF_OK);
  99. string encoded_str = Marshal.PtrToStringUTF8(dst + sizeof(byte));
  100. Assert.AreEqual(encoded_str, str);
  101. Assert.AreEqual(str.Length, Marshal.ReadByte(dst));*/
  102. //c_api.TF_StringDecode(dst, (ulong)str.Length, IntPtr.Zero, ref dst_len, status);
  103. }
  104. /// <summary>
  105. /// tensorflow\c\c_api_test.cc
  106. /// TestEncodeDecode
  107. /// </summary>
  108. [TestMethod]
  109. public void EncodeDecode()
  110. {
  111. }
  112. }
  113. }

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