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.

StringsApiTest.cs 2.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow;
  3. using static Tensorflow.Binding;
  4. namespace TensorFlowNET.UnitTest.ManagedAPI
  5. {
  6. [TestClass]
  7. public class StringsApiTest
  8. {
  9. [TestMethod]
  10. public void StringFromBytes()
  11. {
  12. var jpg = tf.constant(new byte[] { 0x41, 0xff, 0xd8, 0xff }, tf.@string);
  13. var strings = jpg.ToString();
  14. Assert.AreEqual(strings, @"tf.Tensor: shape=(), dtype=string, numpy='A\xff\xd8\xff'");
  15. }
  16. [TestMethod]
  17. public void StringEqual()
  18. {
  19. var str1 = tf.constant("Hello1");
  20. var str2 = tf.constant("Hello2");
  21. var result = tf.equal(str1, str2);
  22. Assert.IsFalse(result.numpy());
  23. var str3 = tf.constant("Hello1");
  24. result = tf.equal(str1, str3);
  25. Assert.IsTrue(result.numpy());
  26. var str4 = tf.strings.substr(str1, 0, 5);
  27. var str5 = tf.strings.substr(str2, 0, 5);
  28. result = tf.equal(str4, str5);
  29. Assert.IsTrue(result.numpy());
  30. }
  31. [TestMethod]
  32. public void ImageType()
  33. {
  34. var imgPath = TestHelper.GetFullPathFromDataDir("shasta-daisy.jpg");
  35. var contents = tf.io.read_file(imgPath);
  36. var substr = tf.strings.substr(contents, 0, 3);
  37. var jpg = tf.constant(new byte[] { 0xff, 0xd8, 0xff }, tf.@string);
  38. var result = math_ops.equal(substr, jpg);
  39. Assert.IsTrue((bool)result);
  40. }
  41. [TestMethod]
  42. public void StringArray()
  43. {
  44. var strings = new[] { "map_and_batch_fusion", "noop_elimination", "shuffle_and_repeat_fusion" };
  45. var tensor = tf.constant(strings, dtype: tf.@string, name: "optimizations");
  46. Assert.AreEqual(3, tensor.shape[0]);
  47. Assert.AreEqual(tensor[0].numpy(), strings[0]);
  48. Assert.AreEqual(tensor[1].numpy(), strings[1]);
  49. Assert.AreEqual(tensor[2].numpy(), strings[2]);
  50. }
  51. [TestMethod]
  52. public void StringSplit()
  53. {
  54. var tensor = tf.constant(new[] { "hello world", "tensorflow .net csharp", "fsharp" });
  55. var ragged_tensor = tf.strings.split(tensor);
  56. Assert.AreEqual((3, -1), ragged_tensor.shape);
  57. }
  58. }
  59. }