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

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