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