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

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