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.

DatasetTest.cs 1.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow.UnitTest;
  6. using static Tensorflow.Binding;
  7. namespace TensorFlowNET.UnitTest.Dataset
  8. {
  9. [TestClass]
  10. public class DatasetTest : EagerModeTestBase
  11. {
  12. [TestMethod]
  13. public void Range()
  14. {
  15. int iStep = 0;
  16. long value = 0;
  17. var dataset = tf.data.Dataset.range(3);
  18. foreach(var (step, item) in enumerate(dataset))
  19. {
  20. Assert.AreEqual(iStep, step);
  21. iStep++;
  22. Assert.AreEqual(value, (long)item.Item1);
  23. value++;
  24. }
  25. }
  26. [TestMethod]
  27. public void Prefetch()
  28. {
  29. int iStep = 0;
  30. long value = 1;
  31. var dataset = tf.data.Dataset.range(1, 5, 2);
  32. dataset = dataset.prefetch(2);
  33. foreach (var (step, item) in enumerate(dataset))
  34. {
  35. Assert.AreEqual(iStep, step);
  36. iStep++;
  37. Assert.AreEqual(value, (long)item.Item1);
  38. value += 2;
  39. }
  40. }
  41. [TestMethod]
  42. public void FromTensorSlices()
  43. {
  44. var X = tf.constant(new[] { 2013, 2014, 2015, 2016, 2017 });
  45. var Y = tf.constant(new[] { 12000, 14000, 15000, 16500, 17500 });
  46. var dataset = tf.data.Dataset.from_tensor_slices(X, Y);
  47. int n = 0;
  48. foreach (var (item_x, item_y) in dataset)
  49. {
  50. print($"x:{item_x.numpy()},y:{item_y.numpy()}");
  51. n += 1;
  52. }
  53. Assert.AreEqual(5, n);
  54. }
  55. }
  56. }