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

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

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