|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Tensorflow.Keras.ArgsDefinition; |
| 5 | +using Tensorflow.Keras.Engine; |
| 6 | +using static Tensorflow.Binding; |
| 7 | + |
| 8 | +namespace Tensorflow.Keras.Layers |
| 9 | +{ |
| 10 | + public class LayersApi |
| 11 | + { |
| 12 | + public Conv2D Conv2D(int filters, |
| 13 | + TensorShape kernel_size = null, |
| 14 | + string padding = "valid", |
| 15 | + string activation = "relu") |
| 16 | + => new Conv2D(new Conv2DArgs |
| 17 | + { |
| 18 | + Filters = filters, |
| 19 | + KernelSize = kernel_size, |
| 20 | + Padding = padding, |
| 21 | + Activation = GetActivationByName(activation) |
| 22 | + }); |
| 23 | + |
| 24 | + |
| 25 | + public Dense Dense(int units, |
| 26 | + string activation = "linear", |
| 27 | + TensorShape input_shape = null) |
| 28 | + => new Dense(new DenseArgs |
| 29 | + { |
| 30 | + Units = units, |
| 31 | + Activation = GetActivationByName(activation), |
| 32 | + InputShape = input_shape |
| 33 | + }); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Turns positive integers (indexes) into dense vectors of fixed size. |
| 37 | + /// </summary> |
| 38 | + /// <param name="input_dim"></param> |
| 39 | + /// <param name="output_dim"></param> |
| 40 | + /// <param name="embeddings_initializer"></param> |
| 41 | + /// <param name="mask_zero"></param> |
| 42 | + /// <returns></returns> |
| 43 | + public Embedding Embedding(int input_dim, |
| 44 | + int output_dim, |
| 45 | + IInitializer embeddings_initializer = null, |
| 46 | + bool mask_zero = false, |
| 47 | + TensorShape input_shape = null, |
| 48 | + int input_length = -1) |
| 49 | + => new Embedding(new EmbeddingArgs |
| 50 | + { |
| 51 | + InputDim = input_dim, |
| 52 | + OutputDim = output_dim, |
| 53 | + MaskZero = mask_zero, |
| 54 | + InputShape = input_shape ?? input_length, |
| 55 | + InputLength = input_length, |
| 56 | + EmbeddingsInitializer = embeddings_initializer |
| 57 | + }); |
| 58 | + |
| 59 | + public Flatten Flatten(string data_format = null) |
| 60 | + => new Flatten(new FlattenArgs |
| 61 | + { |
| 62 | + DataFormat = data_format |
| 63 | + }); |
| 64 | + |
| 65 | + public MaxPooling2D MaxPooling2D(TensorShape pool_size = null, |
| 66 | + TensorShape strides = null, |
| 67 | + string padding = "valid") |
| 68 | + => new MaxPooling2D(new MaxPooling2DArgs |
| 69 | + { |
| 70 | + PoolSize = pool_size ?? (2, 2), |
| 71 | + Strides = strides, |
| 72 | + Padding = padding |
| 73 | + }); |
| 74 | + |
| 75 | + public Rescaling Rescaling(float scale, |
| 76 | + float offset = 0, |
| 77 | + TensorShape input_shape = null) |
| 78 | + => new Rescaling(new RescalingArgs |
| 79 | + { |
| 80 | + Scale = scale, |
| 81 | + Offset = offset, |
| 82 | + InputShape = input_shape |
| 83 | + }); |
| 84 | + |
| 85 | + Activation GetActivationByName(string name) |
| 86 | + => name switch |
| 87 | + { |
| 88 | + "linear" => tf.keras.activations.Linear, |
| 89 | + "relu" => tf.keras.activations.Relu, |
| 90 | + "sigmoid" => tf.keras.activations.Sigmoid, |
| 91 | + "tanh" => tf.keras.activations.Tanh, |
| 92 | + _ => tf.keras.activations.Linear |
| 93 | + }; |
| 94 | + } |
| 95 | +} |
0 commit comments