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

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Utils;
  14. using System.Threading;
  15. using Tensorflow.Framework.Models;
  16. namespace Tensorflow.Keras
  17. {
  18. public class KerasInterface : IKerasApi
  19. {
  20. private static KerasInterface _instance = null;
  21. private static readonly object _lock = new object();
  22. private KerasInterface()
  23. {
  24. Tensorflow.Binding.tf.keras = this;
  25. }
  26. public static KerasInterface Instance
  27. {
  28. get
  29. {
  30. lock (_lock)
  31. {
  32. if (_instance is null)
  33. {
  34. _instance = new KerasInterface();
  35. }
  36. return _instance;
  37. }
  38. }
  39. }
  40. public KerasDataset datasets { get; } = new KerasDataset();
  41. public IInitializersApi initializers { get; } = new InitializersApi();
  42. public Regularizers regularizers { get; } = new Regularizers();
  43. public ILayersApi layers { get; } = new LayersApi();
  44. public ILossesApi losses { get; } = new LossesApi();
  45. public Activations activations { get; } = new Activations();
  46. public Preprocessing preprocessing { get; } = new Preprocessing();
  47. ThreadLocal<BackendImpl> _backend = new ThreadLocal<BackendImpl>(() => new BackendImpl());
  48. public BackendImpl backend => _backend.Value;
  49. public IOptimizerApi optimizers { get; } = new OptimizerApi();
  50. public IMetricsApi metrics { get; } = new MetricsApi();
  51. public IModelsApi models { get; } = new ModelsApi();
  52. public KerasUtils utils { get; } = new KerasUtils();
  53. public Sequential Sequential(List<ILayer> layers = null,
  54. string name = null)
  55. => new Sequential(new SequentialArgs
  56. {
  57. Layers = layers,
  58. Name = name
  59. });
  60. /// <summary>
  61. /// `Model` groups layers into an object with training and inference features.
  62. /// </summary>
  63. /// <param name="input"></param>
  64. /// <param name="output"></param>
  65. /// <returns></returns>
  66. public IModel Model(Tensors inputs, Tensors outputs, string name = null)
  67. => new Functional(inputs, outputs, name: name);
  68. /// <summary>
  69. /// Instantiate a Keras tensor.
  70. /// </summary>
  71. /// <param name="shape"></param>
  72. /// <param name="batch_size"></param>
  73. /// <param name="dtype"></param>
  74. /// <param name="name"></param>
  75. /// <param name="sparse">
  76. /// A boolean specifying whether the placeholder to be created is sparse.
  77. /// </param>
  78. /// <param name="ragged">
  79. /// A boolean specifying whether the placeholder to be created is ragged.
  80. /// </param>
  81. /// <param name="tensor">
  82. /// Optional existing tensor to wrap into the `Input` layer.
  83. /// If set, the layer will not create a placeholder tensor.
  84. /// </param>
  85. /// <returns></returns>
  86. public Tensors Input(Shape shape = null,
  87. int batch_size = -1,
  88. string name = null,
  89. TF_DataType dtype = TF_DataType.DtInvalid,
  90. bool sparse = false,
  91. Tensor tensor = null,
  92. bool ragged = false,
  93. TypeSpec type_spec = null,
  94. Shape batch_input_shape = null,
  95. Shape batch_shape = null) => keras.layers.Input(shape, batch_size, name,
  96. dtype, sparse, tensor, ragged, type_spec, batch_input_shape, batch_shape);
  97. }
  98. }