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

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