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

6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Text;
  7. using Tensorflow;
  8. namespace TensorFlowNET.UnitTest
  9. {
  10. [TestClass]
  11. public class ConstantTest
  12. {
  13. Tensor tensor;
  14. [TestMethod]
  15. public void ScalarConst()
  16. {
  17. tensor = tf.constant(8); // int
  18. tensor = tf.constant(6.0f); // float
  19. tensor = tf.constant(6.0); // double
  20. }
  21. [TestMethod]
  22. public void StringConst()
  23. {
  24. string str = "Hello, TensorFlow.NET!";
  25. tensor = tf.constant(str);
  26. Python.with<Session>(tf.Session(), sess =>
  27. {
  28. var result = sess.run(tensor);
  29. Assert.IsTrue(result.Data<string>()[0] == str);
  30. });
  31. }
  32. [TestMethod]
  33. public void ZerosConst()
  34. {
  35. // small size
  36. tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small");
  37. Python.with<Session>(tf.Session(), sess =>
  38. {
  39. var result = sess.run(tensor);
  40. Assert.AreEqual(result.shape[0], 3);
  41. Assert.AreEqual(result.shape[1], 2);
  42. Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 0, 0, 0, 0, 0, 0 }, result.Data<int>()));
  43. });
  44. // big size
  45. tensor = tf.zeros(new Shape(200, 100), TF_DataType.TF_INT32, "big");
  46. Python.with<Session>(tf.Session(), sess =>
  47. {
  48. var result = sess.run(tensor);
  49. Assert.AreEqual(result.shape[0], 200);
  50. Assert.AreEqual(result.shape[1], 100);
  51. var data = result.Data<int>();
  52. Assert.AreEqual(0, data[0]);
  53. Assert.AreEqual(0, data[result.size - 1]);
  54. });
  55. }
  56. [TestMethod]
  57. public void NDimConst()
  58. {
  59. var nd = np.array(new int[][]
  60. {
  61. new int[]{ 1, 2, 3 },
  62. new int[]{ 4, 5, 6 }
  63. });
  64. tensor = tf.constant(nd);
  65. }
  66. }
  67. }

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