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.

KerasInterface.cs 3.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using Tensorflow.Keras.ArgsDefinition;
  3. using Tensorflow.Keras.Datasets;
  4. using Tensorflow.Keras.Engine;
  5. using Tensorflow.Keras.Layers;
  6. using Tensorflow.Keras.Losses;
  7. using Tensorflow.Keras.Metrics;
  8. using Tensorflow.Keras.Models;
  9. using Tensorflow.Keras.Optimizers;
  10. using Tensorflow.Keras.Saving;
  11. namespace Tensorflow.Keras
  12. {
  13. public class KerasInterface
  14. {
  15. public KerasDataset datasets { get; } = new KerasDataset();
  16. public Initializers initializers { get; } = new Initializers();
  17. public Regularizers regularizers { get; } = new Regularizers();
  18. public LayersApi layers { get; } = new LayersApi();
  19. public LossesApi losses { get; } = new LossesApi();
  20. public Activations activations { get; } = new Activations();
  21. public Preprocessing preprocessing { get; } = new Preprocessing();
  22. public BackendImpl backend { get; } = new BackendImpl();
  23. public OptimizerApi optimizers { get; } = new OptimizerApi();
  24. public MetricsApi metrics { get; } = new MetricsApi();
  25. public ModelsApi models { get; } = new ModelsApi();
  26. public Sequential Sequential(List<ILayer> layers = null,
  27. string name = null)
  28. => new Sequential(new SequentialArgs
  29. {
  30. Layers = layers,
  31. Name = name
  32. });
  33. /// <summary>
  34. /// `Model` groups layers into an object with training and inference features.
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <param name="output"></param>
  38. /// <returns></returns>
  39. public Functional Model(Tensors inputs, Tensors outputs, string name = null)
  40. => new Functional(inputs, outputs, name: name);
  41. /// <summary>
  42. /// Instantiate a Keras tensor.
  43. /// </summary>
  44. /// <param name="shape"></param>
  45. /// <param name="batch_size"></param>
  46. /// <param name="dtype"></param>
  47. /// <param name="name"></param>
  48. /// <param name="sparse">
  49. /// A boolean specifying whether the placeholder to be created is sparse.
  50. /// </param>
  51. /// <param name="ragged">
  52. /// A boolean specifying whether the placeholder to be created is ragged.
  53. /// </param>
  54. /// <param name="tensor">
  55. /// Optional existing tensor to wrap into the `Input` layer.
  56. /// If set, the layer will not create a placeholder tensor.
  57. /// </param>
  58. /// <returns></returns>
  59. public Tensor Input(TensorShape shape = null,
  60. int batch_size = -1,
  61. TensorShape batch_input_shape = null,
  62. TF_DataType dtype = TF_DataType.DtInvalid,
  63. string name = null,
  64. bool sparse = false,
  65. bool ragged = false,
  66. Tensor tensor = null)
  67. {
  68. if (batch_input_shape != null)
  69. shape = batch_input_shape.dims[1..];
  70. var args = new InputLayerArgs
  71. {
  72. Name = name,
  73. InputShape = shape,
  74. BatchInputShape = batch_input_shape,
  75. BatchSize = batch_size,
  76. DType = dtype,
  77. Sparse = sparse,
  78. Ragged = ragged,
  79. InputTensor = tensor
  80. };
  81. var layer = new InputLayer(args);
  82. return layer.InboundNodes[0].Outputs;
  83. }
  84. }
  85. }