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.

LayersTest.cs 3.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow.Keras.Engine;
  6. using Tensorflow.Keras.Layers;
  7. using NumSharp;
  8. using Tensorflow.UnitTest;
  9. using static Tensorflow.Binding;
  10. namespace TensorFlowNET.UnitTest.Keras
  11. {
  12. /// <summary>
  13. /// https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/keras/layers
  14. /// </summary>
  15. [TestClass]
  16. public class LayersTest : EagerModeTestBase
  17. {
  18. [TestMethod]
  19. public void Sequential()
  20. {
  21. var model = tf.keras.Sequential();
  22. model.add(tf.keras.Input(shape: 16));
  23. }
  24. [TestMethod]
  25. public void Functional()
  26. {
  27. var inputs = keras.Input(shape: 784);
  28. Assert.AreEqual((None, 784), inputs.TensorShape);
  29. var dense = layers.Dense(64, activation: "relu");
  30. var x = dense.Apply(inputs);
  31. x = layers.Dense(64, activation: "relu").Apply(x);
  32. var outputs = layers.Dense(10).Apply(x);
  33. var model = keras.Model(inputs, outputs, name: "mnist_model");
  34. model.summary();
  35. }
  36. /// <summary>
  37. /// https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding
  38. /// </summary>
  39. [TestMethod, Ignore]
  40. public void Embedding()
  41. {
  42. var model = tf.keras.Sequential();
  43. var layer = tf.keras.layers.Embedding(7, 2, input_length: 4);
  44. model.add(layer);
  45. // the model will take as input an integer matrix of size (batch,
  46. // input_length).
  47. // the largest integer (i.e. word index) in the input should be no larger
  48. // than 999 (vocabulary size).
  49. // now model.output_shape == (None, 10, 64), where None is the batch
  50. // dimension.
  51. var input_array = np.array(new int[,]
  52. {
  53. { 1, 2, 3, 4 },
  54. { 2, 3, 4, 5 },
  55. { 3, 4, 5, 6 }
  56. });
  57. model.compile("rmsprop", "mse");
  58. var output_array = model.predict(input_array);
  59. Assert.AreEqual((32, 10, 64), output_array.TensorShape);
  60. }
  61. /// <summary>
  62. /// https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
  63. /// </summary>
  64. [TestMethod]
  65. public void Dense()
  66. {
  67. // Create a `Sequential` model and add a Dense layer as the first layer.
  68. var model = tf.keras.Sequential();
  69. model.add(tf.keras.Input(shape: 16));
  70. model.add(tf.keras.layers.Dense(32, activation: "relu"));
  71. // Now the model will take as input arrays of shape (None, 16)
  72. // and output arrays of shape (None, 32).
  73. // Note that after the first layer, you don't need to specify
  74. // the size of the input anymore:
  75. model.add(tf.keras.layers.Dense(32));
  76. Assert.AreEqual((-1, 32), model.output_shape);
  77. }
  78. [TestMethod]
  79. public void SimpleRNN()
  80. {
  81. }
  82. }
  83. }